Row_Number with complex conditions?

Of course you can have complex partitioning expressions, just like you could have complex grouping expressions. Here's one possible way how you could rank rows in your example.

Of course you can have complex partitioning expressions, just like you could have complex grouping expressions. Here's one possible way how you could rank rows in your example: SELECT Id, Type, ROW_NUMBER() OVER ( PARTITION BY CASE WHEN Type IN (32, 64) THEN 1 ELSE 2 END Order BY Date ) FROM atable But you'd have to repeat that condition later when filtering rows to display. So, if I were you, I might try something like this: WITH marked AS ( SELECT *, TypeGroup = CASE WHEN Type IN (32, 64) THEN 1 ELSE 2 END FROM atable ), ranked AS ( SELECT *, RowNb = ROW_NUMBER() OVER (PARTITION BY TypeGroup ORDER BY Date) FROM ) SELECT Id, Type, RowNb FROM ranked WHERE TypeGroup = 1 OR RowNb = 1 ORDER BY TypeGroup, RowNb.

Thank you, the first solution works like a charm for me, and like you said the second one can also be useful in some case. – krtek Nov 8 '11 at 19:20.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions