1
a b c
1 2 3
1 2 2
1 3 3
2 3 4
2 3 5

我想找到所有相同的 a+b

对于该数据,我的结果应该是

1 2 3 (look a and b)
1 2 2 (look a and b) they are same so should be in the result
**
2 3 4 (look a and b)
2 3 5 (look a and b) they are same so should be in the result

*

select count(a),a,count(b),b from Table A
group by a,b
having count(a)>1
and count(b)>1

就像那样工作,我可以看到哪些是那样的。但我想查看原始数据。

我怎样才能看到所有带有这个有子句的数据?因为该表有很多列,我不想按它们分组。我只想按 a 和 b 分组并查看所有列。

4

5 回答 5

0

这是另一种选择具有重复值的值的方法。

在下面的查询中,基于列值的重复值ab视为一个表并与原始表连接。

create table RawDataTable (a int, b int, c int)
insert into RawDataTable values
(1,2,3), (1,2,2), (1,3,3), (2,3,4), (2,3,5)

SELECT a.*
FROM RawDataTable a
JOIN (
    SELECT a
        ,b
        ,COUNT(*) AS RN
    FROM RawDataTable
    GROUP BY a
        ,b
    HAVING count(*) > 1
    ) b ON a.a = b.a
    AND a.b = b.b

这是现场db<>fiddle演示。

于 2020-03-27T15:00:53.433 回答
0

将结果与原始表连接起来,例如:

select * from
(
    select a,b
    from mytable 
    group by a,b
) X
left join  mytable Y on X.a = Y.a and X.b = Y.b;
于 2020-03-27T14:49:38.020 回答
0

我认为你想要的可以做你想做的事exists

select t.*
from mytable t
where exists (
    select 1 
    from mytable t1 
    where t1.a = t.a and t1.b = t.b and t1.c <> t.c
)

假设(a, b, c)元组是唯一的,您还可以进行窗口计数:

select *
from (select t.*, count(*) over(partition by a, b) cnt from mytable t) t
where cnt = 1
于 2020-03-27T14:47:20.177 回答
0

您可以使用窗口函数:

select t.*
from (select t.*, count(*) over (partition by a, b) as cnt
      from t
     ) t
where cnt > 1;
于 2020-03-27T14:47:29.243 回答
0

我将使用一个查询来查找多次出现的 a 和 b 对,然后将其加入原始表中:

SELECT x.*
FROM   mytable x
JOIN   (SELECT a, b
        FROM   mytable
        GROUP BY a, b
        HAVING   COUNT(*) > 1) x ON x.a = y.a and x.b = y.b
于 2020-03-27T14:48:27.227 回答