我有一个表“事件”和一个表“Slots”,其中Slots.SlotID = Events.MainSlot OR Events.ExtraSlot。
我需要将每个时段的“参加者”数量相加(因为MainSlot和ExtraSlot - ExtraSlot是可选的)
表“事件”
ID------Name----------MainSlot-------ExtraSlot-------Attendees
1-------Event1--------1 -------------n/a-------------20
2-------Event2--------1 -------------n/a-------------20
3-------Event3--------2 -------------n/a-------------40
4-------Event4--------2 -------------3---------------20
5-------Event5--------3 -------------4---------------40
6-------Event6--------3 -------------4---------------20
7-------Event7--------3 -------------4---------------10
表“老虎机”
SlotID--- Slot
1-------- 9.00-9.30
2-------- 9.30-10
3-------- 10.30-10.30
4-------- 10.30-11
如果我按如下方式单独查询数据库:
SELECT s.Slot, s.SlotID, ISNULL(SUM(e. Attendees), 0) AS Attendees1
FROM Slots AS s
LEFT OUTER JOIN Events AS e ON e.MainSlot = s.SlotID
GROUP BY s.Slot, s.SlotID
......或:
SELECT s.Slot, s.SlotID, ISNULL(SUM(x.Attendees), 0) AS Attendees2
FROM Slots AS s
LEFT OUTER JOIN Events AS x ON x.ExtraSlot = s.SlotID
GROUP BY s.Slot, s.SlotID
我分别得到以下内容:
SlotID ------ Attendees1
1------------- 40
2------------- 60
3------------- 70
4------------- 0
SlotID ------- Attendees2
1------------- 0
2------------- 0
3------------- 20 *correct
4------------- 70
两种结果都是正确的。
但是如果我把两个查询放在一起就会出现问题,如下表所示
SELECT s.Slot, s.SlotID, ISNULL(SUM(e.Attendees), 0) AS Attendees1,
ISNULL(SUM(x. Attendees), 0) AS Attendees2
FROM Slots AS s LEFT OUTER JOIN
Events AS e ON e.MainSlot = s.SlotID LEFT OUTER JOIN
Events AS x ON x.ExtraSlot = s.SlotID
GROUP BY s.Slot, s.SlotID
SlotID------------- Attendees1---------- Attendees2
1-------------------40-------------------0
2-------------------60-------------------0
3-------------------70------------------60 *wrong
4-------------------0-------------------70
我做错了什么?谢谢你的帮助!
答案 0 :(得分:3)
SELECT s.SlotId,
COALESCE(
(
SELECT SUM(attendees)
FROM events ea
WHERE ea.MainSlot = s.SlotId
), 0) AS AttendeesAsMain,
COALESCE(
(
SELECT SUM(attendees)
FROM events ea
WHERE ea.ExtraSlot = s.SlotId
), 0) AS AttendeesAsExtra
FROM Slots s
答案 1 :(得分:0)
我的猜测是,这与你在该组中都有记录这一事实有关。请尝试以下(MSSQL Server)
SELECT s.Slot, s.SlotID, ISNULL(SUM(e.Attendees), 0) AS Attendees1,
ISNULL(SUM(CASE WHEN x. Attendees IS NULL THEN 0 ELSE x.Attendees END), 0) AS Attendees2
FROM Slots AS s LEFT OUTER JOIN
Events AS e ON e.MainSlot = s.SlotID LEFT OUTER JOIN
Events AS x ON x.ExtraSlot = s.SlotID
GROUP BY s.Slot, s.SlotID
答案 2 :(得分:0)
SELECT a.SlotID, Attendees1, Attendees2 FROM
(SELECT s.Slot, s.SlotID, ISNULL(SUM(e. Attendees), 0) AS Attendees1
FROM Slots AS s
LEFT OUTER JOIN Events AS e ON e.MainSlot = s.SlotID
GROUP BY s.Slot, s.SlotID) as a,
(SELECT s.Slot, s.SlotID, ISNULL(SUM(x.Attendees), 0) AS Attendees2
FROM Slots AS s
LEFT OUTER JOIN Events AS x ON x.ExtraSlot = s.SlotID
GROUP BY s.Slot, s.SlotID) as b
WHERE a.SlotID = b.SlotID
答案 3 :(得分:0)
您加入了两次,因此您在返回时会得到重复的结果。基本上你得到这个:
slotID用于...... e.mainslot ...... x.extraslot ....... x.attendees ..... e.attendees
3 ........... 3 ............... 3 ................. 20 .............. 40个
3 ........... 3 ............... 3 ................. 20 ... ........... 20个
3 ........... 3 ............... 3 ................. 20 ... ........... 10
这是预期的行为,因为您将X连接到Slots JOIN E中的每个结果行。