SUM两个CASE函数

时间:2013-09-13 16:55:55

标签: sql sql-server

我正在尝试SUM我在查询中创建的两个CASE函数。我需要在结果集中返回这两列,但还需要两列的总和作为'DegreeDays'在另一列中返回。我尝试了SUM功能,但无济于事,我尝试了一个简单的'HeatingDegreeDays + CoolingDegreeDays AS DegreeDays',但也没有用。建议?

  ,CASE
    WHEN TempLow > 60.5                     THEN 0
    WHEN ((TempHigh + TempLow)/2) > 60.5    THEN ((60.5-TempLow)/4)
    WHEN  TempHigh >= 60.5                  THEN (((60.5 - TempLow)/2)-((TempHigh-TempLow)/4))
    WHEN TempHigh < 60.5                    THEN (60.5-(TempHigh+TempLow)/4)
   END AS HeatingDegreeDays

  ,CASE
    WHEN TempHigh < 66.25                   THEN 0
    WHEN ((TempHigh + TempLow)/2) < 66.25   THEN ((TempHigh-66.25)/4)
    WHEN  TempLow <= 66.25                  THEN (((TempHigh - 66.25)/2)-((66.25-TempLow)/4))
    WHEN TempLow > 66.25                    THEN ((TempHigh+TempLow)/2)-66.25
   END AS CoolingDegreeDays

4 个答案:

答案 0 :(得分:4)

您可以重复以下公式:

  ,CASE
    WHEN TempLow > 60.5                     THEN 0
    WHEN ((TempHigh + TempLow)/2) > 60.5    THEN ((60.5-TempLow)/4)
    WHEN  TempHigh >= 60.5                  THEN (((60.5 - TempLow)/2)-((TempHigh-TempLow)/4))
    WHEN TempHigh < 60.5                    THEN (60.5-(TempHigh+TempLow)/4)
   END +
   CASE
    WHEN TempHigh < 66.25                   THEN 0
    WHEN ((TempHigh + TempLow)/2) < 66.25   THEN ((TempHigh-66.25)/4)
    WHEN  TempLow <= 66.25                  THEN (((TempHigh - 66.25)/2)-((66.25-TempLow)/4))
    WHEN TempLow > 66.25                    THEN ((TempHigh+TempLow)/2)-66.25
   END AS TotalDays

或者你可以将它放在子查询(或CTE)中并说:

select . . .,
       (HeadingDegreeDays + CoolingDegreeDays) as TotalDays

答案 1 :(得分:3)

一种方法是简单地将现有查询转换为derived table

SELECT a.HeatingDegreeDays,
    a.CoolingDegreeDays,
    a.HeatingDegreeDays + a.CoolingDegreeDays as DegreeDays
FROM
(
    SELECT
      CASE
        WHEN TempLow > 60.5                     THEN 0
        WHEN ((TempHigh + TempLow)/2) > 60.5    THEN ((60.5-TempLow)/4)
        WHEN  TempHigh >= 60.5                  THEN (((60.5 - TempLow)/2)-((TempHigh-TempLow)/4))
        WHEN TempHigh < 60.5                    THEN (60.5-(TempHigh+TempLow)/4)
       END AS HeatingDegreeDays

      ,CASE
        WHEN TempHigh < 66.25                   THEN 0
        WHEN ((TempHigh + TempLow)/2) < 66.25   THEN ((TempHigh-66.25)/4)
        WHEN  TempLow <= 66.25                  THEN (((TempHigh - 66.25)/2)-((66.25-TempLow)/4))
        WHEN TempLow > 66.25                    THEN ((TempHigh+TempLow)/2)-66.25
       END AS CoolingDegreeDays
    FROM MyTable
) AS a; -- Derived tabled aliased as "a"

如果您使用的是SQL Server 2005或更高版本,这也可以与Common Table Expression (CTE)一样有效:

;WITH cte AS (
    SELECT
      CASE
        WHEN TempLow > 60.5                     THEN 0
        WHEN ((TempHigh + TempLow)/2) > 60.5    THEN ((60.5-TempLow)/4)
        WHEN  TempHigh >= 60.5                  THEN (((60.5 - TempLow)/2)-((TempHigh-TempLow)/4))
        WHEN TempHigh < 60.5                    THEN (60.5-(TempHigh+TempLow)/4)
       END AS HeatingDegreeDays

      ,CASE
        WHEN TempHigh < 66.25                   THEN 0
        WHEN ((TempHigh + TempLow)/2) < 66.25   THEN ((TempHigh-66.25)/4)
        WHEN  TempLow <= 66.25                  THEN (((TempHigh - 66.25)/2)-((66.25-TempLow)/4))
        WHEN TempLow > 66.25                    THEN ((TempHigh+TempLow)/2)-66.25
       END AS CoolingDegreeDays
    FROM MyTable
)
SELECT HeatingDegreeDays,
    CoolingDegreeDays,
    HeatingDegreeDays + CoolingDegreeDays as DegreeDays
FROM cte;

这些中的任何一个似乎都比维护查询的逻辑和硬编码值只是为了进行计算更好。

答案 2 :(得分:0)

在查询的SELECT部分中派生列名时,您无法在其他部分引用它们。

您有两个基本选项:

将DegreeDays定义为两个SUM语句的CASE(重复所有逻辑)或执行以下操作:

select ...,HeatingDegreeDays,CoolingDegreeDays,DegreeDays=HeatingDegreeDays+CoolingDegreeDays
from table
cross apply (select
CASE
    WHEN TempLow > 60.5                     THEN 0
    WHEN ((TempHigh + TempLow)/2) > 60.5    THEN ((60.5-TempLow)/4)
    WHEN  TempHigh >= 60.5                  THEN (((60.5 - TempLow)/2)-((TempHigh-TempLow)/4))
    WHEN TempHigh < 60.5                    THEN (60.5-(TempHigh+TempLow)/4)
   END AS HeatingDegreeDays

  ,CASE
    WHEN TempHigh < 66.25                   THEN 0
    WHEN ((TempHigh + TempLow)/2) < 66.25   THEN ((TempHigh-66.25)/4)
    WHEN  TempLow <= 66.25                  THEN (((TempHigh - 66.25)/2)-((66.25-TempLow)/4))
    WHEN TempLow > 66.25                    THEN ((TempHigh+TempLow)/2)-66.25
   END AS CoolingDegreeDays
) x

答案 3 :(得分:-1)

,CASE
    WHEN TempLow > 60.5                     THEN 0
    WHEN ((TempHigh + TempLow)/2) > 60.5    THEN ((60.5-TempLow)/4)
    WHEN  TempHigh >= 60.5                  THEN (((60.5 - TempLow)/2)-((TempHigh-TempLow)/4))
    WHEN TempHigh < 60.5                    THEN (60.5-(TempHigh+TempLow)/4)
   END AS HeatingDegreeDays

  ,CASE
    WHEN TempHigh < 66.25                   THEN 0
    WHEN ((TempHigh + TempLow)/2) < 66.25   THEN ((TempHigh-66.25)/4)
    WHEN  TempLow <= 66.25                  THEN (((TempHigh - 66.25)/2)-((66.25-TempLow)/4))
    WHEN TempLow > 66.25                    THEN ((TempHigh+TempLow)/2)-66.25
   END AS CoolingDegreeDays