Retrive data from a table problem

Posted by Manoj_B under Sql Server on 6/26/2013 | Points: 10 | Views : 661 | Status : [Member] | Replies : 3
Dear All,

I have a table like below:-

Id Name Age
1 Manoj 22
2 Manoj 22
3 Rahul 56
4 Rahul 56
5 Rohit 21
6 Rohit 21

But my o/p will be below:

Id Name Age
1 Manoj 22
3 Rahul 56
5 Rohit 21

How i can write the sql query, please help me, it's urgent.

Thanks & Regards
Manoj Kumar



Responses

Posted by: Niladri.Biswas on: 6/26/2013 [Member] Platinum | Points: 25

Up
0
Down
Try this

Declare @t table(Id int identity, Name varchar(20),Age int)

Insert Into @t values('Manoj',22),('Manoj',22),('Rahul',56),('Rahul',56),('Rohit',21),('Rohit',21)

;With Cte As(
Select Rn = ROW_NUMBER() Over(Partition By Name,Age Order By Name),Id,Name,Age
From @t)
Select Id,Name,Age
From Cte
Where Rn = 1


/* Result */

Id	Name	Age

1 Manoj 22
3 Rahul 56
5 Rohit 21


Best Regards,
Niladri Biswas

Manoj_B, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Neeraaj.Sharma on: 6/28/2013 [Member] Starter | Points: 25

Up
0
Down
Hi : Manoj_B


please always marks as answer in you satisfy with it if not than add your reply might be answer er did not get your question this encourage us and heppfull to other as weel who are having same question


2always put data in a table As Niladri.Biswas has put it,




vist my blog


www.tutorialsqlserver.com





Manoj_B, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: perfectchourasia-9163 on: 6/28/2013 [Member] Starter | Points: 25

Up
0
Down
select distinct(Name) from Tablename

ER sandeep chourasia
sandeepchrs@yahoo.com (on facebook)
http://www.aspnetcodes.com/

Manoj_B, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response