Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I have a douts about ROW_NUMBER() funcction using sql server,
i have a two sample query like below,
My problem is my fist query is working fine because its avoiding duplicate datas, compare to my actual table data
But my second query is not avoiding duplicate values as well as my SN number always showing 1
but i want to show my SN number should be autoincrement.
So how can i achieve this solution, please help me.

My Actual table data is:

id
----
ajson1
ajson1
ajson3
ajson4

query 1:
select DISTINCT id from json order by id asc
OUTPUT:
id
-----
ajson1
ajson3
ajson4

query 2:
SELECT
(SELECT DISTINCT ROW_NUMBER() OVER(ORDER BY id ASC)) AS SN,
id
FROM json

OUTPUT:
SN id
-- -----
1 ajson1
1 ajson1
1 ajson3
1 ajson4


thanks in advance

What I have tried:

Row_Number function in sql server
Posted
Updated 1-Apr-16 1:42am

try this

SQL
with T(id) as ( select distinct id from json )
SELECT  'RowNo'=ROW_NUMBER()   OVER(ORDER BY id ASC) , id from T
 
Share this answer
 
Working with what you already have you could do this ...
SQL
SELECT id, min(SN) FROM
(
	SELECT id,
	ROW_NUMBER() OVER(ORDER BY id ASC) AS SN
	FROM json
) src GROUP BY id

Which gets rid of your duplicate and only shows the lowest row number
ajson1	1
ajson3	3
ajson4	4

But if you want an auto-increment row number then you should consider using an IDENTITY column on the table itself. This will always be unique - even if records are deleted and others subsequently added and any specific row will always have the same number. E.g.
SQL
create table json2
(
	sn int identity(1,1),
	id varchar(max)
)
insert into json2 values
('ajson1'),
('ajson1'),
('ajson3'),
('ajson4')

select min(sn), id
from json2
group by id
Notice how the sql is a little simpler

However, if the results you wanted were actually
ajson1	1
ajson3	2
ajson4	3
then the query would just be
SQL
SELECT id, ROW_NUMBER() OVER(ORDER BY id ASC) AS SN
FROM json
group by id
 
Share this answer
 
Comments
stellus 1-Apr-16 8:04am    
Hi thanks for your reply,
your code working fine but when i implement in my code its not supporting subquery,

SELECT
(SELECT id, ROW_NUMBER() OVER(ORDER BY id ASC)) AS SN
FROM json
group by id

I GOT THE BELOW ERROR
"Only one expression can be specified in the select list when the subquery is not introduced with EXISTS."
CHill60 1-Apr-16 8:43am    
That's not the code I gave you. Remove the first SELECT and get rid of the extra brackets. You are trying to assign both the row number AND the id to a single column SN.
At a push you could change it to be
SELECT * from
(SELECT id, ROW_NUMBER() OVER(ORDER BY id ASC) AS SN
FROM json
group by id
) q
but there is really no point in adding the extra query

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900