简体   繁体   中英

Transposing rows to a single column

I have a grid element which requires input values in a single column

 Key_id  event1    name1  date1   price1   event2   name2  date2   price2 
  234   Marriage   Sasha  09-DEC   4300   Birthday  Kate   10-DEC   3000

My SQL query output will return data listed in rows

Key_id  event     Name    date     price
 234   Marriage   Sasha  09-DEC    4300
 234   Birthday   Kate   10-DEC    3000

So i need to transpose or convert the list of rows to columns based on key_id.The values are not fixed and will be dynamic based on the time period selected by the user.

I have gone through similar questions in this forum , But i would like to know whether the same thing can be achieved using any Java API or SQL queries.

Kindly enlighten me on how to achieve this ..

Thanks

From your question I guess you want to transpose the horizontal axis (columns) into vertical.

Use something like:

select t.key_id
,      t.event1 event
,      t.name1  name
,      t.date1  date_event
,      t.price1 price
from   table t
union all
select t.key_id
,      t.event2 event
,      t.name2
,      t.date2
,      t.price2
from   table t

Did this help you? If so, please confirm. If not, please leave a note.

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