tags:

views:

168

answers:

3

When I perform "SELECT * FROM table" I got results like below:

1 item1 data1
2 item1 data2
3 item2 data3
4 item3 data4

As you can see, there are dup records from column2 (item1 are dupped). So how could I just get result like this:

1 item1 data1
2 item2 data3
3 item3 data4

Only one record are returned from the duplicate, along with the rest of the unique records.

+4  A: 

you can use select distinct or group by to do this.

select distinct a, c
from table_c

or

select a, b
from table_c
group by a, b

group by will be more helpful if you want to use some aggregate function like count or sum

select a, b, count(*)
from table_c
group by a, b

select a, b, sum(d)
from table_c
group by a, b
marshall
+2  A: 

If you only need to remove duplicates then use DISTINCT. GROUP BY should be used to apply aggregate operators to each group

GROUP BY v DISTINCT

rahul
A: 

It depends on which rown you want to return for each unique item. Your data seems to indicate the minimum data value so in this instance for SQL Server.

SELECT item, min(data)
FROM  table
GROUP BY item
Dave Barker