How to find the index number of an item as a result of a query

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

SELECT column1 * ITEMINDEX FROM table 

Hope I cleared up. I am using SQL Server. Thanks.

+4
source share
1 answer

In SQL Server 2005+ :

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

SQL does not have the concept of an implicit row number, so you need ORDER BY to determine the order of rows.

+7
source

All Articles