Database Management
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesDatabase Management

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 11th, 2004, 03:29 PM
jakuza's Avatar
jakuza jakuza is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Posts: 65 jakuza User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 46 m 52 sec
Reputation Power: 6
Partial intervals

Hi everybody,
we have these 2 tables: 'resources' with just an id as key (we don't mind other columns now) and 'periods' with a key, a foreign key to a resource, a start time, an end time (both timestamps) and a price (integer or not, it doesn't matter).
These periods express the resources fares; suppose a bungalow: from 12th Dec 2004 to 23rd Dec 2004 $150 per night but from 24th Dec to 27th Dec $250 and so forth....
Given a check-in date and a check-out one, we'd like to calculate the total fare for this bungalow, even for mixed-period staying (from 20th to 25th December, in our example).
I thought something like SUM(price)s grouped by periods.id could work... but I don't feel comfortable with check-in/out dates that fall in the middle of a period... Has anyone an idea about it?
Thanks in advance, bye!

Reply With Quote
  #2  
Old January 11th, 2004, 04:00 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,950 r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 15 h 29 m 40 sec
Reputation Power: 1024
okay, your results will depend on the checkin and checkout dates, so those are two variables

first, start with an integers table, which has just one column called i, which takes values 0, 1, 2, 3, ...

now select rows from this table based on checkin and checkout dates, like this --

SELECT DATE_ADD($checkin, INTERVAL i DAY)
FROM integers
WHERE DATE_ADD($checkin, INTERVAL i DAY) <= $checkout

(the above uses mysql syntax, your mileage may vary)

to get that working, just run it a few times to make sure it generates the correct range of dates

now join the above to your periods table, such that the date generated lies between the startdate and enddate of the period

to get that working, add the period columns to the SELECT list, and run it a few times to make sure it joins each generated date to the correct range of period startdates and enddates

now remove everything from the SELECT list except the resource foreign key, add SUM(price), and add a GROUP BY on the resource foreign key

voila
__________________
r937.com | rudy.ca

pre-order my book Simply SQL from Amazon

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesDatabase Management > Partial intervals


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
Stay green...Green IT