Data Formatting Question

Hi All,

I'm not sure how to do data formatting using a SQL Statement in Oracle database.

Is it possible to have output from a SQL statement appear like (see attached) or in an excel format using a pivot table?
EE-Question.xlsx
LVL 11
mohammadzahidAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

slightwv (䄆 Netminder) Commented:
Oracle also has a PIVOT ability.

You have to have a known set of columns so based on the data, I guessed that it was the tire location.  I got that with a SUBSTR on position.

See if this works for you:
select * from (
   select asset, status, position, tire, substr(position,6) pivot_col
   from tab1 t
)
pivot 
(
   max(position) max_pos,
   max(tire) max_tire
   for pivot_col in ('LCI','LCO','LF','LRI','LRO','RCI','RCO','RF','RRI','RRO')
)
/

Open in new window

1
slightwv (䄆 Netminder) Commented:
If there is a possibility where an asset isn't 5 characters in length, you might add a regex:
select * from (
   select asset, status, position, tire, regexp_replace(position,'^' || asset) pivot_col
   from tab1 t
)
pivot 
(
   max(position) max_pos,
   max(tire) max_tire
   for pivot_col in ('LCI','LCO','LF','LRI','LRO','RCI','RCO','RF','RRI','RRO')
)
/

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
SQL

From novice to tech pro — start learning today.