Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi Team,
I have data like below
SQL
create table #tmp(id int,name varchar(10))
insert #tmp
select 1, 'a'
union
select 1, 'b'
union
select 1, 'c'
union
select 2, 'a'
union
select 2, 'b'
union
select 2, 'c'
union
select 3, 'a'
union
select 3, 'b'
union
select 3, 'c'



and i want print data like below
1 a
2 b
3 c

please help me on this
Thanks in Advance.
Posted
Comments
Tomas Takac 7-Oct-15 8:01am    
Not clear. What is the relation between input and output?
deepankarbhatnagar 7-Oct-15 8:18am    
No enough data to understand
ZurdoDev 7-Oct-15 8:44am    
No, we can't help. What happened to 1, b and 1,c? Need a lot more info.

1 solution

My best guess:
SQL
DECLARE @tmp TABLE(id int,name varchar(10))

insert @tmp (id, name)
VALUES( 1, 'a'),
(1, 'b'),
(1, 'c'),
(2, 'a'),
(2, 'b'),
(2, 'c'),
(3, 'a'),
(3, 'b'),
(3, 'c')

SELECT DISTINCT t1.id, t2.name
FROM @tmp AS t1 INNER JOIN
    (
        SELECT id, name, ROW_NUMBER() OVER(PARTITION BY id ORDER BY name) AS RowNo
        FROM @tmp
    ) AS t2 ON t1.id = t2.RowNo


Result - as expected:
id  name
1   a
2   b
3   c
 
Share this answer
 

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