如果您只想要一个LineNumber
值的结果,如代码所示,这很简单。只需删除GROUP BY LineNumber
. 没有 group by 的聚合意味着结果将恰好是一行,无论我们有 0 行还是一百万行,在聚合之前:
SELECT ISNULL(Sum(convert(dec,DeliveryPaymentExtras.ExtendedText)),0) AS ExtendedText
FROM DeliveryPaymentExtras
WHERE (LineNumber =21) ;
如果您想要多个值的结果(例如,如果您有:)WHERE LineNumber IN (21, 34, 55) GROUP BY LineNumber
,那么这并不简单。一种方法是:
SELECT v.LineNumber,
ISNULL(Sum(convert(dec,d.DeliveryPaymentExtras.ExtendedText)),0)
AS ExtendedText
FROM ( VALUES (21),(34),(55)
) AS v (LineNumber)
LEFT JOIN DeliveryPaymentExtras AS d
ON d.LineNumber = v.LineNumber
GROUP BY v.LineNumber ;