使用last non null连接并填充NULL值

时间:2017-02-28 20:00:38

标签: sql join teradata

我一直在尝试加入,起初我认为相对简单,但我现在遇到一些麻烦,让它完全正确。我有两组数据类似于以下

<link href="https://cdn.jsdelivr.net/jquery.tooltipster/4.2.2/css/tooltipster.bundle.min.css" rel="stylesheet"/>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<script src="https://cdn.jsdelivr.net/jquery.tooltipster/4.2.2/js/tooltipster.bundle.min.js"></script>

<div style="text-align:center;">
<h1 id="myPageTitle">My Page Title</h1>
<br>
<br>
<a href="http://stackoverflow.com" target="_blank" class="coolLink">StackOverflow</a><br>
<br>
<a href="http://iamceege.github.io/tooltipster/#options" target="_blank" class="coolLink">ToolTipster options</a><br>
<br>
<br>
<a href="http://endoftheinternet.com/" target="_blank" id="eoi">End of Internet</a><br>
</div>

这是我想要的输出

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

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

3 个答案:

答案 0 :(得分:0)

union all + last_value

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 |            |
+----+------------+------------+

答案 1 :(得分:0)

min(...)over(......以下1和1之间的行)* + join
* = LEAD

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 :(得分:0)

选择相关查询

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 |            |
+----+------------+------------+