Skip to content

Instantly share code, notes, and snippets.

@zacps
Created August 3, 2018 09:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zacps/7bc9ad6f718e8d7efe3be2242afca742 to your computer and use it in GitHub Desktop.
Save zacps/7bc9ad6f718e8d7efe3be2242afca742 to your computer and use it in GitHub Desktop.
CREATE OR REPLACE VIEW mytable AS SELECT * FROM
(
VALUES
(1, '2018-08-03 05:00:00+00', 100, 10, 1000),
(2, '2018-08-03 05:00:00+00', 100, 11, 1001),
(3, '2018-08-03 05:00:00+00', 100, 12, 1002),
(4, '2018-08-03 05:00:00+00', 100, 13, 1003),
--
(5, '2018-08-03 05:01:00+00', 200, 20, 2000),
(6, '2018-08-03 05:01:00+00', 100, 10, 1000),
(7, '2018-08-03 05:01:00+00', 100, 11, 1001),
(8, '2018-08-03 05:01:00+00', 100, 12, 1002),
(9, '2018-08-03 05:01:00+00', 100, 13, 1003),
--
(10, '2018-08-03 05:02:00+00', 100, 11, 1001),
(11, '2018-08-03 05:02:00+00', 100, 12, 1002),
(12, '2018-08-03 05:02:00+00', 100, 13, 1003),
--
(13, '2018-08-03 05:03:00+00', 100, 11, 1001),
(14, '2018-08-03 05:03:00+00', 100, 12, 1002)
) AS t (id, ts, stop_sms, service, vehicle_ref);
-- Should return
id | stop_sms | service | vehicle_ref | after_ts
-----+----------+---------+-------------+-----------------------
5 | 100 | 10 | 1000 | 2018-08-03 05:02:00+00 -- Because not present in group with ts='2018-08-03 05:02:00+00'
11 | 100 | 13 | 1003 | 2018-08-03 05:03:00+00 -- Because not present in group with ts='2018-08-03 05:03:00+00'
-- after_ts is the first timestamp that did not include the (stop_sms, service, vehicle_ref) combination, it's a nice-to-have
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment