我有一个 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
非常感谢