我有这两个表:
表1
| EventCode | StartDate | EndDate | Client |
| 13400 | 1/12/2014 | 5/12/2014 | ABC |
| 13400 | 6/12/2014 | 8/12/2014 | ABC |
| 15600 | 3/12/2014 | 5/12/2014 | ABC |
表2
| EventCode | Client | SignUpDate | Agent |
| 13400 | ABC | 2/12/2014 | AG001 |
| 13400 | ABC | 2/12/2014 | AG001 |
| 13400 | ABC | 3/12/2014 | AG002 |
| 15600 | ABC | 4/12/2014 | AG002 |
| 15600 | ABC | 4/12/2014 | AG004 |
| 15600 | ABC | 5/12/2014 | AG002 |
| 13400 | ABC | 7/12/2014 | AG003 |
我需要加入两个表WHERE Table2.SignUpDate BETWEEN Table1.StartDate AND Table1.EndDate
,然后我需要表2中的不同计数代理,以获得每个事件的销售数量,例如一个下面:
| EventCode | StartDate | EndDate | Client | CountOfDistinctAgents |
| 13400 | 1/12/2014 | 5/12/2014 | ABC | 2 | //(AG001, AG002)
| 13400 | 6/12/2014 | 8/12/2014 | ABC | 1 | //(AG003)
| 15600 | 3/12/2014 | 5/12/2014 | ABC | 2 | //(AG002, AG004)
但是我无法获得上述输出,因为我需要在 MS ACCESS 2003 中执行此操作。 请帮我。
这是我的旧代码,但它并不算DISTINCT代理。
SELECT tmp.[Event Code], eb.CampaignLN, eb.StartDate, eb.EndDate, COUNT(*) AS HeadCount
FROM
(SELECT DISTINCT [Event Code], Campaign, [Signup Date], [Agent Id] FROM [Main Table2]) AS tmp
INNER JOIN qryGetEventBookings AS eb ON (tmp.[Signup Date] BETWEEN CDATE(eb.StartDate) AND
CDATE(eb.EndDate)) AND (eb.CampaignLN=tmp.Campaign) AND (eb.LocationCode=tmp.[Event Code])
GROUP BY tmp.[Event Code], eb.CampaignLN, eb.StartDate, eb.EndDate;
我非常感谢你的帮助。提前谢谢。
答案 0 :(得分:3)
要计算不同的代理,您需要使用子查询(因为MS Access不支持count(distinct)
)。结果是这样的:
SELECT [Event Code], COUNT(*) AS NumAgents
FROM (SELECT DISTINCT m.EventCode, eb.Agent
FROM [Main Table2] as m JOIN
qryGetEventBookings eb
ON eb.CampaignLN = m.Campaign AND eb.LocationCode = m.[Event Code] AND
eb.SignUpDate BETWEEN m.StartDate AND m.EndDate
) AS tmp
GROUP BY [Event Code];