我有一张桌子"用户"列ip,os,国家和浏览器。我想找到ip,os,country和浏览器的最大数量。在PostgreSQL中有任何查询
我目前使用的查询是
SELECT *
FROM
(
SELECT COUNT(ip),ip FROM user GROUP BY ip
UNION ALL
SELECT COUNT(os),os FROM user GROUP BY os
UNION ALL
SELECT COUNT(country),country FROM user GROUP BY country
UNION ALL
SELECT COUNT(browser),browser FROM user GROUP BY browser
) user
它显示了所有ip,os,国家和浏览器及其计数 我真正想要的是列名称,该列的最大数量
是否可以在单个查询中执行此操作?
我期待这样的事情
os count ip count
linux 50 xx:xx:xx:xx 95
答案 0 :(得分:2)
DryRun = True
答案 1 :(得分:1)
您可以使用HAVING
和ALL
。出于可读性目的,我将仅显示一列
SELECT COUNT(ip),ip
FROM user
GROUP BY ip
HAVING COUNT(ip) >= all
(
SELECT COUNT(ip)
FROM user
GROUP BY ip
)