Guest User

Untitled

a guest
Apr 25th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. id name value date
  2. a last jones 2018-04-03 01:00:00
  3. a last smith 2018-04-03 03:03:03
  4. a last johns 2018-04-04 02:02:02
  5. b last johns 2018-04-03 12:12:12
  6. b last smith 2018-04-04 03:03:03
  7. c last smith 2018-04-03 11:11:11
  8. c last johns 2018-04-03 17:17:17
  9. c last jones 2018-04-05 01:01:01
  10.  
  11. SELECT distinct a.id
  12. FROM people a
  13. left JOIN people b
  14. ON (a.id = b.id and a.name=b.name and a.date < b.date)
  15. where b.date is null
  16. and a.name = 'last'
  17. and a.value = 'johns';
  18.  
  19. id
  20. --
  21. a
  22.  
  23. id
  24. --
  25. b
  26. c
  27.  
  28. create table people
  29. ( id char(1) not null
  30. , name char(4) not null
  31. , value varchar(20) not null
  32. , ts timestamp not null);
  33.  
  34. insert into people (id, name, value, ts)
  35. values ('a', 'last', 'jones', '2018-04-03 01:00:00')
  36. , ('a', 'last', 'smith', '2018-04-03 03:03:03')
  37. , ('a', 'last', 'johns', '2018-04-04 02:02:02')
  38. , ('b', 'last', 'johns', '2018-04-03 12:12:12')
  39. , ('b', 'last', 'smith', '2018-04-04 03:03:03')
  40. , ('c', 'last', 'smith', '2018-04-03 11:11:11')
  41. , ('c', 'last', 'johns', '2018-04-03 17:17:17')
  42. , ('c', 'last', 'jones', '2018-04-05 01:01:01');
  43.  
  44. select id
  45. from people a
  46. where not exists (
  47. select 1
  48. from people b
  49. where a.id = b.id
  50. and a.name=b.name
  51. and a.ts < b.ts
  52. )
  53. and a.name = 'last'
  54. and a.value = 'johns';
  55.  
  56. select id
  57. from people a
  58. where not exists (
  59. select 1
  60. from people b
  61. where a.id = b.id
  62. and a.name=b.name
  63. and a.ts < b.ts
  64. and b.ts < '2018-04-04 00:00:00'
  65. )
  66. and a.name = 'last'
  67. and a.value = 'johns'
  68. and a.ts < '2018-04-04 00:00:00';
Add Comment
Please, Sign In to add comment