简体   繁体   中英

Oracle SQL : using sub query

select 
    itoh.header_number Transfer_Order, 
    itol.SHIPPED_QTY , 
    itol.REQUESTED_QTY, 
    itol.RECEIVED_QTY,
    itol.DELIVERED_QTY,
    itol.SOURCE_SUBINVENTORY_CODE Transfer_subiinventory 
    (select DISTINCT (esi.ITEM_NUMBER and esi.item_type)
     from egp_system_items_b esi 
     where INVENTORY_ITEM_ID = itol.INVENTORY_ITEM_ID) 
From 
    inv_transfer_order_headers itoh, inv_transfer_order_lines itol 
where 
    itoh.HEADER_ID = itol.HEADER_ID

It is always helpful to provide some sort of DDL and sample data so that the problem can be demo'ed. It's also useful to provide your full version number, some features are only available in recent versions, I will assume you are using at least version 12.2 (as that is the current latest supported version)

This is only necessary if you do not want to distinct against all of the columns in your result set. You can use a lateral join to join to a correlated subquery:

select 
    itoh.header_number Transfer_Order, 
    itol.SHIPPED_QTY , 
    itol.REQUESTED_QTY, 
    itol.RECEIVED_QTY,
    itol.DELIVERED_QTY,
    itol.SOURCE_SUBINVENTORY_CODE Transfer_subiinventory,
    esi_subq.ITEM_NUMBER,
    esi_subq.item_type
From 
    inv_transfer_order_headers itoh, inv_transfer_order_lines itol 
  , lateral
    (select DISTINCT esi.ITEM_NUMBER , esi.item_type
     from egp_system_items_b esi 
     where INVENTORY_ITEM_ID = itol.INVENTORY_ITEM_ID
    ) esi_subq
where 
    itoh.HEADER_ID = itol.HEADER_ID

I've made a small dbfiddle to demo

You are also free to use the newer join syntax but you'll need to use different keywords.

select 
    itoh.header_number Transfer_Order, 
    itol.SHIPPED_QTY , 
    itol.REQUESTED_QTY, 
    itol.RECEIVED_QTY,
    itol.DELIVERED_QTY,
    itol.SOURCE_SUBINVENTORY_CODE Transfer_subiinventory,
    esi_subq.ITEM_NUMBER,
    esi_subq.item_type
From 
     inv_transfer_order_headers itoh
join inv_transfer_order_lines itol 
  on itoh.HEADER_ID = itol.HEADER_ID
outer apply 
    (select DISTINCT esi.ITEM_NUMBER , esi.item_type
     from egp_system_items_b esi 
     where INVENTORY_ITEM_ID = itol.INVENTORY_ITEM_ID
    ) esi_subq

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM