SQL Server ---如何按SELECT语句中CASE语句中声明的列进行分组

时间:2013-05-16 21:45:25

标签: sql sql-server case

我在列[评级]上遇到问题。当我执行它时,它会给出“无效的列名称'率'”。

任何人都可以帮助我吗?谢谢!

SELECT
     ......
   CASE
         WHEN level_1 = 'Corporate'
         THEN 
            CASE
            WHEN ssr.rating = 'A' OR ssr.rating LIKE 'A[+-]'
            THEN 'Corp A'

            WHEN ssr.rating = 'AA' OR ssr.rating LIKE 'AA[+-]'
            THEN 'Corp AA'

            WHEN ssr.rating = 'BBB' OR ssr.rating LIKE 'BBB[+-]'
            THEN 'Corp BBB'

            ELSE NULL
        END

            WHEN level_1 = 'Government' AND level_2 = 'Provincial'
            THEN 'Prov'

            WHEN level_1 = 'Government' AND level_2 = 'Federal'
            THEN 'Canada'

       ELSE NULL
   END AS rating,
     ......
  SUM(...)
     .......
FROM....
GROUP BY rating

3 个答案:

答案 0 :(得分:2)

你可以使用子查询或者像理查德提到的那样重复它,或者使用Common-Table-Expression,我认为它更具可读性:

WITH cte 
     AS (SELECT CASE 
                  WHEN level_1 = 'Corporate' THEN 
                    CASE 
                      WHEN ssr.rating = 'A' 
                            OR ssr.rating LIKE 'A[+-]' THEN 'Corp A' 
                      WHEN ssr.rating = 'AA' 
                            OR ssr.rating LIKE 'AA[+-]' THEN 'Corp AA' 
                      WHEN ssr.rating = 'BBB' 
                            OR ssr.rating LIKE 'BBB[+-]' THEN 'Corp BBB' 
                      ELSE NULL 
                    END 
                  WHEN level_1 = 'Government' 
                       AND level_2 = 'Provincial' THEN 'Prov' 
                  WHEN level_1 = 'Government' 
                       AND level_2 = 'Federal' THEN 'Canada' 
                  ELSE NULL 
                END AS rating -- other columns ...
         FROM   dbo.tablename) 
SELECT cte.*, 
       Sum(col)AS ColSum 
FROM   cte 
GROUP  BY rating 

答案 1 :(得分:0)

将其设为派生表(也称为子查询)或重复整个表达式(不要担心SQL Server会计算一次并多次使用它)。

在您的情况下,重复CASE表达式看起来更容易。

SELECT
     ......
   CASE
         WHEN level_1 = 'Corporate'
         THEN 
            CASE
            WHEN ssr.rating = 'A' OR ssr.rating LIKE 'A[+-]'
            THEN 'Corp A'

            WHEN ssr.rating = 'AA' OR ssr.rating LIKE 'AA[+-]'
            THEN 'Corp AA'

            WHEN ssr.rating = 'BBB' OR ssr.rating LIKE 'BBB[+-]'
            THEN 'Corp BBB'

            ELSE NULL
        END

            WHEN level_1 = 'Government' AND level_2 = 'Provincial'
            THEN 'Prov'

            WHEN level_1 = 'Government' AND level_2 = 'Federal'
            THEN 'Canada'

       ELSE NULL
   END AS rating,
     ......
  SUM(...)
     .......
FROM....
GROUP BY CASE
         WHEN level_1 = 'Corporate'
         THEN 
            CASE
            WHEN ssr.rating = 'A' OR ssr.rating LIKE 'A[+-]'
            THEN 'Corp A'

            WHEN ssr.rating = 'AA' OR ssr.rating LIKE 'AA[+-]'
            THEN 'Corp AA'

            WHEN ssr.rating = 'BBB' OR ssr.rating LIKE 'BBB[+-]'
            THEN 'Corp BBB'

            ELSE NULL
        END

            WHEN level_1 = 'Government' AND level_2 = 'Provincial'
            THEN 'Prov'

            WHEN level_1 = 'Government' AND level_2 = 'Federal'
            THEN 'Canada'

       ELSE NULL
   END

答案 2 :(得分:0)

我发现最简单的方法就是使用子查询:

select rating, .  .
from (SELECT
     ......
   CASE
         WHEN level_1 = 'Corporate'
         THEN 
            CASE
            WHEN ssr.rating = 'A' OR ssr.rating LIKE 'A[+-]'
            THEN 'Corp A'

            WHEN ssr.rating = 'AA' OR ssr.rating LIKE 'AA[+-]'
            THEN 'Corp AA'

            WHEN ssr.rating = 'BBB' OR ssr.rating LIKE 'BBB[+-]'
            THEN 'Corp BBB'

            ELSE NULL
        END

            WHEN level_1 = 'Government' AND level_2 = 'Provincial'
            THEN 'Prov'

            WHEN level_1 = 'Government' AND level_2 = 'Federal'
            THEN 'Canada'

       ELSE NULL
   END AS rating,
   *
 from . . .
) t
group by rating;

这适用于所有数据库,包括MySQL和Access。缺点是某些数据库将子查询视为派生表,实际上保存了中间结果。没有考虑错误的SQL引擎实现。在这种情况下,如果要避免这种开销,则需要在group by语句中重复这种情况。并且,确实存在可以接受rating中的group by的数据库,但只有极少数。