【问题标题】:How to select values from two different tables when certain conditions are met满足某些条件时如何从两个不同的表中选择值
【发布时间】:2021-12-07 18:52:39
【问题描述】:

我有一张桌子:

DROP TABLE TBL_A;

CREATE TABLE TBL_A
(
    number_id int, 
    country varchar(50), 
    status varchar(50), 
    number_of_days int,
    datetime date
);

INSERT INTO TBL_A
VALUES (121144, 'USA', 'CLICKED',2, '2021-10-09'),
       (121144, 'USA', 'CLAIMED',2, '2021-10-10'),
       (121144, 'USA', 'BOUGHT',2, '2021-10-11'),
       (121111, 'CAD', 'CLICKED',3, '2021-10-09'),
       (121111, 'CAD', 'CLAIMED',3, '2021-10-10'),
       (121111, 'CAD', 'BOUGHT',3, '2021-10-11'),
       (121133, 'AUS', 'CLICKED',5, '2021-10-09'),
       (121133, 'AUS', 'CLAIMED',5, '2021-10-10'),
       (121133, 'AUS', 'BOUGHT',5, '2021-10-11');

我还有一张桌子:

DROP TABLE TBL_B;

CREATE TABLE TBL_B
(
    number_id int, 
    country varchar(50), 
    status varchar(50), 
    number_of_days int,
    datetime date
);

INSERT INTO TBL_B
VALUES (121144, 'USA', 'CLICKED',6, '2021-10-20'),
       (121111, 'CAD', 'BOUGHT',10, '2021-10-21'),
       (121133, 'AUS', 'CLAIMED',5, '2021-10-02');

我想从 TBL_A 中选择所有内容,但如果在 TBL_B 中找到相同的 number_id 和 status,我只想在 datetime 较高时选择 TBL_B 中的值。

有没有办法做到这一点?在上面的示例中,只有 USA&CLICKED 和 CAD&BOUGHT 应该“更新”,因为它们在第二个表中的值具有更大的日期时间,而其余记录应该来自第一个表。

这是我目前所拥有的,但无法完成它:

select 
    number_id, 
    country, 
    status, 
    number_of_days,
    datetime date
from 
    TBL_A A
left join
    (select 
         number_id, 
         country, 
         status, 
         number_of_days,
         datetime date
     from
         TBL_B) on A.NUMBER_ID = B.NUMBER_ID 
                and a.STATUS = b.STATUS

【问题讨论】:

    标签: sql snowflake-cloud-data-platform


    【解决方案1】:

    此查询只为您提供结果,当 ID、Status 在两个表中并且表“TBL_B”中的日期大于表“TBL_A”中的等效日期时

    SELECT
     B.*    --everything from B according to WHERE restrict
    FROM
     TBL_A as A,
     TBL_B as B 
    WHERE
     A.number_id = B.number_id 
     AND A.status = B.status 
     AND A.datetime < B.datetime 
    

    当您还需要 TBL_A 的结果时,可以使用 sql 集合运算符(例如 MINUS 和 UNION)扩展此查询。

    https://en.wikipedia.org/wiki/Set_operations_(SQL)

    (
    SELECT
     A.*    
    FROM
     TBL_A as A
    MINUS  --everything from "A", which is not included in "B"
    SELECT
     B.*
    FROM
     TBL_A as A,
     TBL_B as B 
    WHERE
     A.number_id = B.number_id 
     AND A.status = B.status 
     AND A.datetime < B.datetime 
    )
    UNION ALL  -- Add the "B" results
    SELECT
     B.*    -- everything from "B" according to WHERE restrict
    FROM
     TBL_A as A,
     TBL_B as B 
    WHERE
     A.number_id = B.number_id 
     AND A.status = B.status 
     AND A.datetime < B.datetime 
    ;
    

    【讨论】:

    • 您好,感谢您的回复!是的,我需要 TBL_A 的所有结果。基本上,我需要来自 TBL_A 的所有结果,并且只需要来自 TBL_B 的结果,其中在 TBL_A 中找到的数字和状态也在 TBL_B 中找到,并且在 TBL_B 中的日期时间更大。你认为你可以帮助这部分吗?我在编写查询时遇到了一些问题。
    • 使用该方案的最佳方式是使用日期限制数据总量。此查询可能会给数据库带来沉重的负担。 ?
    • 鉴于select from b where not same as a 不应与select from a 重叠,因此union 应为union all 以避免'union 的重复数据删除步骤
    猜你喜欢
    • 2020-08-17
    • 1970-01-01
    • 1970-01-01
    • 2014-07-17
    • 2014-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-29
    相关资源
    最近更新 更多