oracle sql 查询问题(按 2 列分组)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6236339/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 10:51:17  来源:igfitidea点击:

oracle sql query question(grouping by 2 columns)

sqloracleaggregate-functions

提问by csuo

I have a table called testgroupin my database, which is like following:

testgroup我的数据库中有一个表,如下所示:

I                      J                      
---------------------- ---------------------- 
1                      a                      
1                      a                      
2                      a 
1                      b                      
1                      c                      
2                      b      
3                      d    
2                      b 
2                      b
3                      d        

Now, I want the result as below:

现在,我想要的结果如下:

I                      J                      COUNT(J) in I 
---------------------- ---------------------- ----------------------
1                      a                      2                    
2                      a                      1
1                      b                      1
1                      c                      1
2                      b                      3    
3                      d                      2            

...where count(j) in Iis the number of each J related to the I.
For example: with I = 1, there are 2 ain column J, so the third column would be equal 2.

... 其中count(j) in I是与 I 相关的每个 J 的数量。
例如:有I = 1aJ 列中有 2 个,因此第三列将等于 2。

回答by Johan

select I, J, count(*) as JinI
FROM atable
GROUP BY I, J

回答by Farshid Zaker

In fact the question is about counting I and J pairs:

事实上,问题是关于计算 I 和 J 对:

select I, J, count(*) from tblName group by I, J