Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
I have this table (the red number is the number I'm trying to assign)[CODE]Position Invoice9 18001 111 18001 2159 18004 1259 18006 1261 18006 2263 18006 3[/CODE]I need to assign to each record its ordinal number, grouped by Invoice and ordered by position.
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts
Posted - 2012-07-21 : 13:13:01
If you are on SQL 2005 or later you can use the row_number function like this:
SELECT Position, Invoice, ROW_NUMBER() OVER (PARTITION BY Invoice ORDER BY Position) AS OrdinalNumberFROM YourTable