从3小时开始,我试图计算不同的列而不对它们进行分组。 我尝试使用连接或子选择查询,但因为我是SQL的新手,所以我没有解决该怎么做。
所以这是我的SQL查询:
SELECT [Destination ID],
[ProjectID],
[Campaign ProjectId]
FROM Raw_Data;
如何在一个SQL查询中计算目标ID,项目ID和Campaign项目ID,并显示表中的每个ID。
这里有一个例子,输出应该如何:
我正在使用MS Access。
如果有人可以帮助我的话会很棒。
非常感谢!
答案 0 :(得分:1)
如果我理解你的话,你可以试试这个:
SELECT
[Destination ID],
(SELECT COUNT([Destination ID])
FROM Raw_Data AS T2
WHERE T2.[Destination ID] = T1.[Destination ID]) AS DestCount,
[ProjectID],
(SELECT COUNT([ProjectID])
FROM Raw_Data AS T3
WHERE T3.[ProjectID] = T1.[ProjectID]) AS ProjectCount,
[Campaign ProjectId],
(SELECT COUNT([Campaign ProjectId])
FROM Raw_Data AS T4
WHERE T4.[Campaign ProjectId] = T1.[Campaign ProjectId]) AS CampaignCount
FROM Raw_Data AS T1;
修改强>
使用较新的Access版本,您可以尝试:
SELECT
T1.[Destination ID],
T2.[DestCount],
T1.[ProjectID],
T3.[ProjectCount],
T1.[Campaign ProjectId],
T4.[CampaignCount]
FROM (((Raw_Data AS T1
INNER JOIN
(
SELECT [Destination ID], COUNT([Destination ID]) AS DestCount
FROM Raw_Data
GROUP BY [Destination ID]
)
AS T2 ON
T1.[Destination ID] = T2.[Destination ID])
INNER JOIN
(
SELECT [ProjectID], COUNT([ProjectID]) AS ProjectCount
FROM Raw_Data
GROUP BY [ProjectID]
)
AS T3
ON T1.[ProjectID] = T3.[ProjectID])
INNER JOIN
(
SELECT [Campaign ProjectID], COUNT([Campaign ProjectID]) AS CampaignCount
FROM Raw_Data
GROUP BY [Campaign ProjectID]
)
AS T4
ON T1.[Campaign ProjectID] = T4.[Campaign ProjectID])
我无法在较新的Access版本上对此进行测试,只需了解这可行。