SQL 来自多个表的多个计数
SQL multiple counts from multiple tables
我有以下 tables (“All_Countries” & “_Sent”)
并且需要获得 “Result”
table 中显示的结果。我想要的是计算所有“SubscriberKeys”和连接到这些 SubscriberKeys 的总数 “SendIDs”
,按 “Source”
分组——就像在 “Result”
table 中一样。我设法通过使用下面的查询实现了这一点(我认为),但我不确定我是否做错了。难道没有更好(更有效)的方法只用一个 select 语句而不需要额外的子查询吗?我使用 SQL 服务器 2005。
All_Countries
-------------------------------
SubscriberKey* | Source
-------------------------------
10001 | Campaign1
10002 | Campaign2
10003 | Campaign1
_已发送
-----------------------
SendID*| SubscriberKey*
-----------------------
1 | 10001
2 | 10001
3 | 10002
4 | 10002
5 | 10003
6 | 10003
结果
-----------------------------------------------------
Source* | SubscriberCount | SendCount
-----------------------------------------------------
Campaign1 | 2 | 4
Campaign2 | 1 | 2
Primary keys = * (e.g where you have a star in the column)
SELECT a.Source, COUNT(a.SubscriberKey) AS Subscribers,
(SELECT COUNT(b.SubscriberKey) AS Sent FROM _Sent AS b
INNER JOIN All_Countries AS c ON b.SubscriberKey = c.SubscriberKey
WHERE c.Source = a.Source) AS Sent
FROM All_Countries AS a
GROUP BY a.Source
我还没有对此进行测试,但我认为您可以通过在第一个 COUNT
中使用 DISTINCT 关键字来获得您想要的结果
SELECT
c.Source,
COUNT(DISTINCT SubscriberKey) AS SubscriberCount,
COUNT(*) AS SendCount
FROM
All_Countries c
JOIN _Sent s ON s.SubscriberKey = c.SubscriberKey
GROUP BY
c.Source
这样的查询怎么样?
select
source, count(distinct a.SubscriberKey) as SubscriberCount, count(distinct b.SendID) as SendCount
from All_Countries a join
_Sent b ON a.SubscriberKey = b.SubscriberKey
SELECT [Source],
COUNT(DISTINCT a.SubscriberKey) as SubscriberCount,
COUNT(SendId) as SendCount
FROM All_Countries a
INNER JOIN _Sent s
ON s.SubscriberKey = a.SubscriberKey
GROUP BY [Source]
输出:
Source SubscriberCount SendCount
Campaign1 2 4
Campaign2 1 2
我有以下 tables (“All_Countries” & “_Sent”)
并且需要获得 “Result”
table 中显示的结果。我想要的是计算所有“SubscriberKeys”和连接到这些 SubscriberKeys 的总数 “SendIDs”
,按 “Source”
分组——就像在 “Result”
table 中一样。我设法通过使用下面的查询实现了这一点(我认为),但我不确定我是否做错了。难道没有更好(更有效)的方法只用一个 select 语句而不需要额外的子查询吗?我使用 SQL 服务器 2005。
All_Countries
-------------------------------
SubscriberKey* | Source
-------------------------------
10001 | Campaign1
10002 | Campaign2
10003 | Campaign1
_已发送
-----------------------
SendID*| SubscriberKey*
-----------------------
1 | 10001
2 | 10001
3 | 10002
4 | 10002
5 | 10003
6 | 10003
结果
-----------------------------------------------------
Source* | SubscriberCount | SendCount
-----------------------------------------------------
Campaign1 | 2 | 4
Campaign2 | 1 | 2
Primary keys = * (e.g where you have a star in the column)
SELECT a.Source, COUNT(a.SubscriberKey) AS Subscribers,
(SELECT COUNT(b.SubscriberKey) AS Sent FROM _Sent AS b
INNER JOIN All_Countries AS c ON b.SubscriberKey = c.SubscriberKey
WHERE c.Source = a.Source) AS Sent
FROM All_Countries AS a
GROUP BY a.Source
我还没有对此进行测试,但我认为您可以通过在第一个 COUNT
中使用 DISTINCT 关键字来获得您想要的结果SELECT
c.Source,
COUNT(DISTINCT SubscriberKey) AS SubscriberCount,
COUNT(*) AS SendCount
FROM
All_Countries c
JOIN _Sent s ON s.SubscriberKey = c.SubscriberKey
GROUP BY
c.Source
这样的查询怎么样?
select
source, count(distinct a.SubscriberKey) as SubscriberCount, count(distinct b.SendID) as SendCount
from All_Countries a join
_Sent b ON a.SubscriberKey = b.SubscriberKey
SELECT [Source],
COUNT(DISTINCT a.SubscriberKey) as SubscriberCount,
COUNT(SendId) as SendCount
FROM All_Countries a
INNER JOIN _Sent s
ON s.SubscriberKey = a.SubscriberKey
GROUP BY [Source]
输出:
Source SubscriberCount SendCount
Campaign1 2 4
Campaign2 1 2