【问题标题】:How to do a join to get previous days values?如何进行连接以获取前几天的值?
【发布时间】:2021-03-19 14:55:13
【问题描述】:

我想加入如下表格:

Report_Period Entity Tag Users Count Report_Period_M-1 Report_Period_D-1 ...
2021-03-31 entity 1 X 471 2017-05-31 2021-03-18 ...
2020-12-31 entity 2 A 135 2020-11-30 2021-03-18 ...
2020-11-30 entity 3 X 402 2020-10-31 2021-03-18 ...

包含第 1 天结果的视图如下:

Report_Period Entity Tag Users Count Report_Period_D-1
2021-03-31 entity 1 X 445 2021-03-18
2021-03-31 entity 2 A 135 2021-03-18
2021-03-31 entity 3 X 402 2021-03-18

我的目标是至少返回这样的结果:

Report_Period Entity Tag Users Count Users Count D-1 Report_Period_M-1 ...
2021-03-31 entity 1 X 471 445 2021-02-28 ...
2020-12-31 entity 2 A 135 NULL 2020-11-30 ...
2020-11-30 entity 3 X 402 NULL 2020-10-31 ...

如果我在 Report_Period、Entity & Tag 上使用经典联接, 我将只返回包含报告期 2021-03-31 的结果行。

是否可以替换当前期间的用户计数值? 如果我想用当月的用户数 D-1 替换用户数值?

欢迎您的帮助,感谢您的宝贵时间!

【问题讨论】:

    标签: sql join snowflake-cloud-data-platform


    【解决方案1】:

    根据您的示例,您似乎想要一个 LEFT JOIN

    WITH table_a(report_period, entity, tag, users_count, report_period_m1, report_period_d1) AS (
        SELECT * FROM VALUES
        ('2021-03-31', 'entity 1','X',471,'2017-05-31','2021-03-18'),
        ('2020-12-31', 'entity 2','A',135,'2020-11-30','2021-03-18'),
        ('2020-11-30', 'entity 3','X',402,'2020-10-31','2021-03-18')
    ), table_b(report_period, entity, tag, users_count, report_period_d1) AS (
        SELECT * FROM VALUES
        ('2021-03-31','entity 1','X',445, '2021-03-18'),
        ('2021-03-31','entity 2','A',135, '2021-03-18'),
        ('2021-03-31','entity 3','X',402, '2021-03-18')
    )
    SELECT a.*
        ,b.*
    FROM table_a AS a
    LEFT JOIN table_b AS b
        ON a.report_period = b.report_period AND a.entity = b.entity
    ORDER BY 1 desc, 2;
    

    给予:

    REPORT_PERIOD   ENTITY      TAG   USERS_COUNT   REPORT_PERIOD_M1    REPORT_PERIOD_D1    REPORT_PERIOD   ENTITY      TAG   USERS_COUNT   REPORT_PERIOD_D1
    2021-03-31      entity 1    X     471           2017-05-31          2021-03-18          2021-03-31      entity 1    X     445           2021-03-18
    2020-12-31      entity 2    A     135           2020-11-30          2021-03-18          null            null        null  null           null
    2020-11-30      entity 3    X     402           2020-10-31          2021-03-18          null            null        null  null           null
    

    但是对于“替换值”的声明,我可以假设您的意思是,当b 不匹配时,您想将b.users_counta.users_count 合并,因此代码变为..

    WITH table_a(report_period, entity, tag, users_count, report_period_m1, report_period_d1) AS (
        SELECT * FROM VALUES
        ('2021-03-31', 'entity 1','X',471,'2017-05-31','2021-03-18'),
        ('2020-12-31', 'entity 2','A',135,'2020-11-30','2021-03-18'),
        ('2020-11-30', 'entity 3','X',402,'2020-10-31','2021-03-18')
    ), table_b(report_period, entity, tag, users_count, report_period_d1) AS (
        SELECT * FROM VALUES
    
        ('2021-03-31','entity 1','X',445, '2021-03-18'),
        ('2021-03-31','entity 2','A',135, '2021-03-18'),
        ('2021-03-31','entity 3','X',402, '2021-03-18')
    )
    SELECT a.*
        ,COALESCE(b.USERS_COUNT, a.USERS_COUNT) as b_USERS_COUNT
    FROM table_a AS a
    LEFT JOIN table_b AS b
        ON a.report_period = b.report_period AND a.entity = b.entity
    ORDER BY 1,2;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-05
      • 2019-06-01
      • 2014-04-29
      • 2016-01-20
      • 1970-01-01
      • 2020-08-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多