3

SCD テーブルの 1 日あたりの新規/変更された行数を示すレポートを生成する必要があります。

テーブルを作成するSQLは次のとおりです。

create table #scd(
  code      nvarchar not null
, startdate date     not null
, enddate   date
);
alter table #scd add constraint pk_scd primary key (code, startdate);


insert into #scd values
 ('A', '2012-06-01', '2012-06-02')
,('B', '2012-06-01', '2012-06-02')
,('A', '2012-06-02', '2012-06-03')
,('B', '2012-06-02', '2012-06-04')
,('A', '2012-06-03', '2012-06-04')
,('A', '2012-06-04', null)
,('B', '2012-06-04', null)
,('C', '2012-06-04', null)

select * from #scd

結果は次のようになります。

code    startdate   enddate
A   2012-06-01  2012-06-02
B   2012-06-01  2012-06-02
A   2012-06-02  2012-06-03
B   2012-06-02  2012-06-04
A   2012-06-03  2012-06-04
A   2012-06-04  NULL
B   2012-06-04  NULL
C   2012-06-04  NULL

今、私はこのような何かを生成する必要があります:

date            new changed
2012-06-01      2   0
2012-06-02      0   2
2012-06-03      0   1
2012-06-04      1   2

どんな助けでも大歓迎です。

4

2 に答える 2

2
; with
q_00 as ( -- get new records
    select
          code 
        , startdate
    from #scd as s
    where s.startdate = (select MIN(xx.startdate) from #scd as xx where xx.code = s.code)
),
q_01 as ( -- get changed records, those that are not new
    select
          s.code 
        , s.startdate
    from #scd      as s
    left join q_00 as b on b.code = s.code and b.startdate = s.startdate
    where b.code is null
),
q_03 as ( -- get the list of all possible dates
    select distinct 
        startdate
    from #scd
),
q_04 as ( -- count new records per date
    select
          startdate
        , COUNT(1)  as new_rec
    from q_00
    group by startdate
),
q_05 as ( -- count changed records per date
    select
          startdate
        , COUNT(1)  as chn_rec
    from q_01
    group by startdate
)
select
      a.startdate          as OnDate
    , coalesce(new_rec, 0) as new_records
    , coalesce(chn_rec, 0) as changed_records
from      q_03 as a
left join q_04 as b on b.startdate = a.startdate
left join q_05 as c on c.startdate = a.startdate
order by  a.startdate
;
于 2012-06-16T12:19:08.697 に答える
0

結果を得る別の方法

Select A.startdate,isnull(B.cnt,0) [new],(A.Cnt-isnull(B.cnt,0)) [changed]
from
(select startdate,COUNT(*) Cnt from #scd
group by startdate)A
left outer join
(select startdate,COUNT(*) cnt from #scd S1 where code not in
                     (select code from #scd S2 where S2.startdate<S1.startdate)
group by startdate)B
on A.startdate=B.startdate
于 2012-06-25T10:49:50.967 に答える