Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
mobydickins
Starting Member
3 Posts |
Posted - 2007-06-20 : 04:44:58
|
| I have a table (aBookings) with BookingID int SiteID int FromDate DateTime ToDate DateTime I need an sql statement that will give me all bookings for every day between two periods (Say a year)The follownig statements give me the correct result but all as seperate data lists.Is there a way of getting the desired result so I can fill a datagrid from one statement Declare @MinDate DateTimeDeclare @MaxDate DateTimeDeclare @DateLoop DateTimeDeclare @DayCount intSelect @MinDate = Min(FromDate) From aBookingsSelect @MaxDate = Max(ToDate) From aBookingsSet @DateLoop = @MinDateWhile @DateLoop < @MaxDateBeginSELECT Count(BookingID), @DateLoop AS Expr1FROM aBookings Where SiteID = 26 And @DateLoop Between FromDate And ToDate Having Count(BookingID) <> 0 Set @DateLoop = DateAdd(day,1,@DateLoop)End |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-06-20 : 05:09:40
|
Try this.Declare @MinDate DateTimeDeclare @MaxDate DateTimeSelect @MinDate = Min(FromDate) From aBookingsSelect @MaxDate = Max(ToDate) From aBookingsSELECT d.[DATE], Count(b.BookingID)-- F_TABLE_DATE is from http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=61519&SearchTerms=F_TABLE_DATEFROM F_TABLE_DATE_RANGE(@MinDate, @MaxDate) d INNER JOIN aBookings bON d.[DATE] BETWEEN b.FromDate AND b.ToDateWHERE SiteID = 26 GROUP BY d.[DATE]Having Count(b.BookingID) <> 0 KH |
 |
|
|
mobydickins
Starting Member
3 Posts |
Posted - 2007-06-20 : 06:20:49
|
| Thanks for the swift reply.I get this...Invalid object name 'F_TABLE_DATE_RANGE'.Any ideas? |
 |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
|
|
mobydickins
Starting Member
3 Posts |
Posted - 2007-06-20 : 06:26:32
|
| Sorted.F_TABLE_DATEThat is brilliant thanks for your help :-)Phil |
 |
|
|
|
|
|