Look for a record with the same column in the datasheet and another column with different values

Source: Internet
Author: User

If I have a table T which has two columns, as shown in:
A B
1 3
1 3
2 5
2 4
3 7
3 7
Where a column value of 1 corresponds to the B column value of the 3,a column value of 2 corresponds to the value of column B is two, one is 5, one is 4, we are to List 2 5,2 4 of this two records.

The first method uses the Max and Min functions smartly, and when we use the A-column grouping, if Max (b) =min (b), then all the values of column B are definitely the same. The reverse is the record we need to find. SQ is as follows:
Select *  from where inch       (Selectfromgroupby haveMax(TI.B) <> min (TI.B))

You can also use another notation

Select *  from where inch       (Select fromGroup ,selectdistinct  by TI.A         Having count (ti.b) >1

The above method first removes the duplicate columns in a, B, and then, based on the column A, calculates the number of different values in column A, which compares the above one to add a SELECT, more complex!

Not enough to find the internal select can be removed, the simplified version is as follows:

Select *  from where inch       (Selectfrom  Group by ti.a        have  Count(distinct ti.b)>1

Look for a record with the same column in the datasheet and another column with different values

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.