使用NOT LIKE选择不按预期执行的数字列表-Oracle SQL

时间:2019-06-27 13:49:28

标签: sql oracle

我正在尝试提取一个序列号列表,在这些序列号中,对应的部件号永远不能带有-INTERNAL标记。

我的建筑公司下达生产订单的方式是,可以在x个订单中将单个序列号转换为不同的部件号。我试图仅跟踪序列号,该序列号的任何零件号都从未带有-INTERNAL标记。

以下是我正在处理的数据的示例:

| Serial | Make_Order_Number |   Part_Number  |
|:------:|:-----------------:|:--------------:|
|   400  |       MO321       |      NB332     |
|   400  |       MO333       |      NB333     |
|   400  |       MO311       | NB334-INTERNAL |
|   92   |       MO222       |      NB122     |
|   92   |       MO299       |      NB123     |
|   92   |       MO212       |      NB124     |
|   545  |       MO214       |      NB092     |
|   774  |       MO215       | NB093-INTERNAL |
|   774  |       MO200       | NB093-INTERNAL |
|   774  |       MO218       |      NB833     |
|   987  |       MO110       |      NB741     |

理想情况下,我想获得:

| Serial |
|:------:|
|   92   |
|   545  |
|   987  |

我尝试写作

SELECT DISTINCT Serial
FROM tblParts
WHERE Part_Number NOT LIKE '%-INTERNAL%'

但是那让我产生了

| Serial |
|:------:|
|   92   |
|   400  |    
|   545  |
|   774  |
|   987  |

因为在某一点上serial 400 did 具有满足该条件的部件号。任何建议表示赞赏。

3 个答案:

答案 0 :(得分:2)

SELECT DISTINCT Serial
FROM tblParts
WHERE Serial 
NOT IN 
(SELECT Serial 
FROM tblParts 
WHERE Part_Number 
LIKE '%-INTERNAL%')

答案 1 :(得分:2)

您可以通过使用条件和,例如:

WITH your_table AS (SELECT 400 serial, 'MO321' make_order_number, 'NB332' part_number FROM dual UNION ALL
                    SELECT 400 serial, 'MO333' make_order_number, 'NB333' part_number FROM dual UNION ALL
                    SELECT 400 serial, 'MO311' make_order_number, 'NB334-INTERNAL' part_number FROM dual UNION ALL
                    SELECT 92 serial, 'MO222' make_order_number, 'NB122' part_number FROM dual UNION ALL
                    SELECT 92 serial, 'MO299' make_order_number, 'NB123' part_number FROM dual UNION ALL
                    SELECT 92 serial, 'MO212' make_order_number, 'NB124' part_number FROM dual UNION ALL
                    SELECT 545 serial, 'MO214' make_order_number, 'NB092' part_number FROM dual UNION ALL
                    SELECT 774 serial, 'MO215' make_order_number, 'NB093-INTERNAL' part_number FROM dual UNION ALL
                    SELECT 774 serial, 'MO200' make_order_number, 'NB093-INTERNAL' part_number FROM dual UNION ALL
                    SELECT 774 serial, 'MO218' make_order_number, 'NB833' part_number FROM dual UNION ALL
                    SELECT 987 serial, 'MO110' make_order_number, 'NB741' part_number FROM dual)
SELECT serial
FROM   your_table
GROUP BY serial
HAVING SUM(CASE WHEN part_number LIKE '%-INTERNAL%' THEN 1 ELSE 0 END) = 0
ORDER BY serial;

    SERIAL
----------
        92
       545
       987

如果零件编号包含“ -INTERNAL”,则输入1;否则输入0,然后将每个序列的编号相加,然后排除总和大于0的数字。

此查询的速度可能会或可能不会快于自联接查询的速度-您应该针对您的数据进行测试,并查看哪种数据最适合您。

答案 2 :(得分:2)

您可以GROUP BY serial并按组HAVING进行过滤,没有带内部零件的实例。

Oracle设置

CREATE TABLE tblParts ( Serial, Make_Order_Number, Part_Number ) AS
  SELECT 400, 'MO321', 'NB332' FROM DUAL UNION ALL
  SELECT 400, 'MO333', 'NB333' FROM DUAL UNION ALL
  SELECT 400, 'MO311', 'NB334-INTERNAL' FROM DUAL UNION ALL
  SELECT  92, 'MO222', 'NB122' FROM DUAL UNION ALL
  SELECT  92, 'MO299', 'NB123' FROM DUAL UNION ALL
  SELECT  92, 'MO212', 'NB124' FROM DUAL UNION ALL
  SELECT 545, 'MO214', 'NB092' FROM DUAL UNION ALL
  SELECT 774, 'MO215', 'NB093-INTERNAL' FROM DUAL UNION ALL
  SELECT 774, 'MO200', 'NB093-INTERNAL' FROM DUAL UNION ALL
  SELECT 774, 'MO218', 'NB833' FROM DUAL UNION ALL
  SELECT 987, 'MO110', 'NB741' FROM DUAL;

查询

SELECT Serial
FROM   tblParts
GROUP BY Serial
HAVING COUNT( CASE WHEN Part_Number LIKE '%-INTERNAL%' THEN 1 END ) = 0

输出

| SERIAL |
| -----: |
|    987 |
|    545 |
|     92 |

db <>提琴here