Created on 08-25-2018 04:34 PM - edited 09-16-2022 06:37 AM
Hi Team,
I want to convert columns as rows as per user requirement. I know that this can be done using UNION ALL but I have 60 columns need to be converted. If I go with UNION ALL then I would need to write 60 UNION ALLs which will impact the performance.
Is there any direct way of converting columns to rows like using any functions etc.,
Created 08-26-2018 09:33 PM
Hi, you dont have to union 60 times, you can do this:
select
t.rowid,
t.orderdate,
t.shipmode,
t.customername,
t.state,
m.metric,
case m.metric
when 'sales' then t.sales
when 'quantity' then t.quantity
end value from mytable t
cross join (
select 'sales' metric union all
select 'quantity' metric
) m
Created 08-27-2018 04:47 AM
Hello Tomas79,
Yes, it looks good.
Since we go with cross join doesn't it effect the performance?
If I have 100000 records then if I go with 10 UNION ALL then the count of records will go to 1million.
Is there any suggestion please to improve the performance with this approach?
Created 08-29-2018 03:57 AM
Created 08-26-2018 09:33 PM
Hi, you dont have to union 60 times, you can do this:
select
t.rowid,
t.orderdate,
t.shipmode,
t.customername,
t.state,
m.metric,
case m.metric
when 'sales' then t.sales
when 'quantity' then t.quantity
end value from mytable t
cross join (
select 'sales' metric union all
select 'quantity' metric
) m
Created 08-27-2018 04:47 AM
Hello Tomas79,
Yes, it looks good.
Since we go with cross join doesn't it effect the performance?
If I have 100000 records then if I go with 10 UNION ALL then the count of records will go to 1million.
Is there any suggestion please to improve the performance with this approach?
Created 08-29-2018 03:57 AM