onze sponsors
Voor het vullen van een SCD (slowly changing dimension) TYPE 2 tabel voor persoonsgegevens
Hoi Robert,
Hierbij wat aanvullende info:
De brongegevens zijn het resultaat van een union op basis van de volgende queries:
select pers_nr, rubriek_waarde as dienstverb, null as [funct.code], begin_datum, eind_datum
from brontabel
where rubriekcode = 'P01102'
and rel.pers_nr = '3031'
order by 4
pers_nr dienstverb funct.code begin_datum eind_datum
3031 VDV NULL 1985-04-01 2008-01-01
3031 VST NULL 2008-01-01 NULL
select pers_nr, null as dienstverb, rubriek_waarde as [funct.code], begin_datum, eind_datum
where rubriekcode = 'P01106'
3031 NULL 790 1985-04-01 2003-01-01
3031 NULL 414 2003-01-01 2007-03-01
3031 NULL 488 2007-03-01 2008-01-01
3031 NULL 823 2008-01-01 NULL
Ruud,
onderstaand script levert aan de hand van jouw voorbeeld het gewenste resultaat. Ik hoop dat dat je de juiste richting is voor je. Lees voor de tijdelijke tabel je eigen tabel. Ivm met de NULL waarden zijn het twee queries middels een UNION tot een resultaat.
create table #t ( pers_nr int not null, dienstverb varchar(5) null, fcode int null, begindatum smalldatetime not null, einddatum smalldatetime null ); insert into #t values (3031, NULL, 790, '1985-04-01', '2002-12-31'), (3031, 'VDV', NULL, '1985-04-01', '2007-12-31'), (3031, NULL, 414, '2003-01-01', '2007-02-28'), (3031, NULL, 488, '2007-03-01', '2007-12-31'), (3031, NULL, 823, '2008-01-01', NULL), (3031, 'VST', NULL, '2008-01-01', NULL); select t1.pers_nr, t2.dienstverb, t1.fcode, t1.begindatum, t1.einddatum from #t t1 inner join #t t2 on t1.begindatum >= t2.begindatum and t1.einddatum <= t2.einddatum where t2.dienstverb is not null and t1.fcode is not null union select t1.pers_nr, t2.dienstverb, t1.fcode, t1.begindatum, t1.einddatum from #t t1 inner join #t t2 on t1.begindatum = t2.begindatum where t1.einddatum is null and t2.einddatum is null and t2.dienstverb is not null and t1.fcode is not null drop table #t;