Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

row numbering

$
0
0

Environment: SQL Server 2005, Win7

Problem:I am not quit sure how to partition xid2 then number the rows for xid2 based on id1

for example take a look at the following rows

id1              xid2
704729555	96857 -- 1
704729555	96857 -- 1
724696433	96857  --2
724696433	96857  -- 2
704729555	96858 -- 1
704729555	96858  -- 1
724696433	96858  --2
724696433	96858  --2

in the above example, we have two major records 96857 and 96858. Each record has sub records, for example

96858 has two records  704729555, 724696433

My Code:

WITH cte
AS (
	SELECT *
		,row_number() OVER (
			PARTITION BY xid2   ORDER BY id1
			) AS Rn
	FROM 
	(
           select statement
       ) x  


My Results:

id1            xid2       name  row number
383259638	96859	Blaba	1
383259638	96859	Blaba	2
724696433	96859	Gaga	3
724696433	96859	Gaga	4

Desired results

id1            xid2       name  row number
383259638	96859	Blaba	1
383259638	96859	Blaba	1
724696433	96859	Gaga	2
724696433	96859	Gaga	2

This will help me to say blaba as singer#1 and gaga as singer#2 when I use select when case rn= 1...statement

Please advised




Viewing all articles
Browse latest Browse all 23857

Trending Articles