如何找到最常见和最不常见的数字

时间:2013-11-18 04:37:01

标签: mysql sql sqlite

我有一个由5列组成的数据库

每列都是INT

我想找出每列中最常出现的数字

我还想知道更频繁出现哪个数字序列

对于最不常见的

,情况也是如此

我想使用MYSQL或SQLITE

2 个答案:

答案 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