[英]Select Count of ip and Count of DISTINCT ip in same query
I have a table structure like this: 我有这样的表结构:
TABLE NAME : counter
id | datetime | url | ip
-------------------------------------------
1 |2013-04-12 13:27:09 | url1 | ip01
2 |2013-04-13 10:55:43 | url2 | ip02
3 |2013-04-14 11:14:12 | url1 | ip03
4 |2013-04-15 23:23:55 | url2 | ip04
5 |2013-05-02 08:34:44 | url1 | ip03
With a smart SELECT
query, I want to fetch 3 columns that are: 使用智能
SELECT
查询,我想获取3列:
Distinct URL | count of ip | count of distinct ip | for each distinct url | for each distinct url ------------------------------------------------------------------- url1 | 3 | 2 url2 | 2 | 2
I came up with my query below by the help of this solution in stackoverflow which gives me the first 2 columns that I need: 我在下面借助stackoverflow中的这个解决方案提出了我的查询,它给出了我需要的前两列:
SELECT url, COUNT(*) AS total_ip FROM counter GROUP BY url ORDER BY total_ip DESC
But how can I add the 3rd column also into my query? 但是如何将第3列添加到我的查询中呢?
Can you please help me? 你能帮我么?
As you mentioned yourself, you can use DISTINCT
keyword to get distinct URL
s. 正如您自己提到的,您可以使用
DISTINCT
关键字来获取不同的URL
。 Use COUNT
to get count of IP
and COUNT(DISTINCT ID)
to get distinct IP
for each URL
s 使用
COUNT
获取IP
和COUNT(DISTINCT ID)
计数,以获得每个URL
的不同IP
Try this: 试试这个:
SELECT DISTINCT URL AS D_URL
,COUNT(IP) AS IP
,COUNT(DISTINCT IP) AS D_IP
FROM counter
GROUP BY URL
Output: 输出:
╔═══════╦════╦══════╗
║ D_URL ║ IP ║ D_IP ║
╠═══════╬════╬══════╣
║ url1 ║ 3 ║ 2 ║
║ url2 ║ 2 ║ 2 ║
╚═══════╩════╩══════╝
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.