Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. CREATE TABLE DimCustomer (
  2. CustomerKey serial PRIMARY KEY,
  3. CustomerNum int unique,
  4. CustomerName varchar(25) NOT NULL,
  5. Planet varchar(25) NOT NULL,
  6. RowIsCurrent char(1) NOT NULL DEFAULT 'Y',
  7. RowStartDate date NOT NULL DEFAULT CURRENT_TIMESTAMP,
  8. RowEndDate date NOT NULL DEFAULT '12/31/9999'
  9. );
  10.  
  11. INSERT INTO DimCustomer
  12. (CustomerNum, CustomerName, Planet, RowStartDate)
  13. VALUES (101,'Anakin Skywalker', 'Tatooine', CURRENT_TIMESTAMP - INTERVAL '101 days'),
  14. (102,'Yoda', 'Coruscant', CURRENT_TIMESTAMP - INTERVAL '100 days'),
  15. (103,'Obi-Wan Kenobi', 'Coruscant', CURRENT_TIMESTAMP - INTERVAL '100 days')
  16.  
  17. CREATE TABLE Staging_DimCustomer
  18. (
  19. CustomerNum int unique,
  20. CustomerName varchar(25) NOT NULL,
  21. Planet varchar(25) NOT NULL,
  22. ChangeDate date NOT NULL DEFAULT CURRENT_TIMESTAMP,
  23. RankNo int NOT NULL DEFAULT 1
  24. )
  25. INSERT INTO Staging_DimCustomer(CustomerNum, CustomerName, Planet, ChangeDate)
  26. VALUES
  27. (103,'Ben Kenobi', 'Coruscant', CURRENT_TIMESTAMP - INTERVAL '99 days')
  28.  
  29. INSERT INTO DimCustomer (
  30. CustomerNum, CustomerName, Planet, RowIsCurrent, RowStartDate, RowEndDate
  31. )
  32. select CustomerNum, CustomerName, Planet, 'Y', ChangeDate, '12/31/9999'
  33. from Staging_DimCustomer
  34.  
  35. ON CONFLICT (CustomerNum) and RowIsCurrent = 'Y'
  36. DO UPDATE SET
  37. CustomerName = EXCLUDED.CustomerName,
  38. Planet = EXCLUDED.Planet,
  39. RowIsCurrent = 'N',
  40. RowEndDate = EXCLUDED.ChangeDate
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement