Skip to content

Instantly share code, notes, and snippets.

@kohav
Last active April 28, 2017 09:04
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 kohav/b3233c63507323e616158e7d1ddc637f to your computer and use it in GitHub Desktop.
Save kohav/b3233c63507323e616158e7d1ddc637f to your computer and use it in GitHub Desktop.
MS Sql Server / using row_number()over & reversal of result set.
/*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