2
我有這樣的查詢:在union - group中共享行?
SELECT * FROM (
SELECT id, SUM(orderLineTotal) as orderLine1, NULL as orderLine 2
FROM Orders
WHERE Orders.Date < @Today
GROUP BY id
UNION
SELECT id, NULL as orderLine1, SUM(orderLineTotal2) as orderLine 2
FROM Orders
WHERE Orders.Date = @Today
GROUP BY id
) o
GROUP BY o.id, o.orderLine1, o.orderLine2
我得到類似下面的結果:
ID OrderLine1 OrderLine2
-----------------------------------------
1 105.00 NULL
1 NULL 204.00
2 49.30 NULL
2 NULL 94.24
有什麼辦法來修改查詢返回這樣的事情?
ID OrderLine1 OrderLine2
-----------------------------------------
1 105.00 204.00
2 49.30 94.24
最快的方法是利用你的aggegation並將選擇邏輯改爲SELECT ID,Max(OrderLine1),Max(OrderLine2)',但@GordonLinoff答案更好。 – 2013-05-09 18:06:32