在表格中,我有4列,ID
和V1
,V2
和V3
。如何选择Value1和Value2重复的行的ID?例如:
ID | V1 | V2 | V3
1 A B X
2 A C Y
3 A B Z
4 A B W
5 F B K
6 A B N
7 H B N
8 O C N
我期待结果:1,3,4和6
我已经尝试了什么
select MovimentacaoContaId from MovimentacaoConta
group by NumeroDocumento,PessoaId,DataEmissaoReal,ValorTotalReal
having count(NumeroDocumento)>1 and count(PessoaId)>1 and count(DataEmissaoReal)>1 and count(ValorTotalReal)>1
select MovimentacaoContaId from MovimentacaoConta
group by NumeroDocumento,PessoaId,DataEmissaoReal,ValorTotalReal
having count(NumeroDocumento,PessoaId,DataEmissaoReal,ValorTotalReal)>1 //obvious error...
select distinct MovimentacaoContaId from MovimentacaoConta
group by NumeroDocumento,PessoaId,DataEmissaoReal,ValorTotalReal
having count(NumeroDocumento)>1 and count(PessoaId)>1 and count(DataEmissaoReal)>1 and count(ValorTotalReal)>1
PS:我想要这些特定的列,因为V1和V2应该是一个compisite PK,但是之前的开发人员没有这样做,并且在它之上插入了几个错误,而不是更新记录,插入... < / p>
答案 0 :(得分:1)
您需要以某种方式自我加入,因为您必须将聚合与每一行相关联。
SELECT
ID
FROM
MovimentacaoConta mc
INNER JOIN (
SELECT
V1,
V2,
COUNT(*) AS row_count
FROM
MovimentacaoConta
GROUP BY
V1,
V2
) mc0
ON
mc.v1 = mc0.v1
AND
mc.v2 = mc0.v2
WHERE
mc0.row_count > 1
答案 1 :(得分:1)
select table.*
from table
join (select V1, V2
from table
group by V1, V2
having count(*) > 1) V1V2
on table.V1 = V1V2.V1
and table.V2 = V1V2.V2
答案 2 :(得分:0)
这是我最喜欢的方式:
;with cte as
(
select ROW_NUMBER() over (partition by v1,v2 order by v1,v2) as rn, *
from your_tbl
)
select * from cte where rn > 1