views:

55

answers:

1

Hi, is there a simple way to get the number of the current item in a simple SELECT? I need to deliver a column based on a calculation that involves the number of the current index in the select. I am simplifing my problem to an extreme, but roughly speaking, here is an example:

SELECT column1 * ITEMINDEX FROM table

I hope I am being clear. I am using SQL Server. Thank you.

+5  A: 

In SQL Server 2005+:

SELECT  m.*, ROW_NUMBER() OVER (ORDER BY column) AS rn
FROM    mytable m

SQL does not have concept of implicit row number, that's why you need ORDER BY clause to define the order of rows.

Quassnoi
exactly what i was thinking
DForck42