合并日期时间绑定数据集

时间:2018-04-04 13:59:29

标签: sql sql-server

我有一个如下所示的数据集:

InstrID StartDate               EndDate                 RatingName  RatingValue
1234    2014-09-05 00:00:00.000 2016-06-26 23:59:59.000 FitchIntLT  AA+
1234    2014-09-05 00:00:00.000 2017-09-21 23:59:59.000 MoodyIntLT  Aa1
1234    2014-09-05 00:00:00.000 NULL                    SPIntLT     NR
1234    2016-06-27 00:00:00.000 NULL                    FitchIntLT  AA
1234    2017-09-22 00:00:00.000 NULL                    MoodyIntLT  Aa2

每当RatingName更改时,上表基本上都会更新。将填充EndDate并使用新的RatingValue创建新记录。最新记录始终是EndDate为空的记录。

我想将此数据集合并到以下内容中:

InstrID StartDate               EndDate                 FitchIntLT   MoodyIntLT   SPIntLT 
1234    2014-09-05 00:00:00.000 2016-06-26 23:59:59.000 AA+          Aa1          NR
1234    2016-06-27 00:00:00.000 2017-09-21 23:59:59.000 AA           Aa1          NR
1234    2017-09-22 00:00:00.000 NULL                    AA           Aa2          NR

我想知道这样做的最佳方法是什么?或者甚至可能吗?

3 个答案:

答案 0 :(得分:0)

我认为你可以使用条件聚合来做到这一点:

select InstrID, StartDate, max(EndDate) as EndDate,
       max(case when RatingName = 'FitchIntLT' then RatingValue end) as FitchIntLT,
       max(case when RatingName = 'MoodyIntLT' then RatingValue end) as MoodyIntLT,
       max(case when RatingName = 'SPIntLT' then RatingValue end) as SPIntLT
from t
group by InstrID, StartDate;

这并不会产生完全相同的结束日期,但这可能就是您想要的。

答案 1 :(得分:0)

好的,请看看:

create table #temp (instrid int, startdate datetime, enddate datetime, ratingname varchar(max), ratingvalue varchar(max))
insert #temp values
(1234,    '2014-09-05 00:00:00.000', '2016-06-26 23:59:59.000', 'FitchIntLT',  'AA+'),
(1234,    '2014-09-05 00:00:00.000', '2017-09-21 23:59:59.000', 'MoodyIntLT',  'Aa1'),
(1234,    '2014-09-05 00:00:00.000', NULL                    ,'SPIntLT',     'NR'),
(1234,    '2016-06-27 00:00:00.000', NULL                    ,'FitchIntLT',  'AA'),
(1234,    '2017-09-22 00:00:00.000', NULL                    ,'MoodyIntLT',  'Aa2')

declare @holding table (rowid int identity, instrid int, startdate datetime, enddate datetime, ratingname varchar(max), ratingvalue varchar(max)--, position int
)
insert @holding
 select * from #temp order by startdate, ratingname

declare @temp table (tempid int identity, instrid int, startdate datetime, enddate datetime,  ratingname varchar(max), ratingvalue varchar(max))
insert @temp 
 select distinct a.instrid, a.startdate, null enddate, b.ratingname, null ratingvalue  from @holding a cross join @holding b
 order by b.ratingname, a.startdate 

declare @iterator int=1, @inneriterator int=1, @instrid int, @ratingname varchar(max), @startdate datetime, @enddate datetime, @ratingvalue varchar(max)
while @iterator<=(Select max(rowid) from @holding)
begin
select @instrid=instrid, @ratingname=ratingname, @startdate=startdate, @enddate=isnull(enddate, @enddate), 
@ratingvalue=ratingvalue
from @holding where rowid=@iterator

while @inneriterator<=(Select max(tempid) from @temp)
begin


update @temp set ratingvalue=@ratingvalue where  tempid=@inneriterator and 
ratingname=@ratingname and startdate>=@startdate and instrid=@instrid


set @inneriterator=@inneriterator+1
end 
set @inneriterator=1
set @iterator=@iterator+1
end 


select instrid, startdate, null enddate,  max(FitchIntLT)FitchIntLT, 
 max(MoodyIntLT)MoodyIntLT, max(SPIntLT)SPIntLT into #temp2 
 from @temp pivot ( max(ratingvalue) for ratingname in ([FitchIntLT],[MoodyIntLT],[SPIntLT]))a
 group by instrid, startdate 


 select  instrid,  startdate, lead(enddate,1,null) over(order by  instrid,  startdate) enddate, FitchIntLT, MoodyIntLT, SPIntLT from (
 select  instrid,  startdate, max(enddate)enddate, FitchIntLT, MoodyIntLT, SPIntLT
 from 
 (select a.instrid, a.startdate, FitchIntLT, MoodyIntLT, SPIntLT,
 case
 when 
 (isnull(lag(FitchIntLT,1,null) over(order by a.instrid, a.startdate),'')<>FitchIntLT 
 or 
     isnull(lag(MoodyIntLT,1,null) over(order by a.instrid, a.startdate),'')<>MoodyIntLT
     or
     isnull(lag(SPIntLT,1,null) over(order by a.instrid, a.startdate),'')<>SPIntLT ) and 
 lag(a.instrid, 1,null) over (order by a.instrid, a.startdate)=a.instrid
     then dateadd(s,-1, a.startdate) end enddate
      from #temp2 a
      join @holding b on a.instrid=b.instrid and a.startdate=b.startdate)b
      group by instrid,  startdate, FitchIntLT, MoodyIntLT, SPIntLT )c
      order by instrid,  startdate

  drop table #temp 
  drop table #temp2 

答案 2 :(得分:0)

感谢您的所有回复,但我设法找到了一个更简单的解决方案,代码如下:

<?php

     $custom_terms = get_terms('custom_taxonomy_name');

foreach($custom_terms as $custom_term) {
    wp_reset_query();
    $args = array('post_type' => 'custom_post_type_name',
        'tax_query' => array(
            array(
                'taxonomy' => 'custom_taxonomy_name',
                'field' => 'slug',
                'terms' => $custom_term->slug,
            ),
        ),
     );

     $loop = new WP_Query($args);
     if($loop->have_posts()) {
        echo '<h2>'.$custom_term->name.'</h2>';

        while($loop->have_posts()) : $loop->the_post();
            echo '<a href="'.get_permalink().'">'.get_the_title().'</a><br>';
        endwhile;
     }
}
?>