我有两个查询,当选择一个特定的单词时,我使用表达式来计数。这两个查询均按预期方式工作,但我想使用联合查询将其合并。联合有效,但是当两个查询中的代表姓名都出现时,它会两次列出该姓名。
运行联合会时,我收到以下信息
Rep OppCount DevCount BiddingCount
Darrell 0 0 0
Josh 40 7 156
Kyle 0 0 15
Leah 26 4 55
Mark 59 8 159
Vic 25 2 191
Darrell 0 94 0
Josh 0 51 0
Kyle 0 62 0
Leah 0 101 0
Mark 0 87 1
Vic 0 8 0
SELECT Opp_New.Rep AS Rep, "" AS FaceToFaceCount, "" AS PhoneCount, "" AS EmailCount, Sum(IIf([Opp_New]![Status]="Opportunity",1,0)) AS OppCount, Sum(IIf([Opp_New]![Status]="Developing",1,0)) AS DevCount, Sum(IIf([Opp_New]![Status]="Bidding",1,0)) AS BiddingCount, Sum(IIf([Opp_New]![Status]="At MFG for Quote",1,0)) AS AtMFGCount, Sum(IIf([Opp_New]![Status]="Did Not Bid",1,0)) AS DNBCount, Sum(IIf([Opp_New]![Status]="Bid/Quote",1,0)) AS QuotedCount, Sum(IIf([Opp_New]![Status]="Won",1,0)) AS WonCount, Sum(IIf([Opp_New]![Status]="Lost",1,0)) AS LostCount
FROM Opp_New
GROUP BY Rep
UNION ALL
SELECT Calls_New.Rep AS Rep, Sum(IIf([Calls_New]![ContactType]="Face To Face",1,0)) AS FaceToFaceCount, Sum(IIf([Calls_New]![ContactType]="Phone",1,0)) AS PhoneCount, Sum(IIf([Calls_New]![ContactType]="Email",1,0)) AS EmailCount, Sum(IIf([Calls_New]![ProjectStatus]="Opportunity",1,0)) AS OppCount, Sum(IIf([Calls_New]![ProjectStatus]="Developing",1,0)) AS DevCount, Sum(IIf([Calls_New]![ProjectStatus]="Bidding",1,0)) AS BiddingCount, Sum(IIf([Calls_New]![ProjectStatus]="At MFG for Quote",1,0)) AS AtMFGCount, Sum(IIf([Calls_New]![ProjectStatus]="Did Not Bid",1,0)) AS DNBCount, Sum(IIf([Calls_New]![ProjectStatus]="Bid/Quote",1,0)) AS QuotedCount, Sum(IIf([Calls_New]![ProjectStatus]="Won",1,0)) AS WonCount, Sum(IIf([Calls_New]![ProjectStatus]="Lost",1,0)) AS LostCount
FROM Calls_New
GROUP BY Rep
我想像下面这样
Rep OppCount DevCount BiddingCount
Darrell 0 94 0
Josh 40 58 156
Kyle 0 62 15
Leah 26 105 55
Mark 59 95 160
Vic 25 10 191
答案 0 :(得分:2)
在我看来,您根本不需要使用UNION
,而是要在INNER JOIN
上使用rep
。
SELECT t1.Rep, t1.OppCount + t2.OppCount As OppCount, t1.BiddingCount + t2.BiddingCount As BiddingCount
FROM (
SELECT Opp_New.Rep AS Rep, "" AS FaceToFaceCount, "" AS PhoneCount, "" AS EmailCount, Sum(IIf([Opp_New]![Status]="Opportunity",1,0)) AS OppCount, Sum(IIf([Opp_New]![Status]="Developing",1,0)) AS DevCount, Sum(IIf([Opp_New]![Status]="Bidding",1,0)) AS BiddingCount, Sum(IIf([Opp_New]![Status]="At MFG for Quote",1,0)) AS AtMFGCount, Sum(IIf([Opp_New]![Status]="Did Not Bid",1,0)) AS DNBCount, Sum(IIf([Opp_New]![Status]="Bid/Quote",1,0)) AS QuotedCount, Sum(IIf([Opp_New]![Status]="Won",1,0)) AS WonCount, Sum(IIf([Opp_New]![Status]="Lost",1,0)) AS LostCount
FROM Opp_New
GROUP BY Rep
) t1
INNER JOIN (
SELECT Calls_New.Rep AS Rep, Sum(IIf([Calls_New]![ContactType]="Face To Face",1,0)) AS FaceToFaceCount, Sum(IIf([Calls_New]![ContactType]="Phone",1,0)) AS PhoneCount, Sum(IIf([Calls_New]![ContactType]="Email",1,0)) AS EmailCount, Sum(IIf([Calls_New]![ProjectStatus]="Opportunity",1,0)) AS OppCount, Sum(IIf([Calls_New]![ProjectStatus]="Developing",1,0)) AS DevCount, Sum(IIf([Calls_New]![ProjectStatus]="Bidding",1,0)) AS BiddingCount, Sum(IIf([Calls_New]![ProjectStatus]="At MFG for Quote",1,0)) AS AtMFGCount, Sum(IIf([Calls_New]![ProjectStatus]="Did Not Bid",1,0)) AS DNBCount, Sum(IIf([Calls_New]![ProjectStatus]="Bid/Quote",1,0)) AS QuotedCount, Sum(IIf([Calls_New]![ProjectStatus]="Won",1,0)) AS WonCount, Sum(IIf([Calls_New]![ProjectStatus]="Lost",1,0)) AS LostCount
FROM Calls_New
GROUP BY Rep
) t2 ON t1.Rep = t2.Rep
当然,在使用INNER JOIN
时,您可以报废stuf,例如第一个查询中的空FaceToFaceCount,PhoneCount和EmailCount,而不必添加它们。由于您从示例输出中排除了它们,因此我没有将它们包括在外部查询中,但是应该很容易进行调整。
答案 1 :(得分:0)
只需通过sum()
函数汇总结果列
SELECT Rep,
sum(OppCount) as OppCount,
sum(DevCount) as DevCount,
sum(BiddingCount) as BiddingCount
FROM
(
SELECT Opp_New.Rep AS Rep, "" AS FaceToFaceCount, "" AS PhoneCount, "" AS EmailCount, Sum(IIf([Opp_New]![Status]="Opportunity",1,0)) AS OppCount, Sum(IIf([Opp_New]![Status]="Developing",1,0)) AS DevCount, Sum(IIf([Opp_New]![Status]="Bidding",1,0)) AS BiddingCount, Sum(IIf([Opp_New]![Status]="At MFG for Quote",1,0)) AS AtMFGCount, Sum(IIf([Opp_New]![Status]="Did Not Bid",1,0)) AS DNBCount, Sum(IIf([Opp_New]![Status]="Bid/Quote",1,0)) AS QuotedCount, Sum(IIf([Opp_New]![Status]="Won",1,0)) AS WonCount, Sum(IIf([Opp_New]![Status]="Lost",1,0)) AS LostCount
FROM Opp_New
GROUP BY Rep
UNION ALL
SELECT Calls_New.Rep AS Rep, Sum(IIf([Calls_New]![ContactType]="Face To Face",1,0)) AS FaceToFaceCount, Sum(IIf([Calls_New]![ContactType]="Phone",1,0)) AS PhoneCount, Sum(IIf([Calls_New]![ContactType]="Email",1,0)) AS EmailCount, Sum(IIf([Calls_New]![ProjectStatus]="Opportunity",1,0)) AS OppCount, Sum(IIf([Calls_New]![ProjectStatus]="Developing",1,0)) AS DevCount, Sum(IIf([Calls_New]![ProjectStatus]="Bidding",1,0)) AS BiddingCount, Sum(IIf([Calls_New]![ProjectStatus]="At MFG for Quote",1,0)) AS AtMFGCount, Sum(IIf([Calls_New]![ProjectStatus]="Did Not Bid",1,0)) AS DNBCount, Sum(IIf([Calls_New]![ProjectStatus]="Bid/Quote",1,0)) AS QuotedCount, Sum(IIf([Calls_New]![ProjectStatus]="Won",1,0)) AS WonCount, Sum(IIf([Calls_New]![ProjectStatus]="Lost",1,0)) AS LostCount
FROM Calls_New
GROUP BY Rep
) q
GROUP BY Rep