按工作日和月分组

时间:2014-02-20 04:39:32

标签: sql sql-server sql-server-2008

如何按月和周工作分组数据

我知道这是错的..

select sum(x) from demo_table 
group by DATEPART(month,date) 
where DATENAME(weekday,date)='sunday' ;

where子句不能在组内使用... 但这是我想要的

2 个答案:

答案 0 :(得分:1)

where子句放在group by

之前
select sum(x)
from demo_table
where DATENAME(weekday,date)='sunday'
group by DATEPART(month,date)  ;

答案 1 :(得分:1)

你可能想要

select  sum(x) 
from    demo_table 
where   DATENAME(weekday,date)='sunday'
group by DATEPART(month,date) 

来自SELECT (Transact-SQL)

  

[WITH common_table_expression]

     

SELECT select_list [INTO new_table]

     

[FROM table_source] [WHERE search_condition]

     

[GROUP BY group_by_expression]

     

[HAVING search_condition]

     

[ORDER BY order_expression [ASC | DESC]]