Home Forums SQL Server 2008 T-SQL (SS2K8) Counting field values per day between specific date ranges RE: Counting field values per day between specific date ranges

  • If you're counting values per day, you're missing the Calendar table. If you left join the Calendar table to the table of scheduled events, then it's stupid easy. You need all the days between these two dates:

    SELECT MIN(Arrival_Date) AS FirstDate

    ,MAX(Arrival_Date) AS LastDate

    FROM [wce_bookings];

    Then you join your Calendar table to this.

    SELECT c.Date, b.BookingID

    FROM Calendar c LEFT JOIN Booking b ON c.CalendarDate BETWEEN b.StartDate and b.EndDate

    then you can just count them