开发者

MySQL Grouping By and COUNT

开发者 https://www.devze.com 2023-03-12 13:24 出处:网络
I have a table which contains the following columns: UniqueID remote_ip_addr platform I want to create a query, which basically shows each IP address just once from each platform and count how many

I have a table which contains the following columns:

UniqueID remote_ip_addr platform

I want to create a query, which basically shows each IP address just once from each platform and count how many distinct platform entries there are

SELECT platform, COUNT(platform) 
FROM fuckedapps_dm_appdl dm 
GROUP BY platform 
ORDER BY COUNT(platform) DESC

So for example from the following table:

uniqueID---remote_ip_addr----platform
1           20.15.1.234       iPhone
2           20.15.1.234       iPhone
3           20.15.1.234       iPhone
4           20.15.1.234       Android
5           20.15.1.234       Android

I'll get the following result:

platform----COUNT(platform)
iPhone          1
Android         1

Right now I get the following:

platform----COUNT(platform)
iPhone          3
Android         2

Because they're all the same ip, I want to get only 1 distinct value...

Thank you for your help.

edit: I should have mentioned that there's another field, which is called app_id.

So the table is more like:

uniqueID---remote_ip_addr----platform---app_id
1           20.15.1.234       iPhone      23
2           20.15.1.234       iPhone      23
3           20.15.1.234       iPhone      18
4           20.15.1.234       Android     24
5           20开发者_运维问答.15.1.234       Android     25

in this case, I want the result to be:

platform----COUNT(platform)
iPhone          2
Android         2

Thank you, and sorry because I didn't think it was relevant and now I see it is.


If I understand correctly, you want for every platfrom, the number of distinct IP addresses. Then you can use this. Note the COUNTing of DICTINCT remote_ip_addr for every (GROUP BY) platform:

SELECT platform
     , COUNT(DISTINCT remote_ip_addr) AS countDistinctIPaddresses
FROM fuckedapps_dm_appdl dm 
GROUP BY platform 
ORDER BY countDistinctIPaddresses DESC

After OP's update. (it's not good practise to alter the question by the way).

It's not clear if you want distinct app_ids for every platfrom:

SELECT platform
     , COUNT(DISTINCT app_id)
       AS cnt
FROM fuckedapps_dm_appdl dm 
GROUP BY platform 
ORDER BY cnt DESC

or distinct combinations of (app_id,remote_ip_addr)s for every platfrom:

SELECT platform
     , COUNT(DISTINCT app_id, remote_ip_addr)
       AS cnt
FROM fuckedapps_dm_appdl dm 
GROUP BY platform 
ORDER BY cnt DESC

Depends on what the wanted result is, say if you had one more row:

uniqueID---remote_ip_addr----platform---app_id
6           100.30.1.40       iPhone     23


Use COUNT(*) instead:

SELECT platform, COUNT(*) 
FROM fuckedapps_dm_appdl dm 
GROUP BY platform 
ORDER BY COUNT(platform) DESC


You need to group by the IP address as well:

SELECT platform, COUNT(*) 
FROM fuckedapps_dm_appdl dm 
GROUP BY platform, remote_ip_addr 
ORDER BY COUNT(platform) DESC


You have to group by IP address to get the number of different platforms:

SELECT platform, COUNT(platform) 
FROM fuckedapps_dm_appdl dm 
GROUP BY remote_ip_addr 
ORDER BY COUNT(platform) DESC
0

精彩评论

暂无评论...
验证码 换一张
取 消