sql server 2008按会计年度排序

时间:2016-06-01 15:03:46

标签: sql sql-server sql-server-2008

我希望在SQl服务器上使用以下sql按财政年度订购,但我收到错误消息'无效的列名称' MONTH''。我想知道你是否可以帮我修改下面的sql。谢谢!

SELECT 'Closed Home' AS STATUS,
    right(CONVERT(VARCHAR(8), Closed_Dt, 3),5) as "MONTH",
    COUNT(Home_ID) COUNT
FROM Home
GROUP BY HOME_STATUS,right(CONVERT(VARCHAR(8), Closed_Dt, 3),5)
HAVING Facility_STATUS IN ('CLOSED')

UNION

SELECT 'New Placement' AS STATUS,
right(CONVERT(VARCHAR(8), EFFECT_DT, 3),5) AS "MONTH",
COUNT(CLT_NBR) AS COUNT 
FROM STATUS S
GROUP BY right(CONVERT(VARCHAR(8), EFFECT_DT, 3),5)     
ORDER BY status,
  case "MONTH" when '07/15' then 1 
               when '08/15' then 2
               when '09/15' then 3
               when '10/15' then 4
               when '11/15' then 5
               when '12/15' then 6
               when '01/16' then 7
               when '02/16' then 8
               when '03/16' then 9
               when '04/16' then 10
               when '05/16' then 11
               when '06/16' then 12
    end

3 个答案:

答案 0 :(得分:2)

您可以将主查询移动到派生表中,然后使用MONTH列..

SELECT * FROM (
    SELECT  'Closed Home' AS STATUS,
            RIGHT(CONVERT(VARCHAR(8),Closed_Dt,3),5) AS "MONTH",
            COUNT(Home_ID) COUNT
    FROM    Home
    GROUP BY HOME_STATUS,
            RIGHT(CONVERT(VARCHAR(8),Closed_Dt,3),5)
    HAVING  Facility_STATUS IN ('CLOSED')
    UNION
    SELECT  'New Placement' AS STATUS,
            RIGHT(CONVERT(VARCHAR(8),EFFECT_DT,3),5) AS "MONTH",
            COUNT(CLT_NBR) AS COUNT
    FROM    STATUS S
    GROUP BY RIGHT(CONVERT(VARCHAR(8),EFFECT_DT,3),5)
) t
ORDER BY status,
        CASE "MONTH"
          WHEN '07/15' THEN 1
          WHEN '08/15' THEN 2
          WHEN '09/15' THEN 3
          WHEN '10/15' THEN 4
          WHEN '11/15' THEN 5
          WHEN '12/15' THEN 6
          WHEN '01/16' THEN 7
          WHEN '02/16' THEN 8
          WHEN '03/16' THEN 9
          WHEN '04/16' THEN 10
          WHEN '05/16' THEN 11
          WHEN '06/16' THEN 12
        END

答案 1 :(得分:0)

    SELECT 'Closed Home' AS STATUS,
        right(CONVERT(VARCHAR(8), Closed_Dt, 3),5) as "MONTH",
        COUNT(Home_ID) COUNT
    FROM Home
    GROUP BY HOME_STATUS,right(CONVERT(VARCHAR(8), Closed_Dt, 3),5)
    HAVING Facility_STATUS IN ('CLOSED')

    UNION

    SELECT 'New Placement' AS STATUS,
    right(CONVERT(VARCHAR(8), EFFECT_DT, 3),5) AS "MONTH",
    COUNT(CLT_NBR) AS COUNT 
    FROM STATUS S
    GROUP BY right(CONVERT(VARCHAR(8), EFFECT_DT, 3),5)     
    ORDER BY status,
      case right(CONVERT(VARCHAR(8), EFFECT_DT, 3),5)  when '07/15' then 1 
                   when '08/15' then 2
                   when '09/15' then 3
                   when '10/15' then 4
                   when '11/15' then 5
                   when '12/15' then 6
                   when '01/16' then 7
                   when '02/16' then 8
                   when '03/16' then 9
                   when '04/16' then 10
                   when '05/16' then 11
                   when '06/16' then 12
        end

答案 2 :(得分:0)

您收到错误是因为您无法在CASE表达式中引用别名,即使它在ORDER BY子句中也是如此。

这应该有效:

...
case right(CONVERT(VARCHAR(8), Closed_Dt, 3),5)
...