需要查询两个字段的不同组合,以及发生不同组合的计数 [英] Need to query distinct combination of two fields, along with a count that distinct combination occurs

查看:86
本文介绍了需要查询两个字段的不同组合,以及发生不同组合的计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要的是对表的查询,该查询将返回列A和B的不同组合,以及每种组合在表中出现的次数。

What I need is a query on a table that would return distinct combinations of columns A and B, along with the count of how many times each combination occurs in the table. This would all be sorted by Column A.

如果表为:

A    B  .......
1    1
1    1
1    1
1    2
2    1
2    1

结果将是:

A    B     count
1    1       3
1    2       1
2    1       2

任何帮助都会很棒。

推荐答案

GROUP BY是您的朋友在这里:

GROUP BY is your friend here:

select a,b,count(*) from test
group by a,b
order by a

SQLFiddle: http://sqlfiddle.com/#!9/062b0e/5

SQLFiddle: http://sqlfiddle.com/#!9/062b0e/5

这篇关于需要查询两个字段的不同组合,以及发生不同组合的计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆