sql - Grouping totals by date ranges -
i have totals table using different criteria, this:
select distinct sum(case when mycondition1 1 else 0 end) total1, sum(case when mycondition2 1 else 0 end) total2 table1, table2 common_condition1 , common_condition2 , between date1 , date2;
this works fine , intended result.
now, have repeat every week last 12 months, excluding holidays period. so, generate set of date ranges used in queries. so, repeat above sql statement date ranges, lengthy process.
i have totals every week. example 26-sep-2010 02-oct-2010, 19-sep-2010 25-sep-2010, 12-sep-2010 18-sep-2010, etc.. how should put ranges in query grouping , in select list, don't have them in table.
how can in single shot , totals each date range. please me sql.
thank you.
you create table contains date ranges. can join on new table; you'll have replace distinct group by. example:
select table3.date1 , table3.date2 , sum(case when mycondition1 1 else 0 end) total1 , sum(case when mycondition2 1 else 0 end) total2 table1, table2, table3 common_condition1 , common_condition2 , date_column between table3.date1 , table3.date2 group table3.date1 , table3.date2
Comments
Post a Comment