如何消除SQL中不存在的不需要的数据

时间:2015-07-27 07:26:44

标签: sql oracle

嗨我有像 -

这样的数据
ORDER_NUMBER   REVISION_NUMBER
2-345                  1
2-345                  2
2-345                  3
5-436                  1
6-436                  1

现在我只需要选择那些只有版本号1的order_numbers,而order_number不应该有任何其他版本号,如2,3,

在这种情况下,它应显示order_numbers - 5-436和6-436,因为2-345也有修订号(2,3)。

我如何在SQL中执行此操作?

4 个答案:

答案 0 :(得分:2)

使用不存在的另一种解决方案:

Select ordernumber from table a
WHERE NOT EXISTS (
SELECT 1 
FROM table b 
WHERE a.ordernumber = b.ordernumber and revisionnumber != 1)

答案 1 :(得分:2)

因为您具体说明要使用NOT EXISTS ...

SELECT * FROM Orders T WHERE T.REVISION_NUMBER = 1
AND NOT EXISTS ( SELECT * FROM Orders T2 
                WHERE T2.ORDER_NUMBER = T.ORDER_NUMBER 
                AND T2.REVISION_NUMBER <> 1)

Here's an SQL Fiddle that demonstrates it in action

答案 2 :(得分:1)

试试这个

select max(order_number), revision_number
from table
group by revision_number
having count(*) = 1 and revision_number = 1

答案 3 :(得分:0)

首先找到每个ORDER_NUMBER的最大修订版,然后过滤REVISION_NUMBER > 1

select * from 
(select ORDER_NUMBER, max(REVISION_NUMBER) as max_rev from [your_table_name] 
group by ORDER_NUMBER)
where max_rev = 1;