Advertisement
Guest User

Untitled

a guest
Jul 26th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. create table test_history
  2. (
  3. id int,
  4. prop VARCHAR(16)
  5. );
  6.  
  7. INSERT into test_history values ( 1,'a');
  8. INSERT into test_history values ( 2,'b');
  9.  
  10. create TABLE updated_test ( id int ,prop VARCHAR(16));
  11.  
  12. INSERT into updated_test values ( 1,'c');
  13.  
  14. create table test_history_all
  15. (
  16. id int,
  17. prop VARCHAR(16),
  18. begin_date date,
  19. end_date date
  20. );
  21.  
  22. insert into test_history_all
  23. select
  24. id,
  25. prop,
  26. '2017-01-01',
  27. '3000-12-31'
  28. from test_history;
  29.  
  30.  
  31. select
  32. alls.id,
  33. alls.prop,
  34. alls.begin_date,
  35. case
  36. when
  37. alls.end_date = '3000-12-31' and updated.id is not null then cast('2017-01-02' as date) else cast('3000-12-31' as date)
  38. end
  39. from test_history_all alls left join updated_test updated on alls.id = updated.id
  40. union all
  41. select
  42. alls.id,
  43. updated.prop,
  44. cast('2017-01-02' as date),
  45. end_date
  46. from test_history_all alls
  47. JOIN updated_test updated on alls.id = updated.id;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement