Click here to Skip to main content
15,904,828 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I want only distinct values in one cloumn other columns can contain duplicate values,how do I achieve this

SQL
create table test
(
    Id int,
    Name varchar(max),
    Department varchar(max),
    Country varchar(max)
)
insert into test values (1,'B','IT','India')
insert into test values (2,'B','IT','US')
insert into test values (3,'C','HR','UK')
insert into test values (4,'D','HR','India')
insert into test values (5,'E','IT','US')
insert into test values (6,'F','IT','UK')
select * from test


I just want the non duplicate values in country column.
select id,country from test group by country FAILED
select id,country distinct from test FAILED
Posted
Updated 18-Jun-13 7:03am
v2
Comments
Zoltán Zörgő 18-Jun-13 14:05pm    
Ok, but what about the rest - what about the other column values? What result do you expect?
You have two "US" there. You want a single one. Great. And what will be the "unification" result in your case:
a) 2,'B','IT','US'?
b) 5,'E','IT','US'?
c) 2+5, 'BE', 'ITIT', 'US'?
d) ?????
"other columns can contain duplicate values" makes no sense. If you want a single row, a field can contain only one value.

You gave us a sample input. Please, give us also the desired result based on that input.

1 solution

The group by approach would work, but you need to decide what you want to see with the other columns. All columns not in the group by clause need to have some kind of aggregate function (max, min, avg etc)

an example of how to write the group by would be something like this:

select max(id), country
from test
group by country
 
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