我有一个由5列组成的数据库
每列都是INT
我想找出每列中最常出现的数字
我还想知道更频繁出现哪个数字序列
对于最不常见的
,情况也是如此我想使用MYSQL或SQLITE
答案 0 :(得分:4)
tbl(..., int_col1, int_col2, int_col3, int_col4, int_col5, ...)
对每列执行以下查询
SELECT int_col1, COUNT(int_col1)
FROM tbl
GROUP BY int_col1
ORDER BY COUNT(int_col) DESC LIMIT 1
对每列执行以下查询
SELECT int_col1, COUNT(int_col1)
FROM tbl
GROUP BY int_col1
ORDER BY COUNT(int_col) ASC LIMIT 1
SELECT int_col1, int_col2, int_col3, int_col4, int_col5, COUNT(*)
FROM tbl
GROUP BY int_col1, int_col2, int_col3, int_col4, int_col5
ORDER BY COUNT(*) DESC;
答案 1 :(得分:0)
assuming column is userid
select UserID, count(UserID)
from myUsers
group by UserID
order by count(UserID) desc
OR
with cte as
(
select user_id,ROW_NUMBER() over (order by UserID) as rn
)
select user_id,count(user_id) as se_count from cte group by user_id