3

我正在尝试对我刚刚在查询中创建的两个 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

4 回答 4

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
于 2013-09-13T17:01:50.643 回答
3

一种方法是简单地将现有查询变成派生表

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 或更高版本,这也可以与公用表表达式 (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;

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

于 2013-09-13T17:03:57.837 回答
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
于 2013-09-13T17:05:18.127 回答
-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
于 2020-01-09T14:49:46.353 回答