简体   繁体   中英

SQL Server: Slowly Changing Dimension Type 2 on historical records

I am trying to set up a SCD of Type 2 for historical records within my Customer table. Attached is how the Customer table is set up alongside the expected outcome. Note that the Customer table in practice has 2 million distinct Customer IDs. I tried to use the query below, but the Start_Date and End_Date are repeating for each row.

 SELECT t.Customer_ID, t.Lifecyle_ID, t.Date As Start_Date,
 LEAD(t.Date) OVER (ORDER BY t.Date) AS End_Date
 FROM Customer AS t
  1. It doesn't seem like you're aggregating the original data in any way. You will have the same number of rows in your Type 2 table. Add a DISTINCT or GROUP BY.<\/li>
  2. I'd suggest using MAX and MIN, partitioning by customer and lifecycle.<\/li>
  3. <\/li><\/ol>

    Hopefully this makes sense.

    "

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM