2010-08-16 110 views
3

由於(Department_Code,Course_Code)行,以下查詢給出錯誤「#1241 - 操作數應包含1列」。當我用(Course_Code)代替它時,它可以工作。然而,這不是我想要的GROUP BY CASE的問題

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 

我怎麼能按Department_Code,當條件(2)滿意是Course_Code?

回答

5

A case表達式只能返回單個值,所以您需要兩個case表達式。此外,使用單個case表達式代替彼此嵌套兩個:

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