Last active
April 28, 2017 09:04
-
-
Save kohav/b3233c63507323e616158e7d1ddc637f to your computer and use it in GitHub Desktop.
MS Sql Server / using row_number()over & reversal of result set.
This file contains hidden or 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
/*using row_number()over*/ | |
select Id_Otdel, | |
firstName, | |
row_number()over(partition by Id_Otdel order by firstName) rn | |
from d_Sotr | |
where Id_Otdel in (11, 12, 21) | |
/*with reversal of result set*/ | |
select max(case when Id_Otdel=11 | |
then firstName else null end) as analysts, | |
max(case when Id_Otdel=12 | |
then firstName else null end) as mgrs, | |
max(case when Id_Otdel=21 | |
then firstName else null end) as prez | |
from ( | |
select Id_Otdel, | |
firstName, | |
row_number()over(partition by Id_Otdel order by firstName) rn | |
from d_Sotr | |
where Id_Otdel in (11, 12, 21) | |
) x | |
group by rn |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment