Can you put a "Group By" clause in a Case statement?
I have a query that needs both. I can put 2 statements in a stored procedure and call the stored procedure if necessary (one with a group by and one without).
For example:
Would like to do something like:
I could do this using dynamic SQL but would rather not.
Thanks,
Tom
I have a query that needs both. I can put 2 statements in a stored procedure and call the stored procedure if necessary (one with a group by and one without).
For example:
Code:
IF @Grouped = 1
BEGIN
SELECT Name, SUM(Quantity)
FROM TableA
Group by Name
END
ELSE
BEGIN
SELECT Name, Quantity
FROM TableA
END
Would like to do something like:
Code:
SELECT NAME, CASE WHEN @Grouped = 1 THEN SUM(Quantity) ELSE Quantity END
FROM TableA
CASE WHEN @Grouped = 1 THEN Group BY Name END
I could do this using dynamic SQL but would rather not.
Thanks,
Tom