0

我有一张如下表

C1 | C2 | C3

aa | bb | cc
aa | dd | tt
jj | uu | yy
aa | bb | cc
jj | rr | pp

C1 和C2 可以以任何组合出现并且可以重复值。每个 C3 值对于 C2 值都是唯一的。我需要查询表以获取 C1 中的所有不同值,并且没有时间映射到 C2 中的不同值。按降序排列(不需要降序)。但需要按 C1 分组如下

C1 | C2 |count| C3

aa | bb | 2   | cc
aa | dd | 1   | tt
jj | uu | 1   | yy
jj | rr | 1   | pp

最好的方法是什么?

4

2 回答 2

2

This can be done using an aggregate function with a GROUP BY on the C1, C2, and C3 columns. Since you group by these 3 columns, the count will be a total of the rows that match:

select C1, C2, count(*) Total, C3
from yourtable
group by C1, C2, C3
order by C1, total desc

See SQL Fiddle with Demo

于 2013-06-14T17:46:26.213 回答
0
select C1, C2, Count(1), C3
from mytable
group by C1, C2, C3
order by 3 desc
于 2013-06-14T17:46:42.220 回答