Click here to Skip to main content
15,898,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a table mame record in database which has two fields id and name. and have some duplicate records. i show my tables with data as
Id     Name
 1      A
 2      A
 3      A
 4      B
 5      B
 6      B
 7      C
 8      C
 9      C

now write a querry for this table to show the output as follow
Id    Name
1      A
4      B
7      C
Posted
Updated 27-Aug-12 20:37pm
v2

Try this
SQL
CREATE TABLE #TempTable
(Id INT, [Name] VARCHAR(50))

INSERT INTO #TempTable
SELECT 1, 'A' UNION
SELECT 2, 'A' UNION
SELECT 3, 'A' UNION
SELECT 4, 'B' UNION
SELECT 5, 'B' UNION
SELECT 6, 'B' UNION
SELECT 7, 'C' UNION
SELECT 8, 'C' UNION
SELECT 9, 'C'



SELECT MIN(Id) AS Id, [Name] FROM #TempTable
GROUP BY [Name]

DROP TABLE #TempTable
 
Share this answer
 
Comments
Mohamed Mitwalli 28-Aug-12 2:47am    
5+
__TR__ 28-Aug-12 3:09am    
Thanks :)
srishti_ 28-Aug-12 2:47am    
thanx
__TR__ 28-Aug-12 3:09am    
You are welcome.
try this
SQL
SELECT ID,NAME FROM [TABLE_NAME] GROUP BY NAME
 
Share this answer
 
Comments
Mohamed Mitwalli 28-Aug-12 2:47am    
Error Will Occur
bhagirathimfs 28-Aug-12 2:59am    
why??
Mohamed Mitwalli 28-Aug-12 3:01am    
is not contained in either an aggregate function this is error msg will appear
Check it
srishti_ 28-Aug-12 2:47am    
thanx
Hi ,
Check this
SQL
select min( Id) ,name from dbo.Tablename
Group by name



Best Regards
M.Mitwalli
 
Share this answer
 
Comments
__TR__ 28-Aug-12 3:08am    
My 5!
Mohamed Mitwalli 28-Aug-12 3:11am    
Thanks TR
Hi,

Try this code,

SQL
select *
from (select Id,Name,row_number()over(partition by Name order by Id) as occurrence
          from Example) x
          where occurrence = 1


Here table name is Examle.

The result as follows:

Id Name occurrence
1 A 1
4 B 1
7 C 1

Regards
 
Share this answer
 
Comments
__TR__ 28-Aug-12 3:12am    
5+
D-Kishore 28-Aug-12 3:22am    
Thanks TR
Use :
SQL
select min(ID) as ID, name from [tablename] group by name
 
Share this answer
 
Comments
__TR__ 28-Aug-12 2:44am    
My 5!
srishti_ 28-Aug-12 2:47am    
thanx
Mohamed Mitwalli 28-Aug-12 2:47am    
5+

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