Skip to content

Instantly share code, notes, and snippets.

@hoganlong
Last active August 12, 2020 01:58
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 hoganlong/c9ef8693255918c86ddd72a85212f82b to your computer and use it in GitHub Desktop.
Save hoganlong/c9ef8693255918c86ddd72a85212f82b to your computer and use it in GitHub Desktop.
with data(id, dept, age, name) as
(
values
(1, 'A', 60, 'A A' ),
(2, 'A', 70, 'B B' ),
(3, 'A', 80, 'C C' ),
(4, 'A', 90, 'D D' ),
(5, 'A', 100, 'E E' ),
(6, 'B', 10, 'F F' ),
(7, 'B', 20, 'G G' ),
(8, 'B', 30, 'H H' ),
(9, 'B', 40, 'I I' ),
(10, 'B', 50, 'J J' )
)
SELECT DEPT, FIRST(AGE) OVER (PARTITION BY dept ORDER BY NAME DESC) AS CASE_ONE,
FIRST(AGE) OVER (PARTITION BY dept ORDER BY NAME DESC RANGE UNBOUNDED PROCEDING UNBOUNDED FOLLOWING) AS CASE_TWO
FROM data
-- the following line can be added or not (without it will be clearer what is going on)
GROUP BY DEPT
)
@hoganlong
Copy link
Author

Code sample for this so answer https://stackoverflow.com/a/63223784/215752

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment