Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Have a look at <a href="https://data.stackexchange.com/stackoverflow/query/4418/so3014289" rel="nofollow noreferrer">this example</a>.</p> <p>It should be relatively straightforward.</p> <p>It pivots the base data according to your rules.</p> <p>It determines the change times for the denormalized "row"</p> <p>It creates a triangular join to determine the start and end of each period (what I'm calling a snapshot)</p> <p>Then it joins those windows to the base data to determine what the state of the data was at that time (the pivot is actually completed at this time)</p> <p>I think you may need to look at the windowing mechanism - it's returning the right data, but I don't like the way the window overlap logic looks to me - it doesn't quite small right - I'm worried about the boundary conditions.</p> <pre><code>-- SO3014289 CREATE TABLE #src ( key1 varchar(4) NOT NULL ,key2 varchar(3) NOT NULL ,key3 varchar(3) NOT NULL ,AttribCode int NOT NULL ,AttribSubCode varchar(2) ,Value varchar(10) NOT NULL ,[Start] date NOT NULL ,[End] date NOT NULL ) INSERT INTO #src VALUES ('9750', 'C04', '789', 1, NULL, 'AAA', '1/1/2000', '12/31/9999') ,('9750', 'C04', '789', 2, NULL, 'BBB', '1/1/2000', '12/31/9999') ,('9750', 'C04', '789', 3, 'V1', 'XXXX', '1/1/2000', '12/31/9999') ,('9750', 'C04', '789', 3, 'V2', 'YYYY', '1/1/2000', '1/2/2000') ,('9750', 'C04', '789', 3, 'V2', 'YYYYY', '1/2/2000', '12/31/9999') ;WITH basedata AS ( SELECT key1 + '-' + key2 + '-' + key3 AS NK ,CASE WHEN AttribCode = 1 THEN Value ELSE NULL END AS COL1 ,CASE WHEN AttribCode = 2 THEN Value ELSE NULL END AS COL2 ,CASE WHEN AttribCode = 3 AND AttribSubCode = 'V1' THEN Value ELSE NULL END AS COL3 ,CASE WHEN AttribCode = 3 AND AttribSubCode = 'V2' THEN Value ELSE NULL END AS COL4 ,[Start] ,[End] FROM #src ) ,ChangeTimes AS ( SELECT NK, [Start] AS Dt FROM basedata UNION SELECT NK, [End] AS Dt FROM basedata ) ,Snapshots as ( SELECT s.NK, s.Dt AS [Start], MIN(e.Dt) AS [End] FROM ChangeTimes AS s INNER JOIN ChangeTimes AS e ON e.NK = s.NK AND e.Dt &gt; s.Dt GROUP BY s.NK, s.Dt ) SELECT Snapshots.NK ,MAX(COL1) AS COL1 ,MAX(COL2) AS COL2 ,MAX(COL3) AS COL3 ,MAX(COL4) AS COL4 ,Snapshots.[Start] ,Snapshots.[End] FROM Snapshots INNER JOIN basedata ON basedata.NK = Snapshots.NK AND NOT (basedata.[End] &lt;= Snapshots.[Start] OR basedata.[Start] &gt;= Snapshots.[End]) GROUP BY Snapshots.NK ,Snapshots.[Start] ,Snapshots.[End] </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload