MSCF
MSCF

Reputation: 157

SQL - GROUP BY not working with DateName

I'm trying to extract how many times a particular thing happened on each day of the week. Here is what I tried:

SELECT Source , Event, Qty, DateName(WEEKDAY,( TranDate )) As DayOfWk
FROM OurDB
GROUP BY Source, Event, DateName(WEEKDAY,( TranDate )), Qty

But it isn't grouping by Source (which only has two possible values). If I take out the DateName function, then everything groups correctly. Basically I'm looking to get:

EDIT: Added row data

OurDB:

Desired Output:

Upvotes: 0

Views: 311

Answers (1)

Vamsi Prabhala
Vamsi Prabhala

Reputation: 49270

SELECT Source , Event, max(Qty) --or min of qty
,DateName(WEEKDAY,( TranDate )) As DayOfWk
FROM OurDB
GROUP BY Source, Event, DateName(WEEKDAY,( TranDate ))

Your query didnot have a aggregate function. From the expected result, it looks like you have to use a aggregate function on Qty column.

Upvotes: 1

Related Questions