Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

building SCD2 table issue

$
0
0

Hello,

I have to build SCD type 2 on one column value. Before I implement "catch-modification" in our DWH, I have to load historical data to such dimension.

I have original table which is catching modifications on many fields (about 20), so my one field I am interested in is repeated many times,although its value was not changed.

This is sample of original , SDC2 field is "status", business key is "matnr".

CREATE TABLE #Data
(matnr char(8),
status char(2),
imp_date date)
INSERT INTO #Data
SELECT 'L3194200','04','2012-09-11' UNION ALL
SELECT 'L3194200','04','2012-09-15' UNION ALL
SELECT 'L3194200','06','2012-11-06' UNION ALL
SELECT 'L3194200','04','2012-11-07' UNION ALL
SELECT 'L3194200','04','2013-04-03' UNION ALL
SELECT 'L3194200','06','2013-07-03'

I need to create SCD2 result

SELECT 'L3194200' as matnr,'04' as status,'2000-01-01' as valid_from ,'2012-11-05' as valid_to UNION ALL
SELECT 'L3194200','06','2012-11-06','2012-11-06' UNION ALL
SELECT 'L3194200','04','2012-11-07','2013-07-02' UNION ALL
SELECT 'L3194200','06','2013-07-03',null

Problem I am not able to solve is in the part of aggregating of repeated statuses. 04 is used before 06, but its again used after 06.... I know how to solve that with SSIS and script component, but I am wondering how to do that without temporary table also in TSQL. Code must be fast adequatly, source table has about 350 000 rows and it could be shrinked to something about 65 000 with aggregation described above.

Thanks


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>