Taie
Taie

Reputation: 1179

Pivot rows into unknown number of columns

I have an access to Oracle server. There is a table on the Oracle server called Transactions which contains the following data:

enter image description here

I don't known the number of values, so we need to implement dynamic sql in Oracle. I need to pivot that data so the results are:

enter image description here

Any suggestions?

Upvotes: 0

Views: 174

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269773

You can use conditional aggregation:

select subno,
       sum(case when offer = 'offer1' then 1 else 0 end) as offer1,
       sum(case when offer = 'offer2' then 1 else 0 end) as offer2,
       sum(case when offer = 'offer3' then 1 else 0 end) as offer3
from t
group by subno;

Upvotes: 1

Related Questions