简体   繁体   中英

How can I Mix three rows based on the unique value of specific column SQL Oracle?

I want to write a sql query in Oracle developer to convert three rows to one row and combine the value of those row.

The table has three column. column1 : NUMBER(10,0) column2 : VARCHAR2(15 CHAR) column3 : TIMESTAMP(6)

I combine column2 and column3 together to be in one column. At first I have :

在此处输入图片说明

I want to combine these three rows base on the value of column1 (which is 41). I want to have one row at the end:

在此处输入图片说明

Could you please help me to find the correct query?

Edit by mathguy: The oracle-sqldeveloper tag is relevant, because after the right query is written, there is still the issue of being able to see it is "correct" in the Query Result Window in SQL Developer. This is a bit of an issue, because SQL Developer removes white space so it's not clear whether the result is actually correct. This is discussed in comments and the answer.

select column1,
       listagg(column2 || ' ' || to_char(column3, 'dd-MON-rr hh:mi:ss.ff6 AM'), chr(10)) 
               within group (order by column3)
from   table_name
group by column1
;

(Not tested.)

This will present the text column is a single table row, but it will be displayed as three lines of text.

You will also likely want to give an alias to the second column.

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