Guest User

Untitled

a guest
Jul 20th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. item
  2. item_location_date
  3.  
  4. ild_item (FK to Item)
  5. ild_location (FK to location)
  6. ild_pallet (Nullable FK to pallet id)
  7. ild_created_when (creation timestamp)
  8.  
  9. select *
  10. from item
  11. cross apply (
  12. select top 1 *
  13. from item_location_date
  14. where ild_item = itm_id
  15. order by ild_date_created desc
  16. ) ild
  17.  
  18. ALTER TABLE [dbo].[item_location_date] ADD CONSTRAINT [PK_item_location_date] PRIMARY KEY CLUSTERED
  19. (
  20. [ild_date_created] DESC,
  21. [ild_item] DESC,
  22. [ild_location] DESC
  23. )
  24.  
  25. select *
  26. from
  27. item inner join
  28. (select *
  29. from (
  30. select *,
  31. ROW_NUMBER() over (PARTITION by ild_item order by ild_date_created desc) rn
  32. from item_location_date
  33. ) ild
  34. where rn = 1
  35. ) itm_location
  36. on itm_location.ild_item = itm_id
  37.  
  38. select *
  39. from item
  40. cross apply (
  41. select top 1 *
  42. from item_location_date
  43. where ild_item = itm_id
  44. order by ild_date_created desc
  45. ) ild
  46.  
  47. CREATE NONCLUSTERED INDEX NCI_item_location_date_item_date_created
  48. ON [dbo].[item_location_date] (ild_item, ild_date_created)
Add Comment
Please, Sign In to add comment