简体   繁体   中英

SQL Group by on multiple columns

My input is:

    a b c 
    -------
    A 5 3
    A 4 2
    B 3 1
    B 5 3

I would like to get all a values having the same values in b and c , so the output should be as:

{A,B} 5 3

I am using the group by , but I am not achieving my goal.

In standard SQL, this would look like:

select b, c, listagg(a, ',') within group (order by a)
from t
group by b, c;

Not all databases support listagg() , but most have a method for concatenating strings.

In Hive, you would use collect_list() or collect_set() :

select b, c, collect_list(a, ',')
from t
group by b, c;

You can convert the array back to a string, but I recommend keeping it as an array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM