如何按以下别名列分组

时间:2017-05-12 12:49:49

标签: sql sql-server database group-by

如何按以下查询对别名列Test_Quarter进行分组:

SELECT  a.testID, 
        a.projectYear ,
        CASE a.ProjectPeriod
            WHEN 1 THEN 1
            WHEN 2 THEN 1
            WHEN 3 THEN 1
            WHEN 4 THEN 2
            WHEN 5 THEN 2
            WHEN 6 THEN 2
            WHEN 7 THEN 3
            WHEN 8 THEN 3
            WHEN 9 THEN 3
            WHEN 10 THEN 4
            WHEN 11 THEN 4
            WHEN 12 THEN 4 
        END AS Test_Quarter,
        SUM(a.cost)
FROM    test a
GROUP   BY a.testID, a.projectYear, Test_Quarter

谢谢,

1 个答案:

答案 0 :(得分:2)

按条款

分组案例陈述
SELECT  a.testID, 
        a.projectYear ,
        CASE a.ProjectPeriod
            WHEN 1 THEN 1
            WHEN 2 THEN 1
            WHEN 3 THEN 1
            WHEN 4 THEN 2
            WHEN 5 THEN 2
            WHEN 6 THEN 2
            WHEN 7 THEN 3
            WHEN 8 THEN 3
            WHEN 9 THEN 3
            WHEN 10 THEN 4
            WHEN 11 THEN 4
            WHEN 12 THEN 4 
        END AS Test_Quarter,
        SUM(a.cost)
FROM    test a
GROUP   BY a.testID, a.projectYear, CASE a.ProjectPeriod
            WHEN 1 THEN 1
            WHEN 2 THEN 1
            WHEN 3 THEN 1
            WHEN 4 THEN 2
            WHEN 5 THEN 2
            WHEN 6 THEN 2
            WHEN 7 THEN 3
            WHEN 8 THEN 3
            WHEN 9 THEN 3
            WHEN 10 THEN 4
            WHEN 11 THEN 4
            WHEN 12 THEN 4 
        END