Can groupby solve this query? Solution

Source: Internet
Author: User
Can groupby solve this query? & nbsp; field & nbsp; a & nbsp; B & nbsp; c & nbsp; & nbsp; 1 & nbsp; 2 & nbsp; can the group by statement in & nbs solve this query?
Field a B c
1 2 3
2 3 2
2 1 5
5 3 4
3 5 3
3 4 7
4 3 6
Query out
1. fields a or B include three
2. select only the one with the largest c value for the combination of a and B.

The last thing you want to get is,
2 3 2
5 3 4
3 4 7
The statement is not very good. I hope you can understand it and help me solve it.


------ Solution --------------------
Yes, yes ......


SELECT a, b, MAX( c ) AS c
FROM (
SELECT 1 a, 2 b, 3 c
UNION ALL SELECT 2 , 3, 2
UNION ALL SELECT 2 , 1, 5
UNION ALL SELECT 5 , 3, 4
UNION ALL SELECT 3 , 5, 3
UNION ALL SELECT 3 , 4, 7
UNION ALL SELECT 4 , 3, 6
)t
WHERE t.a =3
OR t.b =3
GROUP BY (
IF( t.a < t.b, CONCAT( t.a,  ',', t.b ) , CONCAT( t.b,  ',', t.a ) )
)

Execution result
Abc
232
347
534

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.