SQL query multiple non-repeating record value brief analysis "reprint"

Source: Internet
Author: User

Reprint Http://hi.baidu.com/my_favourate/item/3716b0cbe125f312505058eb

SQL query multiple non-repeating record values brief analysis 2008-02-28 11:36 below test under Oracle 10g
Because of the design needs, need to query the record value of the same kind of problem, presumably we have encountered, so immediately Google, found that this kind of problem is quite a lot, the solution is also a lot, take a closer look.
For example, the following table structure and values
Table
FID name Sex
1 a male
2 b Male
3 C Female
4 D Female
5 a male
6 B Male
Scenario One: Distinct
Select DISTINCT name from table
Get results:
Name
A
B
C
D
To achieve the effect, what if you want to open other records at the same time? Try again.
SELECT DISTINCT name,id from table
The test has no effect, check that it is true that the name and ID fields are repeated before being filtered. You can continue to find the following methods:
Scenario Two: Group by
SELECT *, COUNT (distinct name) from the table group by name

Oracle under test failed, it is said to pass under MySQL, regardless, continue to think ....
Turn over the books and try
Select min (FID), name,sex from table group by name
Success, the reality of the following results:
FID name Sex
1 a male
2 b Male
3 C Female
4 D Female
Continue thinking, if you want to open all records, do not specify a field (*), it seems that this method is not!
SELECT * FROM table where FID in (Select min (FID) from table group by name)
Test success
FID name Sex
1 a male
2 b Male
3 C Female
4 D Female
Programme III:
It was already over, and suddenly remembered the last few days on the internet to check all the fields in the query data duplicate records
SELECT * FROM table where name in (select name from table group by name has count (name) =2)
The following results are obtained:
FID name Sex
1 a male
2 b Male
5 a male
6 B Male
And so on
SELECT * FROM table where name in (select name from table group by name has count (name) =1)
According to Reason said no problem, everyone try ~ ~
A lot more fields are all realistic. Hey, that's easy! Review the online method Distinct,inner join and so on, troublesome, and have great limitations.
Summarized as follows:
Select DISTINCT name from table to open a single field for duplicate records
SELECT * FROM table where FID in (Select min (FID) from table group by name) Open all field values for duplicate records
SELECT * FROM table where name in (select name from table group by name has count (name) =1) Open Repeat all records of any number of times

In thousands of records, there are some identical records, how can you use SQL statements, delete duplicates?
1, look for redundant records in the table, duplicate records are based on a single field (Peopleid) to determine
SELECT * from People
where Peopleid in (select Peopleid from People GROUP by Peopleid have count (Peopleid) > 1)

2, delete redundant records in the table, duplicate records are based on a single field (Peopleid) to judge, leaving only the smallest ROWID records
Delete from people
where Peopleid in (select Peopleid from People GROUP by Peopleid have count (Peopleid) > 1)
and rowID not in (select min (rowid) from people GROUP by Peopleid have Count (Peopleid) >1)

3. Find redundant duplicate records (multiple fields) in the table
SELECT * FROM Vitae a
where (A.PEOPLEID,A.SEQ) in (select Peopleid,seq from Vitae GROUP by PEOPLEID,SEQ have count (*) > 1)

4. Delete extra duplicate records (multiple fields) in the table, leaving only the record with ROWID minimum
Delete from Vitae a
where (A.PEOPLEID,A.SEQ) in (select Peopleid,seq from Vitae GROUP by PEOPLEID,SEQ have count (*) > 1)
and rowID not in (select min (rowid) from Vitae GROUP by PEOPLEID,SEQ have Count (*) >1)

5. Find redundant duplicate records (multiple fields) in the table, not including the smallest ROWID records
SELECT * FROM Vitae a
where (A.PEOPLEID,A.SEQ) in (select Peopleid,seq from Vitae GROUP by PEOPLEID,SEQ have count (*) > 1)
and rowID not in (select min (rowid) from Vitae GROUP by PEOPLEID,SEQ have Count (*) >1)

For example, there is a field "name" in Table A, and the "name" value between different records may be the same,
Now is the need to query out the records in the table, "name" value has duplicate entries;
Select Name,count (*) from A Group by Name have Count (*) > 1

SQL query multiple non-repeating record value brief analysis "reprint"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.