Created
May 26, 2020 18:14
-
-
Save hoganlong/47e36df3ee0a1c368d2677c22098144a to your computer and use it in GitHub Desktop.
answer for https://stackoverflow.com/questions/62027855/compare-value-in-sql-column-and-output-accordingly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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