views:

33

answers:

1

The following query gives the error "#1241 - Operand should contain 1 column(s)" because of the (Department_Code, Course_Code) line. When I replace that with just (Course_Code) it works. However, that's 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 Department_Code, Course_Code when condition (2) is satisfied?

+1  A: 

A case expression can only return a single value, so you need two case expressions. Also, use a single case expression for each instead of nesting 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
Guffa