计算一个或两个查询中两列的数据
Count data from two columns in one or two queries
我的 table 中有以下不同的行:
JobID ClientID Date URL
a 1 Apr 27 2020 8:21AM http://somewebsite.com
a 1 Apr 29 2020 12:57AM http://somewebsite.com
a 1 Apr 30 2020 5:05AM http://anotherwebsite.com
a 2 May 3 2020 6:09PM http://anotherwebsite.com
a 3 May 20 2020 12:55AM https://thirdlink.com
b 1 Apr 30 2020 5:16AM http://anotherwebsite.com
b 2 May 3 2020 6:09PM http://anotherwebsite.com
b 2 May 11 2020 8:39AM https://thirdlink.com
我正在尝试构建一个或两个会给出以下结果的查询:
- 每个客户每个作业的总点击次数:
JobID ClientID Number of Clicks
a 1 3
a 2 1
a 3 1
b 1 1
b 2 2
- 每个客户每个作业的唯一 URL 数量:
JobID ClientID Number of URLs
a 1 2
a 2 1
a 3 1
b 1 1
b 2 2
这是我尝试过的方法,但它没有正确聚合数据:
SELECT ClientID,
COUNT(ClientID) AS [Number of Clicks],
JobId
FROM [table]
GROUP BY ClientID, JobId
SELECT ClientID,
COUNT(URL) AS [Number of URLs],
JobId
FROM [table]
GROUP BY ClientID, JobId
任何有关如何实现此目的的提示,我们将不胜感激,谢谢!
您导致聚合和 count()
如下:
select
jobid,
clientid,
count(*) cnt_clicks,
count(distinct url) cnt_distinct_url
from mytable
group by clientid, jobid
我的 table 中有以下不同的行:
JobID ClientID Date URL
a 1 Apr 27 2020 8:21AM http://somewebsite.com
a 1 Apr 29 2020 12:57AM http://somewebsite.com
a 1 Apr 30 2020 5:05AM http://anotherwebsite.com
a 2 May 3 2020 6:09PM http://anotherwebsite.com
a 3 May 20 2020 12:55AM https://thirdlink.com
b 1 Apr 30 2020 5:16AM http://anotherwebsite.com
b 2 May 3 2020 6:09PM http://anotherwebsite.com
b 2 May 11 2020 8:39AM https://thirdlink.com
我正在尝试构建一个或两个会给出以下结果的查询:
- 每个客户每个作业的总点击次数:
JobID ClientID Number of Clicks
a 1 3
a 2 1
a 3 1
b 1 1
b 2 2
- 每个客户每个作业的唯一 URL 数量:
JobID ClientID Number of URLs
a 1 2
a 2 1
a 3 1
b 1 1
b 2 2
这是我尝试过的方法,但它没有正确聚合数据:
SELECT ClientID,
COUNT(ClientID) AS [Number of Clicks],
JobId
FROM [table]
GROUP BY ClientID, JobId
SELECT ClientID,
COUNT(URL) AS [Number of URLs],
JobId
FROM [table]
GROUP BY ClientID, JobId
任何有关如何实现此目的的提示,我们将不胜感激,谢谢!
您导致聚合和 count()
如下:
select
jobid,
clientid,
count(*) cnt_clicks,
count(distinct url) cnt_distinct_url
from mytable
group by clientid, jobid