我有一张有栏的桌子:
column1|column2|column3|column4|Column5
A,B,1,2,a
A,B,3,4,b
C,D,1,2,a我需要检查字段组column1|column2在字段column3|column4上是否有多个组合。
示例:
A,B 2(因为对于A,B组合,我有两个列的组合,即1,2和3,4)
C,D 1发布于 2014-11-25 15:54:38
在正常情况下,你只需要:
select col1, col2, count(*)
from table t
group by col1, col2
having count(*) > 1;在第3和第4列中,您需要不同的值。一些数据库支持:
select col1, col2, count(distinct col3, col4)
from table t
group by col1, col2
having count(distinct col3, col4) > 1;在其他数据库中,连接值是最简单的:
select col1, col2, count(distinct concat(col3, ':', col4))
from table t
group by col1, col2
having count(distinct concat(col3, ':', col4)) > 1;注意:虽然concat()函数是标准的,但并非所有数据库都支持它。
发布于 2014-11-25 15:56:24
获取column3和column4组合的不同计数,如下所示
select column1, column2, count (distinct column3 || '-' || column4)
from table
group by column1, column2
having count (distinct column3 || '-' || column4) > 1;||是Oracle的级联操作符。在其他数据库中,您将有其他连接操作符。例如concat。分隔符-将确保像12、3和1,23这样的对不会产生相同的组合。
https://stackoverflow.com/questions/27131009
复制相似问题