Sqlite和计算多个字段的频率

时间:2013-08-16 08:12:03

标签: sqlite count

我有一个名为“number”的表,有4列(id,N1,N2,N3),如下所示:

id = 1,2 3, 4


N1 = 22 , 14 , 13 , 15

N2 = 13 , 11 , 80 , 11

N3 = 11 , 22 , 47 , 23

我可以计算此表中个别数字的总频率吗? (11 = 3) ** (22=2)等。

对于一列我的查询是:

"SELECT N1, COUNT(*) as Frequency FROM number GROUP BY n1 order by count(*) desc"

感谢您提供帮助

1 个答案:

答案 0 :(得分:0)

试试这个:

SELECT t.num, COUNT(t.num)
FROM (SELECT N1 AS num FROM number
      UNION ALL
      SELECT N2 AS num FROM number
      UNION ALL
      SELECT N3 AS num FROM number) AS t
GROUP BY t.num
ORDER BY COUNT(t.num) desc;

fiddle demo

UNION operator