我正在创建一个以230美元的基本成本开始的查询,然后将参加每个人注册到课堂会话的父母数量乘以55美元(每个父母的成本)参加会议)。
然后,它将检查参加者迄今已支付的金额,并将从刚刚计算的总费用中减去该金额。
我的问题是我还需要在每一行中包含电子邮件,因此我需要查询GROUP BY GuestEmail
部分。使用此GROUP BY
行会导致COUNT(Guest.ID)
始终返回1,即使在多种情况下也应返回2,3或4。
SELECT Registration.ID, Guest.GuestEmail,
CASE
WHEN ((COUNT(Guest.ID) * 55) + 230) - SUM(DISTINCT Transaction.AuthorizationAmount) is not null
THEN ((COUNT(Guest.ID) * 55) + 230) - SUM(DISTINCT Transaction.AuthorizationAmount)
WHEN ((COUNT(Guest.ID) * 55) + 230) is not null AND SUM(DISTINCT Transaction.AuthorizationAmount) is null
THEN ((COUNT(Guest.ID) * 55) + 230)
WHEN ((COUNT(Guest.ID) * 55) + 230) is null AND SUM(DISTINCT Transaction.AuthorizationAmount) is not null
THEN - SUM(DISTINCT Transaction.AuthorizationAmount)
ELSE '0'
END AS Balance
FROM Registration
LEFT JOIN Transaction ON Registration.ID = Transaction.ID
LEFT JOIN Guest ON Registration.ID = Guest.ID
WHERE Registration.IsActive = 'True' AND Registration.ID = 111111 AND ConfirmationEmail = 0 AND Guest.IsActive = 'True' AND Registration.ID = Guest.ID
GROUP BY Registration.ID, Guest.GuestEmail
注册是保存学生信息的表,而访客持有父母的信息。通过注册和访客中包含的ID将每个父母标识为其子女的父母。 ID与父母和学生的编号相同。
Transaction.AuthorizationAmount是已注册人已支付的金额。每位客人都有一个单独的行。返回的电子邮件是父母的电子邮件,该电子邮件存储在Guest表中。
问题:如何更改查询以使COUNT(Guest.ID)
返回1以外的数字?
答案 0 :(得分:1)
只是盲目地尝试这个,并且不确定这个查询将如何执行,但它可能会给你一些想法:
SELECT r.ID, r.balance, g.GuestEmail
FROM Guest g
JOIN (
SELECT Registration.ID,
CASE
WHEN ((COUNT(Guest.ID) * 55) + 230) - SUM(DISTINCT Transaction.AuthorizationAmount) is not null
THEN ((COUNT(Guest.ID) * 55) + 230) - SUM(DISTINCT Transaction.AuthorizationAmount)
WHEN ((COUNT(Guest.ID) * 55) + 230) is not null AND SUM(DISTINCT Transaction.AuthorizationAmount) is null
THEN ((COUNT(Guest.ID) * 55) + 230)
WHEN ((COUNT(Guest.ID) * 55) + 230) is null AND SUM(DISTINCT Transaction.AuthorizationAmount) is not null
THEN - SUM(DISTINCT Transaction.AuthorizationAmount)
ELSE '0'
END AS Balance
FROM Registration
LEFT JOIN Transaction ON Registration.ID = Transaction.ID
LEFT JOIN Guest ON Registration.ID = Guest.ID
WHERE Registration.IsActive = 'True' AND Registration.ID = 111111 AND ConfirmationEmail = 0 AND Guest.IsActive = 'True' AND Registration.ID = Guest.ID
GROUP BY Registration.ID
) r ON (g.ID = r.ID)
GROUP BY g.GuestEmail