SQL - 从子查询中检索字段

时间:2013-09-27 04:00:39

标签: sql oracle

我正在使用Oracle数据库11g编写脚本。

我目前有几个嵌套的子查询。我需要从其中一个子查询中检索一个字段,但有一些困难。 我的脚本目前是:

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)))

我正在尝试从最后一个嵌套子查询(RA_CUSTOMER_TRX_ALL表)中选择一个字段'LAST_UPDATE_DATE'。 我试图在SELECT部分​​中包含一个select子查询。和FROM部分,但不能将它链接到子查询的最后部分。 有人可以帮忙吗? 干杯, RussH。

1 个答案:

答案 0 :(得分:1)

未经测试的赃物。您可能必须删除重复项,具体取决于您的数据:

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')