1

it's been some years since I used the Oracle SQL pivot function and I cannot quite get mine to work for this short example.

I would like to transform the multiple columns/ rows from this table

ID   CLASS   NAME   WHEIGHT
1    2020    Tom    80
2    2020    Tim    100
3    2020    Ben    120

into this single result row:

CLASS    LISTAGG_NAMES    SUM_WHEIGHT
2020     Tom,Tim,Ben      300

Thank you very much for some help!

1 Answer 1

1

This is not a "pivot" problem. You do not want separate columns for each value. You just want to concatenate them. Use listagg():

select class, listagg(name, ',') within group (order by weight) as names, sum(weight)
from t
group by class;
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Gordon, that did the trick. I also changed the question title

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.