【问题标题】:Join and fill down NULL values with last non null加入最后一个非空值并填充空值
【发布时间】:2017-07-19 22:50:37
【问题描述】:

我一直在尝试加入,起初我认为它相对简单,但现在很难完全正确。我有两组类似于以下的数据

ID | stmt_dt             ID | renewal_dt
   --                       --
1 |1/31/15                1 | 2/28/15
1 |2/28/15                1 | 4/30/15
1 |3/31/15                2 | 2/28/15
1 |4/30/15                3 | 1/31/15
1 |5/31/15                
2 |1/31/15
2 |2/28/15
2 |3/31/15
2 |4/30/15
2 |5/31/15
3 |1/31/15
3 |2/28/15
3 |3/31/15
3 |4/30/15
3 |5/31/15
4 |1/31/15
4 |2/28/15
4 |3/31/15
4 |4/30/15
4 |5/31/15

这是我想要的输出

ID | stmt_dt | renewal_dt
   --         
1 |1/31/15   | NA
1 |2/28/15   | 2/28/15          
1 |3/31/15   | 2/28/15         
1 |4/30/15   | 4/30/15          
1 |5/31/15   |  4/30/15           
2 |1/31/15   |  NA
2 |2/28/15   |  2/28/15
2 |3/31/15   |  2/28/15
2 |4/30/15   |  2/28/15
2 |5/31/15   |  2/28/15
3 |1/31/15   |  1/31/15
3 |2/28/15   |  1/31/15
3 |3/31/15   | 1/31/15
3 |4/30/15   |  1/31/15
3 |5/31/15   |  1/31/15
4 |1/31/15   |  NA
4 |2/28/15   |  NA
4 |3/31/15   |  NA
4 |4/30/15   |  NA
4 |5/31/15   |  NA

我最大的问题是让合并的值填充到每个组中的下一个非空值。关于如何实现这种加入的任何想法?谢谢!

【问题讨论】:

  • 尝试更好的解释。
  • 试试LAST_VALUE(renewal_dt IGNORE NULLS) OVER (PARTITION BY ID ORDER BY stmt_dt)
  • @dnoeth 做到了。谢谢!

标签: sql join teradata


【解决方案1】:

min(...) over (... rows between 1 following and 1 following)* + join
* = 领先

select  s.ID
       ,s.stmt_dt
       ,r.renewal_dt

from                stmt    s

        left join  (select  ID
                           ,renewal_dt                              

                           ,min (renewal_dt) over
                            (
                                partition by    ID
                                order by        renewal_dt
                                rows            between 1 following 
                                                and     1 following
                            ) as next_renewal_dt

                    from    renewal
                    ) r

        on          s.ID        =   r.ID

                and s.stmt_dt   >=  r.renewal_dt
                and s.stmt_dt   <   coalesce (r.next_renewal_dt,date '9999-01-01')

order by    s.ID
           ,s.stmt_dt

+----+------------+------------+
| ID |  stmt_dt   | renewal_dt |
+----+------------+------------+
|  1 | 2015-01-31 |            |
|  1 | 2015-02-28 | 2015-02-28 |
|  1 | 2015-03-31 | 2015-02-28 |
|  1 | 2015-04-30 | 2015-04-30 |
|  1 | 2015-05-31 | 2015-04-30 |
|  2 | 2015-01-31 |            |
|  2 | 2015-02-28 | 2015-02-28 |
|  2 | 2015-03-31 | 2015-02-28 |
|  2 | 2015-04-30 | 2015-02-28 |
|  2 | 2015-05-31 | 2015-02-28 |
|  3 | 2015-01-31 | 2015-01-31 |
|  3 | 2015-02-28 | 2015-01-31 |
|  3 | 2015-03-31 | 2015-01-31 |
|  3 | 2015-04-30 | 2015-01-31 |
|  3 | 2015-05-31 | 2015-01-31 |
|  4 | 2015-01-31 |            |
|  4 | 2015-02-28 |            |
|  4 | 2015-03-31 |            |
|  4 | 2015-04-30 |            |
|  4 | 2015-05-31 |            |
+----+------------+------------+

【讨论】:

    【解决方案2】:

    联合所有+最后一个值

    select      ID
               ,dt  as stmt_dt
    
               ,last_value (case when tab = 'R' then dt end ignore nulls) over
                (
                    partition by    id
                    order by        dt  
                                   ,case tab when 'R' then 1 else 2 end
                )   as renewal_dt
    
    from        (           select 'S',ID,stmt_dt    from stmt
                union all   select 'R',ID,renewal_dt from renewal
                ) as t (tab,ID,dt)
    
    
    qualify     tab = 'S'
    
    order by    ID
               ,stmt_dt
    

    +----+------------+------------+
    | ID |  stmt_dt   | renewal_dt |
    +----+------------+------------+
    |  1 | 2015-01-31 |            |
    |  1 | 2015-02-28 | 2015-02-28 |
    |  1 | 2015-03-31 | 2015-02-28 |
    |  1 | 2015-04-30 | 2015-04-30 |
    |  1 | 2015-05-31 | 2015-04-30 |
    |  2 | 2015-01-31 |            |
    |  2 | 2015-02-28 | 2015-02-28 |
    |  2 | 2015-03-31 | 2015-02-28 |
    |  2 | 2015-04-30 | 2015-02-28 |
    |  2 | 2015-05-31 | 2015-02-28 |
    |  3 | 2015-01-31 | 2015-01-31 |
    |  3 | 2015-02-28 | 2015-01-31 |
    |  3 | 2015-03-31 | 2015-01-31 |
    |  3 | 2015-04-30 | 2015-01-31 |
    |  3 | 2015-05-31 | 2015-01-31 |
    |  4 | 2015-01-31 |            |
    |  4 | 2015-02-28 |            |
    |  4 | 2015-03-31 |            |
    |  4 | 2015-04-30 |            |
    |  4 | 2015-05-31 |            |
    +----+------------+------------+
    

    【讨论】:

      【解决方案3】:

      选择相关查询

      select      s.ID
                 ,s.stmt_dt
      
                 ,(   
                      select      max (r.renewal_dt)
                      from        renewal r
                      where       r.ID = s.ID
                              and r.renewal_dt <= s.stmt_dt   
                  )   as renewal_dt
      
      from        stmt    s
      
      order by    ID
                 ,stmt_dt
      

      +----+------------+------------+
      | ID |  stmt_dt   | renewal_dt |
      +----+------------+------------+
      |  1 | 2015-01-31 |            |
      |  1 | 2015-02-28 | 2015-02-28 |
      |  1 | 2015-03-31 | 2015-02-28 |
      |  1 | 2015-04-30 | 2015-04-30 |
      |  1 | 2015-05-31 | 2015-04-30 |
      |  2 | 2015-01-31 |            |
      |  2 | 2015-02-28 | 2015-02-28 |
      |  2 | 2015-03-31 | 2015-02-28 |
      |  2 | 2015-04-30 | 2015-02-28 |
      |  2 | 2015-05-31 | 2015-02-28 |
      |  3 | 2015-01-31 | 2015-01-31 |
      |  3 | 2015-02-28 | 2015-01-31 |
      |  3 | 2015-03-31 | 2015-01-31 |
      |  3 | 2015-04-30 | 2015-01-31 |
      |  3 | 2015-05-31 | 2015-01-31 |
      |  4 | 2015-01-31 |            |
      |  4 | 2015-02-28 |            |
      |  4 | 2015-03-31 |            |
      |  4 | 2015-04-30 |            |
      |  4 | 2015-05-31 |            |
      +----+------------+------------+
      

      【讨论】:

        猜你喜欢
        • 2015-07-22
        • 2015-09-17
        • 2021-10-25
        • 1970-01-01
        • 2021-10-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-05
        相关资源
        最近更新 更多