2014-02-20 72 views
0

如何通過月份和星期組數據集團由週日期和月份

我知道這是不對的..

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

where子句不能內按... 但這種使用是想我需要

+0

你可以試試'having'而不是'where' – Miller

回答

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]]

1

where子句group by前:

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