Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Query - Pulling Data from another table


Query - Pulling Data from another table

Author
Message
Kingston Dhasian
Kingston Dhasian
Hall of Fame
Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)

Group: General Forum Members
Points: 3003 Visits: 5007
wafw1971 (2/21/2013)
Hi Kingston

I didn't think of that and I should have, each price is a daily price so if the price for a pitch is £9.00 on the 31/01/2010 and the price of the pitch is £9.70 for the 01/01/2011 then the total should be £18.70

And after looking at the data that has not happened.

How do we amend the code to compensate for that?


This will be a bit complex
I will try and post a solution as soon as I get some time for that
Meanwhile I think somebody else might help as well..


Kingston Dhasian

How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Kingston Dhasian
Kingston Dhasian
Hall of Fame
Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)

Group: General Forum Members
Points: 3003 Visits: 5007
The below code will help you handle the overlapping conditions

SELECT   Bookings.Booking_Skey, 
SUM (
(
DATEDIFF(
DAY,
CASE
WHEN PitchValues.StartDate BETWEEN Bookings.ArrivalDate AND Bookings.DepartureDate THEN PitchValues.StartDate
ELSE Bookings.ArrivalDate
END,
CASE
WHEN PitchValues.EndDate BETWEEN Bookings.ArrivalDate AND Bookings.DepartureDate THEN PitchValues.EndDate
ELSE Bookings.DepartureDate
END
) + 1
) * PitchValues.Value
) AS BookingValue, PitchValues.PitchType_Skey
FROM Bookings
INNER JOIN PitchValues
ON Bookings.PitchType_Skey = PitchValues.PitchType_Skey
AND (
Bookings.ArrivalDate BETWEEN PitchValues.StartDate AND PitchValues.EndDate OR
Bookings.DepartureDate BETWEEN PitchValues.StartDate AND PitchValues.EndDate OR
PitchValues.StartDate BETWEEN Bookings.ArrivalDate AND Bookings.DepartureDate OR
PitchValues.EndDate BETWEEN Bookings.ArrivalDate AND Bookings.DepartureDate
)
WHERE (Bookings.Booking_Skey = 1)
GROUP BY Bookings.Booking_Skey, PitchValues.PitchType_Skey




Kingston Dhasian

How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search