繁体   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