简体   繁体   English

带ORDER,DISTINCT和COUNT的SQL请求

[英]SQL request with ORDER, DISTINCT and COUNT

My table has 4 columns: id, name, ip, timestamp 我的表有4列:ID,名称,IP,时间戳

I'm trying to get results as following: "Show me for each "name" the count of rows you have with distinct "ip", and non-distinct ip (the total)" 我正在尝试获得如下结果:“为每个“名称”显示我具有不同“ ip”和非唯一ip(总数)的行数”

I'm using this table to store all clicks users did on some links and I want to show a top click: for each "name" the amount of users who did click, and the total amount of clicks for this "name". 我正在使用此表存储用户在某些链接上进行的所有点击,我想显示一次点击:对于每个“名称”,单击的用户数量以及该“名称”的总点击量。

Is that even possible in one SQL request ? 在一个SQL请求中甚至可能吗?

Do it like this: 像这样做:

select name
     , count(*) total_clicks
     , count(distinct ip) distinct_ppl
from table_name
group by name
order by name /* or by count(*) desc or count(distinct ip) desc */

您可以按以下方式获得排名前10位的点击器:

SELECT count(ip),* FROM table GROUP BY ip ORDER BY count(ip) LIMIT 10;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM