简体   繁体   中英

SQL request with ORDER, DISTINCT and COUNT

My table has 4 columns: id, name, ip, timestamp

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)"

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 ?

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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