我迷失在这一个。看似简单,但无法得到答案。我有一种感觉,我可能会对答案感到尴尬。好吧,好吧。我试图在WebMatrix 3.0中这样做,我相信它的SQL Express。不确定。
Table Dlivry
tID cID Boxes Delivered
1 01 5 1
2 03 7 1
3 01 2 0
4 01 3 1
5 03 5 1
6 05 4 1
7 05 10 0
我想要的是
Client Delivered NotDelivered
01 8 2
03 12 0
05 4 10
到目前为止我做了什么
SELECT D1.cid AS Client,
Sum(D1.boxes) AS Delivered,
Sum(D2.boxes) AS NotDelivered
FROM dlivr D1,
dlivry D2
WHERE D1.delivered = 1
AND D2.delivered = 0
AND D1.cid = D2.cid
GROUP BY cid
好的就是。准备好让我感到尴尬,我准备好学习。
感谢您的帮助。
答案 0 :(得分:3)
由于您正在使用内部联接,因此您只会获得包含已发送和未发送框的CID
行。所以你不会获得CID = 3
的任何行。试试这个:
SELECT CID,
SUM(CASE WHEN Delivered = 1 THEN boxes ELSE 0 END) AS Delivered,
SUM(CASE WHEN Delivered = 0 THEN boxes ELSE 0 END) AS NotDelivered
FROM Dlivr
GROUP BY CID