-1

我有一个 SQL 语句,它有多个 case 语句,如下所示:

SUM(CASE [Type]
    WHEN 418
    THEN CASE WHEN PMTS.DateCreated BETWEEN @sd AND @ed
    AND( PMTS.Amount >0 OR PMTS.Mileage >0)
    AND PMTS.PaymentEventID = 0 THEN Mileage END
    ELSE 0 END) AS DriverMileage

但是我需要总和的结果来设置一个变量并在 WHERE 子句的 where 中使用它。

下面的例子

DECLARE @contribution decimal
SElECT PMTS.VolunteerId ,
SET @contribution = SUM(CASE [Type]
              WHEN 1003
              THEN CASE WHEN PMTS.DateCreated BETWEEN @sd AND @ed
      AND( PMTS.Amount >0 OR PMTS.Mileage >0)
              AND PMTS.PaymentEventID = 0
              THEN Amount END
              ELSE 0 END) AS GPContributions
FROM PMTS WHERE @contribution >0

非常感谢

4

1 回答 1

0

我认为您在这里不需要变量,还请确认使用的“case when”语法。这是一个示例查询:

select * from 
(SElECT PMTS.VolunteerId ,SUM(CASE [Type]
              WHEN 1003
              THEN CASE WHEN PMTS.DateCreated BETWEEN @sd AND @ed
      AND( PMTS.Amount >0 OR PMTS.Mileage >0)
              AND PMTS.PaymentEventID = 0
              THEN Amount END
              ELSE 0 END) AS GPContributions
FROM PMTS)as A WHERE GPContributions >0
于 2013-08-13T20:18:35.077 回答