选择具有相同列值的行

时间:2014-12-24 07:09:05

标签: oracle group-by

我正在尝试为以下方案构建查询

如果我的表是

signature |   id  | operation

abc       |    1  |   1234 

xyz       |    1  |    1234

pqr       |    2  |    1234

然后我的输出应该是

signature |  id

abc       |  1

xyz       |  1

即。特定列中具有相同值的行。

我已形成像

这样的查询
select signature,id 
from tablename 
where operation = '1234' 
group by signature,id 
having count(*) >0;

但这会返回包括xyz |在内的所有内容1也。

有人可以建议我正确查询吗?

4 个答案:

答案 0 :(得分:0)

试试这个:

SELECT signature,COUNT(id) FROM table_name WHERE operation = '1234' GROUP BY id;

答案 1 :(得分:0)

我还没有对此进行过测试,这可能过于复杂,但我认为这样可行:

SELECT Signature, ID from tablename WHERE ID in( SELECT ID FROM (SELECT ID, COUNT(*) as NumRecords from tablename GROUP BY ID HAVING NumRecords > 1)))

答案 2 :(得分:0)

试试这个:

我们需要在ID列上应用PARTITION,如下所示,

SELECT Result。[signature],Result.ID, ROW_NUMBER()over(PARTITION BY Result.ID ORDER BY Result.ID)AS [RowNum] INTO #TempResult FROM table_name AS Result GROUP BY结果。[签名],Result.ID

SELECT Result。[签名],Result.ID
来自#TempResult AS结果 结果。[RowNum] = 1

答案 3 :(得分:0)

您可以使用where exists替代group by

select 
signature,
id 
from tablename t1 
where exists (
   select * from tablename t2 
   where t1.id = t2.id 
   and t1.signature != t2.signature 
   and t1.operation = 1234 
);