I need to generate report that will show number of new / changed rows on per-day basis for SCD table.
Here is the SQL to create the table:
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
The result look like this:
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
Now, I need to produce someting like this:
date new changed
2012-06-01 2 0
2012-06-02 0 2
2012-06-03 0 1
2012-06-04 1 2
Any help is much appreciated.