Link to home
Start Free TrialLog in
Avatar of mikegrad7
mikegrad7Flag for United States of America

asked on

Counts by SQL date range

hi, i need some help getting started with the proper SQL to do a count, or group by date range.

I have items that are rented over periods of time, or date ranges. Currently i can say, from 4/1 to 5/30 there are 20 items rented, and 15 not. I would like to enhance this to break out the booking by week, so it would look like the following table:

              4-7-14    4-14-14      4-21-14       5-5-14         5-12-14        5-19-14       5-26-14
Asset type 1
Rented      10            15                25               20             20                 10                5
not rent     20           15                5                 10            10                  20              25

Asset Type n
Rented      10            15                25               20             20                 10                5
not rent     20           15                5                 10            10                  20              25


notice the dates need to start on the Monday of each full week within the range, so while the choosen range here is 4/1-5/31, so the first week range is actually 4/7, because that is the first Monday (4/2 is a tuesday).

I'm just trying to get an idea of the way i need to group and structure the results to make this work? Can i do this in a single query or will it be better to use temp tables/table variables for manipulating the data? I just don't know how to get started to get this output. The table i showed above is the ideal result of an SSRS report - so the column headers there don't have to be the column headers in the query, as long as i can identify the start date, group, and asset type. there are multiple asset types.
ASKER CERTIFIED SOLUTION
Avatar of Jim P.
Jim P.
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mikegrad7

ASKER

wow, is it that easy? I hope so, i'm going to give it a shot and see.
Avatar of Scott Pletcher
Would need to see the "rent" table structure.  I suspect you will need a version of the "Gaps and Islands" queries researched by Itzik Ben-Gan.
If you want to count the 4/1-4/7 values as on 4/7 and 4/8-4/14 values as on 4/14, you can try like this.

This gives the next Monday of today. If today is Monday, it simply gives that date.
SELECT DATEADD(WEEK, DATEDIFF(WEEK, 0, GETDATE()), CASE WHEN DATEPART(dw,GETDATE()) <> 2 THEN 7 ELSE 0 END)

Open in new window

You can use this in your query.
SELECT NextMonday,COUNT(Rented) RentedCnt
  FROM (SELECT DATEADD(WEEK, DATEDIFF(WEEK, 0, DateFld), CASE WHEN DATEPART(dw,DateFld) <> 2 THEN 7 ELSE 0 END) NextMonday,
			   Rented
		  FROM RentalsTable) T1
 GROUP BY NextMonday
 ORDER BY NextMonday

Open in new window

accepting the first solution as thats what i looked at before going off and making the changes. All were good !