将具有不同日期的 2 个表连接到一个日期列中

join 2 tables with different dates into one date column

我有两个表:a_table 和 b_table。它们包含关闭记录和结帐记录,可以在不同日期为每个客户执行。我想将这2个表合并在一起,这样就只有一个日期字段,一个客户字段,一个关闭字段和一个检查字段。

a_table

time_modified         customer_name   
2021-05-03                Ben      
2021-05-08                Ben
2021-07-10                Jerry

b_table

time_modified account_id
2021-05-06        Ben
2021-07-08        Jerry
2021-07-12        Jerry

预期结果

date            account_id_a   close   check
2021-05-03          Ben          1       0
2021-05-06          Ben          0       1
2021-05-08          Ben          1       0
2021-07-08         Jerry         0       1
2021-07-10         Jerry         1       1
2021-07-12         Jerry         0       1

目前查询:

with a_table as ( 
    select rz.time_modified::date, rz.customer_name,
    case when rz.time_modified::date is not null then 1 else 0 end as close
    from schema.rz
                    ),
    b_table as (
    select bo.time_modified::date, bo.customer_name,
    case when bo.time_modified::date is not null then 1 else 0 end as check
    from schema.bo
                    )
SELECT (CURRENT_DATE::TIMESTAMP  - (i * interval '1 day'))::date as date,
        a.*, b.*
FROM generate_series(1,2847) i 
left join a_table a  
    on a.time_modified = i.date 
left join b_table b
    on b.time_modified = i.date

上面的查询returns:

SQL Error [500310] [0A000]: [Amazon](500310) Invalid operation: Specified types or functions (one per INFO message) not supported on Redshift tables.;

你只需要做一个联合而不是一个连接。 Join 将两个 table 合并为一个,其中 union 将第二个 table 添加到第一个

首先,您遇到的错误是由于在查询中使用了 generate_series() 函数,其结果需要与 table 数据相结合。 Generate_series() 是一个仅限前导节点的函数,其结果不能用于计算节点。您将需要以另一种方式生成您想要的数字系列。请参阅 How to Generate Date Series in Redshift 了解可能的方法。

我不确定我是否完全遵循了您的查询,但您似乎想要 UNION tables 而不是 JOIN 它们。您还没有定义 rz 和 bo 是什么,所以有点混乱。然而 UNION 和一些关闭和检查的计算似乎是可行的方法