假设我有以下问题:
SELECT DISTINCT(Value1,value2,value3) From table1
并获得类似
的内容 # value1 value2 value3
--------------------------------------
1. result1 result1 result2
2. result1 result2 result2
3. result5 result6 result7
如何添加一个额外的列,告诉我出现这些唯一值组合的次数,例如(最后一列是它出现的次数):
# value1 value2 value3 #occurred
--------------------------------------
1. result1 result1 result2 15
2. result1 result2 result2 25
3. result5 result6 result7 35
答案 0 :(得分:3)
SELECT Value1,value2,value3,count(*)
From table1
group by Value1,value2,value3
答案 1 :(得分:2)
使用group by
代替distinct
。然后,您可以使用count
。
select value1, value2, value3, count(*) as occurred
from table1
group by value1, value2, value3;
SQLite中的演示。
sqlite> select * from table1 order by value1, value2, value3;
value1 value2 value3
---------- ---------- ----------
1 1 2
1 1 2
1 2 2
1 2 2
1 2 2
5 6 7
sqlite> select value1, value2, value3, count(*) as '#occurred' from table1 group by value1, value2, value3;
value1 value2 value3 #occurred
---------- ---------- ---------- ----------
1 1 2 2
1 2 2 3
5 6 7 1