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
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.
4 Answers
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
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
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
I find that if I can't use DISTINCT for any reason, then GROUP BY will work.
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.
4 Answers
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
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
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
I find that if I can't use DISTINCT for any reason, then GROUP BY will work.
- How to select unique records by SQL
- select records by Date in SQL Server
- SQL Select N to M Records (single Table)
- MySQL how to calculate average value of max/min N records by group
- SQL Syntax - how to group by interval
- How to delete duplicate records from tables without Primary key
- [How To] Remove deleted records from Main Index in Sphinx
- how to connect SQL servr by sqlcmd comment
- how to select data in multiple datatables after using join,group by and order by in Linq
- How to By Pass Proxy
- how to confirm unique fiscal year
- Select top N records from table for each group (group by X)
- How to allow new records to be inserted, but prevent existing data from being edited
- How to deploy portlet to my website - image records of my way
- SQL: How to get a series of dates in sql using connect by
- LINQ TO SQL--select
- How to select the first/least/max row per group in SQL
- how to enable eclipse generate select statement 如何在eclipse里面写sql DDL语句
- 其他优化技巧
- c++ 关联容器详解——从内部结构到应用
- Java Map 按Key排序和按Value排序
- json数据格式
- Android中状态栏的隐藏
- How to select unique records by SQL
- Java List排序
- 托管代码
- 100个windows平台C++开发错误之二十
- 内核内存分配器(Kernel Memory Allocator, KMA)
- Core Java (十一) Java 继承,类,超类和子类
- SQL Server的聚集索引和非聚集索引
- 使用Log4j进行日志操作(经典)
- make sure that /opt/google/chrome/chrome-sandbox is owned by root and mode 4755