在 SQL 中重复使用多个日期的值

Reusing value for multiple dates in SQL

我有一个 table 看起来像这样

ID            Type               Change_Date               
1              t1                2015-10-08
1              t2                2016-01-03
1              t3                2016-03-07
2              t1                2017-12-13
2              t2                2018-02-01

它显示客户是否更改了帐户类型以及更改时间。但是,我想要一个可以给我以下输出的查询

ID            Type               Change_Date               
1              t1                2015-10
1              t1                2015-11
1              t1                2015-12
1              t2                2016-01
1              t2                2016-02
1              t3                2016-03
1              t3                2016-04
...            ...               ...
1              t3                2018-10

每个 ID。输出显示客户在当前月份之前每个月的帐户类型。我的问题是填写 "empty" 个月。在某些情况下,帐户更改之间的间隔可能超过一年。

我希望这是有道理的。

提前致谢。

基于 Presto SQL(因为您的来源问题是关于 Presto/SQL)


2018-11-01更新:使用lead()简化SQL


准备数据

Table mytable 和你的一样

id  type  update_date
1   t1    2015-10-08
1   t2    2016-01-03
1   t3    2016-03-07
2   t1    2017-12-13
2   t2    2018-02-01

Table t_month 是一个字典 table,其中包含从 2015-012019-12 的所有月份数据。这种词典table很有用

ym
2015-01
2015-02
2015-03
2015-04
2015-05
2015-06
2015-07
2015-08
2015-09
...
2019-12

添加 mytable

的生命周期

通常,您应该 'manage' 您的数据就像它们的生命周期一样。所以mytable应该喜欢

id  type   start_date      end_date
1   t1     2015-10-08      2016-01-03
1   t2     2016-01-03      2016-03-07
1   t3     2016-03-07      null
2   t1     2017-12-13      2018-02-01
2   t2     2018-02-01      null

但在这种情况下,您不需要。所以下一步是 'create' 一步。使用 lead() window 函数。

select 
    id,
    type, 
    date_format(update_date, '%Y-%m') as start_month,
    lead(
        date_format(update_date, '%Y-%m'), 
        1, -- next one
        date_format(current_date+interval '1' month, '%Y-%m') -- if null return next month
    ) over(partition by id order by update_date) as end_month
from mytable

输出

id  type  start_month  end_month
1   t1    2015-10     2016-01
1   t2    2016-01     2016-03
1   t3    2016-03     2018-11
2   t1    2017-12     2018-02
2   t2    2018-02     2018-11

交叉连接 idmonth

很简单

with id_month as (
    select * from t_month 
    cross join (select distinct id from mytable)
)
select * from id_month

输出

ym      id
2015-01 1
2015-02 1
2015-03 1
...
2019-12 1
2015-01 2
2015-02 2
2015-03 2
...
2019-12 2

终于

现在,您可以在 select 子句中使用 subquery

select 
    id,
    type,
    ym
from (
    select
        t1.id,
        t1.ym,
        (select type from mytable2 where t1.id = id and t1.ym >= start_month and t1.ym < end_month) as type
    from id_month t1
)
where type is not null
-- order by id, ym

完整 sql

with mytable2 as (
    select 
        id,
        type, 
        date_format(update_date, '%Y-%m') as start_month,
        lead(
            date_format(update_date, '%Y-%m'), 
            1, -- next one
            date_format(current_date+interval '1' month, '%Y-%m') -- if null return next month
        ) over(partition by id order by update_date) as end_month
    from mytable
)
, id_month as (
    select * from t_month 
    cross join (select distinct id from mytable)
)
select 
    id,
    type,
    ym
from (
    select
        t1.id,
        t1.ym,
        (select type from mytable2 where t1.id = id and t1.ym >= start_month and t1.ym < end_month) as type
    from id_month t1
)
where type is not null
--order by id, ym

输出

id  type  ym
1   t1    2015-10
1   t1    2015-11
1   t1    2015-12
1   t2    2016-01
1   t2    2016-02
1   t3    2016-03
1   t3    2016-04
...
1   t3    2018-10
2   t1    2017-12
2   t1    2018-01
2   t2    2018-02
...
2   t2    2018-10