0
我有查詢:聚合函數不給總和
SELECT o.idorder,
sum(CASE
WHEN cast(isnull(ama.FreeShiping, 0) AS INT) = 0
THEN - 1000
ELSE cast(isnull(ama.FreeShiping, 0) AS INT)
END) FreeShiping
FROM OrderDetail od,
amazonsku ama,
orders o,
TempPrintRecords tmp
WHERE o.idOrder = od.idOrder
AND o.idCustomer = ama.idCustomer
AND od.SKU = ama.Sku
--and od.idOrder=350184
AND od.idOrder = tmp.idOrder
AND ama.FreeShiping != 0
GROUP BY o.idorder
結果:
在這裏,我在ama.FreeShiping
有0和空基本上這列位,只包含0和1.因此,如果發現0,我將它設爲-1000以確定idorder
具有0或空作爲Freeshipping
但它沒有給出適當的總和,它應該給每列的-ve結果。
如果你刪除了SUM()和GROUP BY,我懷疑你會發現'idorder' 350184和350185的多行,這兩行的'ama.FreeShipping'都是'1'。你總結了這些多行,並在這裏以'2'結尾。 – JNevill 2015-04-01 15:04:25
也許我錯過了一些東西,但是如果ama.freeshipping!= 0意味着沒有零,所以case語句是毫無價值的,因爲你永遠不會有零。 – xQbert 2015-04-01 15:05:10