簡體   English   中英

如何編寫 Oracle Sql 查詢組合多個表

[英]How to write Oracle Sql Query combining multiple tables

我有 4 個表,分別稱為 A、B、C、C_History。 以下是每個表包含的內容。 cust_id 是所有表的通用標識符。

A - cust_id,status,cust_type

B - cust_id、acnt_nmbr、acnt_systm

C - cust_id,email,mod_date

C_History - cust_id,email,mod_date(與 C 相同,用於插入在 C 中刪除/更新的行)。

我需要編寫一個查詢,結合上述所有表來提取 A.cust_id、B.acnt_nmbr、C.email(作為新電子郵件)、C_History(作為舊電子郵件)。

這里需要注意的是,A 和 B 表將只有一個 cust_id 記錄,但 C 和 C_History 可以有多個 email 記錄作為 cust_id。

所以從 C 表中,我只需要一個 email ,它是使用 mod_date (TIMESTAMP 數據類型)最新的,

And from C_Hisory also, i just need single latest email using mod_date, but one more condition is that, whichever latest email picked from C_History, should not be same as the latest email picked from C.

請幫助解決這個問題,因為我從 2 天以來一直在努力解決這個問題。

提前感謝傳說。

SELECT A.cust_id
    ,B.acnt_nmbr
    ,C.email
    ,C_History.email
FROM A
    ,B
    ,(
        SELECT cust_id
            ,MAX(email) KEEP (
                DENSE_RANK LAST ORDER BY mod_date
                ) OVER (PARTITION BY cust_id)
        FROM c
        ) c
    ,(
        SELECT cust_id
            ,MAX(email) KEEP (
                DENSE_RANK LAST ORDER BY mod_date
                ) OVER (PARTITION BY cust_id)
        FROM c_history
        ) c_history
WHERE a.cust_id = b.cust_id
    AND c_history.cust_id = a.cust_id
    AND c.cust_id = a.cust_id

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM