December 10, 2011 at 7:30 am
Recently I have gone through an interview.
There I was asked that I have a table and I have to get another column in the table.
This column values should be like.
1
2
3
4
5
1
2
3
4
5
1
2
3
4
5
...............
He asked me that there is some functionality in SQL server by which we can do that.
I don't think that there is something like that in Sql Server. Can anyone tell me how can we develop such column in a table.
Thanks
Amit
December 10, 2011 at 8:16 am
look up SQL server ranking functions, specifically ROW_NUMBER() and the OVER(PARTITION BY) predicate; that can do it, as well as
RANK and DENSE RANK;
if it was just an arbitrary # of rows, ie in groups of 5, you'd probably use integer modulus to determine one of the partition by columns based on another row_number(), ie SomeColumn % 5
if you can show us some example data, we can show you some better row_number examples, but any decent example depends on the data it will manipulate, which we really need first.
Lowell
December 10, 2011 at 8:28 am
The display order of this problem is a bit deceiving. I believe the interviewer was looking for NTILE().
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply