bibinmatthew
bibinmatthew

Reputation: 177

Group by week day and Month

How to group data by month and week day

I know this is wrong..

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

where clause cannot be used inside group by... but this is want i need

Upvotes: 0

Views: 88

Answers (2)

Adriaan Stander
Adriaan Stander

Reputation: 166626

You probably want

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

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

Upvotes: 1

Gordon Linoff
Gordon Linoff

Reputation: 1271241

Put the where clause before the group by:

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

Upvotes: 1

Related Questions