1. 程式人生 > >【終結版】sql 按照周 月 季度 年進行彙總

【終結版】sql 按照周 月 季度 年進行彙總

--按日 

selectsum(consume),day([date]from consume_record whereyear([date]='2006'

groupbyday([date]


--按周quarter 

selectsum(consume),datename(week,[date]from consume_record whereyear([date]='2006'

groupbydatename(week,[date]


--按月 

selectsum(consume),month([date]from consume_record whereyear([date

]='2006'

groupbymonth([date]


--按季 

selectsum(consume),datename(quarter,[date]from consume_record whereyear([date]='2006'

groupbydatename(quarter,[date]

--指定日期你就看上面的例子變通下呀,無非就是一個聚合函式和Group by 

select[date],sum(consume) from consume_record where[date]

between'2006-06-01'and'2006-07-10'groupby[date]