I’m working with the SQL Server database. Say, I have a selection that returns an array of IDs. Can I find out (with SQL) the order of an ID equalled N in that array?
Here’s what I mean. This is my actual selection:
SELECT [id] FROM [dbo.test_db_002] t1
LEFT JOIN [dbo.test_db_003] t2
ON t1.[id]=t2.[itmid]
ORDER BY t2.[iid] ASC;
Say, it will return the following array of IDs:
13, 7, 34, 5, 2, 14
And, say I need to know what index is ID=5 in that array (the answer is 3, if 13 is index 0, 7 = index 1, 34 = index 2, 5 = becomes index 3.)
Try using ROW_NUMBER():
More on it here http://msdn.microsoft.com/en-us/library/ms186734.aspx