Oracle:如何选择不重复的行

时间:2014-10-16 17:53:49

标签: sql oracle

我必须消除具有X值且重复的那些。如果他们没有X,他们会留下来,如果他们有X但是没有重复他们留下来。你能帮忙吗?

Id        Value          

1.          A.               
2.          X.               
3.          X.               
3.          C.              
3.          D.                
4.          X.    
4.          F
5.          G
6.          Z
7.          X
8.          X
8.          G

查询结果应为:

1. A
2. X
5. G
6. Z
7. X

2 个答案:

答案 0 :(得分:2)

也许有帮助:

SELECT Id,
       Value
  FROM(SELECT Id,
              Value,
              COUNT(*) OVER (PARTITION BY Id) cnt,
              SUM(DECODE(Value, 'X', 1, 0)) OVER (PARTITION BY Id) sumx
         FROM your_table
      )
 WHERE cnt = 1 OR sumx = 0;

答案 1 :(得分:1)

那还不够吗?

SELECT "Id", MIN("Value") "Value" FROM T
GROUP BY "Id" HAVING COUNT("Id") = 1
ORDER BY "Id"

它也会丢弃给定id的多个非-X值。至少,根据您的示例数据,似乎可以产生所需的结果:

ID  VALUE
1   A
2   X
5   G
6   Z
7   X

请参阅http://sqlfiddle.com/#!4/81baa/10