Advertisement
Guest User

Untitled

a guest
Apr 24th, 2014
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. 14
  2.  
  3. 14
  4.  
  5. ms
  6.  
  7. DECLARE @as_of_date DATE = '2014-01-04'
  8.  
  9. ;WITH cte AS (
  10. SELECT [id], [idd], [created_on], [property], [old_value], [new_value], rn=ROW_NUMBER() OVER (PARTITION BY idd ORDER BY created_on DESC)
  11. FROM YourTable
  12. WHERE created_on <= @as_of_date
  13. )
  14. SELECT *
  15. FROM cte
  16. WHERE rn = 1
  17.  
  18. ;WITH CTE1 (created_on, idd)
  19. AS (
  20. SELECT MAX(created_on), idd
  21. FROM Table_1
  22. WHERE created_on < @DateTo
  23. GROUP BY idd)
  24. SELECT t1.* FROM Table_1 t1
  25. INNER JOIN CTE1 ON t1.created_on = CTE1.created_on and t1.idd = CTE1.idd
  26. ORDER BY idd;
  27.  
  28.  
  29. SELECT * FROM Table_1 t1
  30. WHERE t1.created_on = (SELECT MAX(created_on) FROM Table_1 t2 where t1.idd = t2.idd AND created_on < @DateTo)
  31. ORDER BY idd;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement