Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL: GROUP BY multiple columns with CASE statement

I'm new to SQL.

I'd like to use GROUP BY with a CASE statement, to group results in a particular way if @myboolean is true.

I've seen many examples of how to use GROUP BY and CASE BY with a single field, or how to use GROUP BY with multiple fields without a CASE statement.

I don't know how to combine the two. When I enclose the GROUP BY fields within the CASE statement, I get a syntax error:

Incorrect syntax near ','

So, this works:

GROUP BY 
/* This works with no enclosing CASE statement */
field1,
field2,
field3,
field4

This produces a syntax error:

GROUP BY 
CASE WHEN (@myboolean=1)
  THEN
    field1,   <-- ERROR HERE:  Incorrect syntax near ','
    field2,
    field3,
    field4
  ELSE 
    field1
  END

I have already looked at these questions:

  • SQL: Group By with Case Statement for multiple fields: seems to be a GROUP BY either/or based on CASE, rather than group by multiple. No commas involved anyway.

  • Group by Multiple columns and case statement: maybe I'm thick, but I can't see how this includes a case statement in the GROUP BY clause

  • various others, less relevant

I'm using Microsoft SQL Server Management Studio.

Please note that I'm inheriting a very complex/long SQL statement, which I want to avoid altering too much. I don't want to split the query into two separate SELECT statements.

like image 225
Lydia Ralph Avatar asked Aug 10 '15 10:08

Lydia Ralph


People also ask

Can you put a case statement in a GROUP BY clause?

The case statement in SQL returns a value on a specified condition. We can use a Case statement in select queries along with Where, Order By, and Group By clause. It can be used in the Insert statement as well.

Can you GROUP BY multiple columns in SQL?

We can use the group by multiple column technique to group multiple records into a single record. All the records that have the same values for the respective columns mentioned in the grouping criteria can be grouped as a single column using the group by multiple column technique.

Can you GROUP BY multiple columns at once?

We can group the resultset in SQL on multiple column values. When we define the grouping criteria on more than one column, all the records having the same value for the columns defined in the group by clause are collectively represented using a single record in the query output.


2 Answers

You could use...

GROUP BY field1,
    CASE WHEN @myboolean=1 THEN field2 END,
    CASE WHEN @myboolean=1 THEN field3 END,
    CASE WHEN @myboolean=1 THEN field4 END

If @myboolean is not 1 that evaluates to NULL which doesn't affect the result.

If it's in a stored-procedure you can also use an IF.

like image 89
Tim Schmelter Avatar answered Nov 02 '22 05:11

Tim Schmelter


You can try this way:

IF (@myboolean=1)
BEGIN
SELECT
field1,
field2,
field3,
field4
FROM myTable
GROUP BY
field1,
field2,
field3,
field4

END
ELSE
BEGIN
SELECT
field1
FROM myTable
GROUP BY
field1
END
like image 45
mxix Avatar answered Nov 02 '22 05:11

mxix