【问题标题】:Conditional Joins | SQL | Optimizing performance and reduce no. of JOINS条件连接 | SQL |优化性能并减少数量。加入数
【发布时间】:2021-08-10 17:08:04
【问题描述】:

我有以下表格:

t1:

create table t1 (auth_date string,
                 auth_nbr int,
                 debit_date string,
                 debit_nbr int,
                 txn_dttm string,
                 iss_id string,
                 audit_num int);

insert into t1 values 
('01-05-2021',12,null,null,'01-05-2021','b',124),
(null,null,'02-05-2021',13,'02-05-2021','c',125),
('02-05-2021',14,'02-05-2021',14,'02-05-2021','d',126);

t2:

create table t2 (txn_amt int,txn_dttm string,iss_id string,audit_num int);

insert into t2 values 
(2000,'01-05-2021','b',124),
(2500,'02-05-2021','c',125),
(1000,'02-05-2021','d',126);

我想要我的输出:

dw_date|dw_nbr|amt
01-05-2021|12|2000
02-05-2021|13|2500
02-05-2021|14|1000

我有以下查询。但由于加入是一项繁重的操作,我只想将其缩小到 1 个加入。但是我在这两种情况下的加入是不同的条件,即在第一个条件中加入是在 3 个列上,而在另一个加入是在 2 个列上。我想找到一种方法来实现所需的输出。条件连接。或者别的什么。 从t2表中,我只需要相关的txn_amt。

select dw_date,dw_nbr,amt 
from (select t1.auth_date,t1.auth_nbr,t2.amt from t1 join t2 on t1.txn_dttm=t2.txn_dttm and t1.iss_id=t2.iss_id and t1.audit_num=t2.audit_num 
where auth_date is not null and auth_nbr is not null and debit_date is null and debit_nbr is null)a 
union all 
select dw_date,dw_nbr,amt 
from (select t1.debit_date,t1.debit_nbr,t2.amt from t1 join t2 on t1.txn_dttm=t2.txn_dttm and t1.audit_num=t2.audit_num 
where debit_date is not null and debit_nbr is not null)a;

【问题讨论】:

    标签: sql join impala


    【解决方案1】:

    请检查这个。由于使用的是最新版本的 MYSQL,因此请使用 CTE 以避免多次使用大表。

    -- MySQL(v5.8)
    WITH a_cte AS (
        SELECT t1.auth_date, t1.auth_nbr, t2.txn_amt
             , t1.debit_date, t1.debit_nbr
             , CASE WHEN t1.iss_id = t2.iss_id
                       THEN 1
                    ELSE 0
               END iss_id
        FROM t1
        INNER JOIN t2
                ON t1.txn_dttm = t2.txn_dttm 
               AND t1.audit_num = t2.audit_num 
    ), b_cte AS (
        SELECT auth_date AS dw_date
             , auth_nbr AS dw_nbr
             , txn_amt AS amt
        FROM a_cte
        WHERE iss_id = 1
            AND auth_date is not null 
            AND auth_nbr is not null 
            AND debit_date is null 
            AND debit_nbr is null
        
        UNION ALL
        
        SELECT debit_date AS dw_date
             , debit_nbr AS dw_nbr
             , txn_amt AS amt
        FROM a_cte
        WHERE debit_date is not null 
            AND debit_nbr is not null 
    )
    SELECT *
    FROM b_cte;
    

    请查看网址https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=1a482589e2cdc1f6b288c93f88150fa5

    【讨论】:

    • 嗨@AnukritiSingh,请检查这个。希望你能得到你想要的结果,也能得到更好的表现。如果对此有任何疑问,请告诉我。
    • 问题被标记为 impala,而不是 mysql。你确定这是一个正确的答案吗?
    • @RahulBiswas。感谢你的回答。我会验证并通知您。
    • @AnukritiSingh 我的回答对你有帮助吗?
    • @RahulBiswas,是的,它帮助了我。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-09
    相关资源
    最近更新 更多