1

我在按 [评分] 列分组时遇到问题。当我执行它时,它给出“无效的列名'rate'”。

谁能帮我 ?谢谢!!

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
4

3 回答 3

2

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

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 
于 2013-05-16T21:56:44.520 回答
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,但很少。

于 2013-05-17T02:30:07.457 回答
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
于 2013-05-16T21:49:06.687 回答