[SQL] An example of data deletion and selection, using GROUP and DISTINCT

Source: Internet
Author: User

The problem encountered today had been resolved after a long morning. As recorded here, I think this Code can be applied to many similar problems.


Requirement Description:

A table named MyImage has the following columns: Number ID and PATH
For example:

ID PATH

1 C :/
1 C :/
1 D :/
2 C :/
2 C :/
3 :/
3 C :/
4 D :/



Write an SQL statement and return the ID number of the record: different paths exist for the same ID. In the above example, the correct result should be:

ID

1

3

(ID2 does not have different paths, and ID4 only has one record without different paths)


Here is my SQL statement:

Select ID from [MyImage] as A group by ID having (select COUNT (distinct (Path) from [MyImage] as B where B. ID = A. ID)> 1


One morning, I wrote such a sentence! Excellent! Yes!

Related Article

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.