sql server 2008 - Is this is the best way to write SQL for this report? -
sql server 2008 - Is this is the best way to write SQL for this report? -
we have orders
info , need generate weekly - custom date range of 7 days
report using sql
. study display weekly count
of orders. user select end date
, need create 12 daterange units based on date. illustration user selects 1/24/2013
need 12 points/units as:
point12 = end date - 7 days point11 = end date - 14 days point10 = end date - 21 days point9 = end date - 28 days . . point1 = end date - x days
our solution:
we planning create temporary table table have 12 rows. each row have info (we calculate startdate , enddate each point):
point startdate enddate totalorders point12 2013-01-24 2013-01-30 point11 2013-01-17 2013-01-23
after count
of orders each row.
is solution problem or can optimized?
edit:
the weekly daterange custom date range of 7 days
.
given sqlserver, suggest using table variable, rather temporary table.
an alternative approach generate 12 rows in cte part of main query, rather generating required rows (in table variable/temporary table) separate step prior main query. cut down total number of steps required, create main query more complicated.
sql sql-server-2008 count group-by
Comments
Post a Comment