简体   繁体   中英

SQL - Retrieve field from sub-query

I am writing a script using Oracle database 11g.

I currently have several nested sub-queries. I need to retrieve a field from one of these sub-queries, but having some difficulty. My script currently is:

SELECT  a.PARTY_ID, a.PARTY_NAME, a.STATUS
        ,a.OBJECT_VERSION_NUMBER, a.PARTY_NUMBER
FROM    HZ_PARTIES a
WHERE   a.PARTY_TYPE = 'ORGANIZATION'
AND     a.STATUS = 'A' and a.party_id = 4402
AND     a.CREATED_BY_MODULE IN ('HZ_CPUI','TCA_V1_API','TCA_FORM_WRAPPER')
AND     (a.PARTY_ID IN (SELECT b.PARTY_ID FROM HZ_CUST_ACCOUNTS b
           WHERE b.CUSTOMER_TYPE = 'R'
           AND b.CUST_ACCOUNT_ID IN (SELECT c.BILL_TO_CUSTOMER_ID FROM RA_CUSTOMER_TRX_ALL c
                    WHERE c.BILL_TO_CUSTOMER_ID IS NOT NULL
                    AND c.LAST_UPDATE_DATE < SYSDATE-100)))

I am trying to select a field 'LAST_UPDATE_DATE' from the last of the nested sub-queries (the RA_CUSTOMER_TRX_ALL table). I've tried to include a select sub-query in the SELECT section. and the FROM section, but cannot link it into the last part of the sub-query. Can anyone assist? Cheers, RussH.

An untested swag at it. You may have to remove duplicates depending on your data:

SELECT  a.PARTY_ID, a.PARTY_NAME, a.STATUS
        ,a.OBJECT_VERSION_NUMBER, a.PARTY_NUMBER, c.last_update_date
FROM    HZ_PARTIES a JOIN hz_cust_accounts b 
                       ON b.customer_type = 'R' 
                          AND a.party_id = b.party_id
                     JOIN ra_customer_trx_all c 
                       ON b.cust_account_id = c.bill_to_customer_id
                          AND c.last_update_date < SYSDATE-100
WHERE   a.PARTY_TYPE = 'ORGANIZATION'
AND     a.STATUS = 'A' and a.party_id = 4402
AND     a.CREATED_BY_MODULE IN ('HZ_CPUI','TCA_V1_API','TCA_FORM_WRAPPER')

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