Oracle查询结果记录特定状态和描述

时间:2012-06-19 12:12:57

标签: sql oracle11g

我有一个Oracle表,如下所示:

Orders
---------
OrderFullFillID
Description
OrderDate
OrderStatus
Qty

Below are sample records

1    PTS123   1/1/2012     Settled     10
2    PTS123   1/1/2012    ArchSettled  10
3    PTS124   2/1/2012    Settled      10
4    PTS124   2/1/2012    Settled      20
5    PTS125   2/1/2012    Settled      20
6    PTS125   2/1/2012    Settled      20
7    PTS126   2/1/2012    Settled      20
8    PTS126   2/1/2012    ArchSettled  20

如何显示具有相同描述且重复状态='已结算'的记录? 将有两个具有相同描述,状态但状态不同的记录。 我只需要列出status ='已结算'且描述相同的那些。

在上面的示例中,查询应返回以下内容:

    3    PTS124   2/1/2012    Settled      10
    4    PTS124   2/1/2012    Settled      20
    5    PTS125   2/1/2012    Settled      20
    6    PTS125   2/1/2012    Settled      20

请帮忙

2 个答案:

答案 0 :(得分:1)

SELECT OrderFulFillID,
       Description,
       OrderData,
       OrderStatus,
       Qty
  FROM orders a
 WHERE status = 'Settled'
   AND NOT EXISTS( SELECT 1
                     FROM orders b
                    WHERE a.description = b.description
                      AND a.statusDate  = b.statusDate
                      AND a.status     != b.status )

应该有效

答案 1 :(得分:0)

SELECT OrderFulFillID,
       Description,
       OrderData,
       OrderStatus,
       Qty
  FROM orders a
  WHERE status = 'Settled'
    AND EXISTS( SELECT 1
                  FROM orders b
                  WHERE a.description     = b.description
                    AND a.OrderFulFillID != b.OrderFulFillID
                    AND a.status          = b.status )