How to select unique records by SQL

来源:互联网 发布:java if else同时执行 编辑:程序博客网 时间:2024/04/30 09:50

http://stackoverflow.com/questions/1641718/how-to-select-unique-records-by-sql




8down votefavorite
4

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

1 item1 data12 item1 data23 item2 data34 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 data12 item2 data33 item3 data4

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

share|improve this question

76% accept rate
 
feedback

4 Answers

activeoldestvotes
up vote17down voteaccepted

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

select distinct a, cfrom table_c

or

select a, bfrom table_cgroup 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_cgroup by a, bselect a, b, sum(d)from table_cgroup by a, b
share|improve this answer
 
feedback
up vote9down vote

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

GROUP BY v DISTINCT

share|improve this answer
 
feedback
up vote1down vote

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  tableGROUP BY item
share|improve this answer
 
feedback
up vote0down vote

I find that if I can't use DISTINCT for any reason, then GROUP BY will work.

share|improve this answer

8down votefavorite
4

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

1 item1 data12 item1 data23 item2 data34 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 data12 item2 data33 item3 data4

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

share|improve this question

76% accept rate
 
feedback

4 Answers

activeoldestvotes
up vote17down voteaccepted

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

select distinct a, cfrom table_c

or

select a, bfrom table_cgroup 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_cgroup by a, bselect a, b, sum(d)from table_cgroup by a, b
share|improve this answer
 
feedback
up vote9down vote

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

GROUP BY v DISTINCT

share|improve this answer
 
feedback
up vote1down vote

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  tableGROUP BY item
share|improve this answer
 
feedback
up vote0down vote

I find that if I can't use DISTINCT for any reason, then GROUP BY will work.

share|improve this answer
原创粉丝点击