TECHNOLOGIESTieInn Technologies

Testing slowly changing dimensions (Type 2)

ETL & Data Warehouse Testing · 7 questions

Short notes

Key idea

A Type 2 dimension keeps history as versions (surrogate key, business key, valid_from, valid_to, is_current). Test that each customer has exactly one current version, the flag agrees with the dates, versions don't overlap or leave gaps, the current version matches the source, no version is redundant, and every fact matches exactly one version on its date.

Syntax

SELECT customer_id FROM dw_customers GROUP BY 1
HAVING COUNT(*) FILTER (WHERE is_current) <> 1;

SELECT customer_id, customer_key FROM dw_customers
QUALIFY valid_to IS DISTINCT FROM
  LEAD(valid_from) OVER (PARTITION BY customer_id ORDER BY valid_from);

-- point-in-time join
ON c.customer_id = f.customer_id AND f.order_date >= c.valid_from
AND (c.valid_to IS NULL OR f.order_date < c.valid_to)

Example

Shop: customer 16 has two current versions; customer 13's versions overlap 2024-05-20 to 24; customer 11's move to Bengaluru was missed; customers 7, 23 and 38 are missing.

Traps

  • Testing only current rows.
  • <> on valid_to vs next valid_from (both NULL on the last version).
  • BETWEEN valid_from AND valid_to double-matches the change day.
  • Only testing with facts that exist today: probe suspicious dates.

Want the full lesson with step-by-step examples, a project and a certificate? ETL & Data Warehouse Testing course, ₹120 →

Questions

Open in Code Lab5 free questions
5 questions

    Testing slowly changing dimensions (Type 2)

  1. Question 1easy

    How many versions does customer 8 have?

    SELECT COUNT(*) FROM dw_customers WHERE customer_id = 8;
  2. Testing slowly changing dimensions (Type 2)

  3. Question 2medium

    Return the customer_id of every customer in dw_customers that doesn't have exactly one current version.

    Tables
  4. Testing slowly changing dimensions (Type 2)

  5. Question 3easy

    How many rows have an is_current flag that disagrees with valid_to?

    SELECT COUNT(*) FROM dw_customers WHERE is_current <> (valid_to IS NULL);
  6. Testing slowly changing dimensions (Type 2)

  7. Question 4medium

    A customer has two consecutive versions with exactly the same city and premium status. What does this suggest?

  8. Testing slowly changing dimensions (Type 2)

  9. Question 5hard

    How many versions of customer 13 are valid on 22 May 2024?

    SELECT COUNT(*)
    FROM dw_customers
    WHERE customer_id = 13
      AND DATE '2024-05-22' >= valid_from
      AND (valid_to IS NULL OR DATE '2024-05-22' < valid_to);

2 more questions on this topic, with hints, and tests

Sign up free for 21 days of every question and test.