优先/过滤从 UNION 查询中获取的数据

问题描述 投票:0回答:1

我从下面的查询中获得了以下数据样本,其中我有两个

UNION
ed 查询。我想从总体查询中执行什么操作,我想要排除记录,其中第一个查询中的第二个查询中存在相同的
ITEM_NUMBER
SUBINVENTORY_CODE
,并且第二个查询中的记录的
INV_LOCATOR
存在于其中值为“AAAAAAA”。我尝试通过使用具有
ROW_NUMBER
的分析窗口函数来执行此操作,但我承认我没有看到需要如何更改它以正确设置
ROW_NUMBER
值,然后在最外层查询中对其进行过滤。

这是我基于以下查询的当前结果:

ITEM_NUMBER SUBINVENTORY_CODE   INV_LOCATOR     QUERY       IVT_SEQNUM  PRIORITY    OVERALL_PRIORITY
12923       41OPR               Z-B-01-02-C     1st Query   1           1           1
12923       41OPR               W-130-00-00-00  1st Query   2           1           2
12923       11OPR               AAAAAAA         2nd Query   1           2           1
12923       41OPR               AAAAAAA         2nd Query   1           2           1

期望的结果:

ITEM_NUMBER SUBINVENTORY_CODE   INV_LOCATOR     QUERY       IVT_SEQNUM  PRIORITY    OVERALL_PRIORITY
12923       41OPR               Z-B-01-02-C     1st Query   1           1           1
12923       41OPR               W-130-00-00-00  1st Query   2           1           2
12923       11OPR               AAAAAAA         2nd Query   1           2           1

正如您在所需结果中看到的,我们已从“第二个查询”中排除了最后一条记录,因为它包含

INV_LOCATOR
AND
AAAAAAA 值,同样的
ITEM_NUMBER
SUBINVENTORY_CODE
组合已存在于“第一个查询”的结果集

SELECT * FROM
(SELECT item_number, subinventory_code, INV_LOCATOR, QUERY, ivt_seqnum, priority, ROW_NUMBER() OVER (PARTITION BY priority, item_number, 
subinventory_code  ORDER BY priority DESC, priority) Overall_Priority 
  
  FROM
(SELECT ESI.item_number,  IOQD.subinventory_code  SUBINVENTORY_CODE, 

ROW_NUMBER() OVER (PARTITION BY ESI.item_number , IOQD.subinventory_code  
ORDER BY ESI.item_number, IOQD.subinventory_code ) ivt_seqnum
               
 ,LOC.SEGMENT1||'-'||LOC.SEGMENT2||'-'||LOC.SEGMENT3||'-'||LOC.SEGMENT4||'-'||LOC.SEGMENT5 INV_LOCATOR, 1 as priority, '1st Query' AS QUERY 
 
FROM egp_system_items ESI, inv_onhand_quantities_detail IOQD, inv_item_locations LOC
WHERE ESI.INVENTORY_ITEM_ID = 100000110904412
AND ioqd.subinventory_code = '41OPR'
AND IOQD.inventory_item_id = ESI.inventory_item_id    
AND LOC.inventory_location_id = ioqd.locator_id

UNION ALL
      
SELECT ESI.item_number, decode(substr(IOQD.secondary_inventory,1,3),                                
                                                              
                                                              ,'41O','41OPR'                                                                  
                                                              ,IOQD.secondary_inventory) subinventory_code, 

ROW_NUMBER() OVER (PARTITION BY ESI.item_number , IOQD.secondary_inventory  
ORDER BY ESI.item_number, IOQD.secondary_inventory ) ivt_seqnum , 
               
case when 
 (SELECT iil.SEGMENT1||'-'||iil.SEGMENT2||'-'||iil.SEGMENT3||'-'||iil.SEGMENT4||'-'||iil.SEGMENT5
           FROM inv_item_loc_defaults iild, 
                inv_item_locations iil
          WHERE iild.inventory_item_id = esi.inventory_item_id
            AND iild.subinventory_code = IOQD.secondary_inventory
            AND iild.locator_id       = iil.inventory_location_id
            AND rownum= 1 and length(Segment1) >= 1 ) is not null 
then (SELECT iil.SEGMENT1||'-'||iil.SEGMENT2||'-'||iil.SEGMENT3||'-'||iil.SEGMENT4||'-'||iil.SEGMENT5
           FROM inv_item_loc_defaults iild, 
                inv_item_locations iil
          WHERE iild.inventory_item_id = esi.inventory_item_id
            AND iild.subinventory_code = IOQD.secondary_inventory
            AND iild.locator_id       = iil.inventory_location_id
            AND rownum= 1 and length(Segment1) >= 1 )     
 
Else 'AAAAAAA'
END as      INV_LOCATOR   , 2 as priority  , '2nd Query' AS QUERY       
               
FROM egp_system_items ESI, INV_ITEM_SUB_INVENTORIES IOQD 
WHERE IOQD.inventory_item_id = ESI.inventory_item_id    
AND ESI.INVENTORY_ITEM_ID = 100000110904412

) --WHERE (priority <> 2 AND INV_LOCATOR <> 'AAAAAAA') 
) 
sql oracle-database
1个回答
0
投票

为了达到预期的结果,您可以使用公共表表达式 (CTE) 来分隔第一个和第二个查询,以便您在过滤第二个查询中的记录时引用第一个查询。以下是修改 SQL 查询的方法:

WITH 
First_Query AS (
    SELECT ESI.item_number, IOQD.subinventory_code AS subinventory_code, 
        ROW_NUMBER() OVER (PARTITION BY ESI.item_number, IOQD.subinventory_code ORDER BY ESI.item_number, IOQD.subinventory_code) AS ivt_seqnum,
        LOC.SEGMENT1 || '-' || LOC.SEGMENT2 || '-' || LOC.SEGMENT3 || '-' || LOC.SEGMENT4 || '-' || LOC.SEGMENT5 AS INV_LOCATOR,
        1 AS priority,
        '1st Query' AS QUERY
    FROM egp_system_items ESI
    JOIN inv_onhand_quantities_detail IOQD ON IOQD.inventory_item_id = ESI.inventory_item_id
    JOIN inv_item_locations LOC ON LOC.inventory_location_id = IOQD.locator_id
    WHERE ESI.INVENTORY_ITEM_ID = 100000110904412
      AND IOQD.subinventory_code = '41OPR'
),
Second_Query AS (
    SELECT ESI.item_number,
        DECODE(SUBSTR(IOQD.secondary_inventory, 1, 3), '41O', '41OPR', IOQD.secondary_inventory) AS subinventory_code,
        ROW_NUMBER() OVER (PARTITION BY ESI.item_number, IOQD.secondary_inventory ORDER BY ESI.item_number, IOQD.secondary_inventory) AS ivt_seqnum,
        NVL((
            SELECT iil.SEGMENT1 || '-' || iil.SEGMENT2 || '-' || iil.SEGMENT3 || '-' || iil.SEGMENT4 || '-' || iil.SEGMENT5
            FROM inv_item_loc_defaults iild
            JOIN inv_item_locations iil ON iild.locator_id = iil.inventory_location_id
            WHERE iild.inventory_item_id = ESI.inventory_item_id
              AND iild.subinventory_code = IOQD.secondary_inventory
              AND ROWNUM = 1
              AND LENGTH(iil.SEGMENT1) >= 1
        ), 'AAAAAAA') AS INV_LOCATOR,
        2 AS priority,
        '2nd Query' AS QUERY
    FROM egp_system_items ESI
    JOIN INV_ITEM_SUB_INVENTORIES IOQD ON IOQD.inventory_item_id = ESI.inventory_item_id
    WHERE ESI.INVENTORY_ITEM_ID = 100000110904412
),
Unioned_Queries AS (
    SELECT * FROM First_Query
    UNION ALL
    SELECT * FROM Second_Query
),
All_Queries AS (
    SELECT item_number, subinventory_code, INV_LOCATOR, QUERY, ivt_seqnum, priority,
        ROW_NUMBER() OVER (PARTITION BY priority, item_number, subinventory_code ORDER BY priority DESC, priority) AS Overall_Priority
    FROM Unioned_Queries
)
SELECT * FROM All_Queries
WHERE NOT (
    priority = 2
    AND INV_LOCATOR = 'AAAAAAA'
    AND EXISTS (
        SELECT 1 FROM First_Query FQ
        WHERE FQ.item_number = All_Queries.item_number
          AND FQ.subinventory_code = All_Queries.subinventory_code
    )
)
© www.soinside.com 2019 - 2024. All rights reserved.