Skip to content

Instantly share code, notes, and snippets.

@jgomo3
Last active September 9, 2016 02:28
Show Gist options
  • Save jgomo3/9bd3acafba4d8123deb600e0a04ce6a2 to your computer and use it in GitHub Desktop.
Save jgomo3/9bd3acafba4d8123deb600e0a04ce6a2 to your computer and use it in GitHub Desktop.
Example of hypotetical FIRST, SECOND and NTH Agregate Function
CREATE TEMPORARY TABLE object_with_cases
(
id int,
case int
);
INSERT INTO object_with_cases
(id, case)
VALUES
(3, 1)
(1, 2)
(2, 5)
(1, 6)
(2, 4)
(3, 3)
(1, 4)
;
SELECT
id
, FIRST(case) AS FST
, SECOND(case) AS SND
, NTH(3, case) AS TRD
FROM object_with_cases
GROUP BY id
ORDER BY case
;
-- OUTPUT
-- | id | FST | SND | TRD |
-- | 1 | 2 | 4 | 6 |
-- | 2 | 4 | 5 | NULL |
-- | 3 | 1 | 3 | NULL |
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment