0

我正在创建一个查询,该查询的基本费用为 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

注册是保存学生信息的表,而 Guest 保存家长信息。每个家长都通过注册和访客中包含的 ID 被识别为他们孩子的父母。家长和学生的 ID 号码相同。Transaction.AuthorizationAmount 是注册人已经支付的金额。每位客人都有单独的一行。返回的电子邮件是存储在 Guest 表中的父母的电子邮件。问题:如何更改查询以导致COUNT(Guest.ID)返回 1 以外的数字?

4

1 回答 1

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
于 2013-06-25T23:22:59.713 回答