我尝试进行SQL
查询以获取客户的姓名以及c
列,其中Count
是Customers.[Customer ID] = Referrals.[Referred From]
许多行通过Referrals.[Used Our Service] = True
和c2
包含Count
的第二列,其中Customers.[Customer ID] = Referrals.[Referred From]
表示连接到Referrals.[Valid Referral] = True
和c2
此查询正常但在SELECT
Customers.[Customer ID], Customers.[First Name], Customers.[Last Name], Count(r.[Valid Referral]) AS c, Count(rr.[Used Our Service]) AS c2
FROM (Customers
INNER JOIN Referrals r ON Customers.[Customer ID] = r.[Referred From] AND (r.[Valid Referral])=True)
INNER JOIN Referrals rr ON Customers.[Customer ID] = rr.[Referred From] AND (rr.[Used Our Service])=True
WHERE (r.[Valid Referral])=True
GROUP BY Customers.[Customer ID], Customers.[First Name], Customers.[Last Name]
列中返回错误的数字。它应为1时返回2.
{{1}}
客户表:
推荐表:
结果表:
答案 0 :(得分:1)
我建议您不要将同一张桌子加入两次,而是将SUM
与Iif
一起使用,如下所示:
SELECT
Customers.[Customer ID],
Customers.[First Name],
Customers.[Last Name],
SUM(Iif(r.[Valid Referral] = True, 1, 0)) AS c,
Sum(Iif(r.[Used Our Service] = True, 1, 0)) AS c2
FROM Customers
INNER JOIN Referrals r ON Customers.[Customer ID] = r.[Referred From]
WHERE (r.[Valid Referral])=True
GROUP BY Customers.[Customer ID], Customers.[First Name], Customers.[Last Name]