Problem with GROUP BY CASE

The following query gives the error "# 1241 - The operand must contain 1 column" because of the row (Department_Code, Course_Code). When I replace it simply (Course_Code), it works. However, this is not what I want.

SELECT * FROM Classes
GROUP BY CASE 
WHEN (1) THEN
 Department_Code
 ELSE CASE WHEN (2) THEN 
  (Department_Code, Course_Code)
 ELSE Class_ID
 END
END

How can I group by the Department_Code, Course_Code method when condition (2) is satisfied?

+3
source share
1 answer

A caseexpression can only return one value, so you need two expressions case. Also, use a single expression casefor each, not a nested two inside each other:

SELECT * FROM Classes
GROUP BY
  CASE 
  WHEN (1) THEN
    Department_Code
  WHEN (2) THEN 
    Department_Code
  ELSE
    Class_ID
  END,
  CASE 
  WHEN (2) THEN 
    Course_Code
  ELSE
    1
  END
+5
source

Source: https://habr.com/ru/post/1759831/


All Articles