Skip to content

Instantly share code, notes, and snippets.

@hoganlong
Created May 26, 2020 18:14
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/47e36df3ee0a1c368d2677c22098144a to your computer and use it in GitHub Desktop.
Save hoganlong/47e36df3ee0a1c368d2677c22098144a to your computer and use it in GitHub Desktop.
for example if you have
Id State1 State2
1 1 DE
2 1 GA
3 1 AL
4 1 DC
5 1 FG
6 1 KG
7 1 PM
9 2 LM
9 2 Nk
Then
SELECT State1, COUNT(*) AS COUNT, LISTAGG(CASE WHEN RN > 5 THEN State2 ELSE NULL END, ', ') AS STATES
FROM (SELECT *,
ROW_NUMBER() OVER (PARTITION BY State1 ORDER BY ID) as RN
FROM TABLE1
) AS X
GROUP BY State1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment