How can multiple rows be concatenated into one in Oracle without creating a stored procedure?
There are many way to do the string aggregation, but the easiest is a user defined function. Try this for a way that does not require a function. As a note, there is no simple way without the function.
This is the shortest route without a custom function: (it uses the ROW_NUMBER() and SYS_CONNECT_BY_PATH functions )
SELECT questionid,
LTRIM(MAX(SYS_CONNECT_BY_PATH(elementid,','))
KEEP (DENSE_RANK LAST ORDER BY curr),',') AS elements
FROM (SELECT questionid,
elementid,
ROW_NUMBER() OVER (PARTITION BY questionid ORDER BY elementid) AS curr,
ROW_NUMBER() OVER (PARTITION BY questionid ORDER BY elementid) -1 AS prev
FROM emp)
GROUP BY questionid
CONNECT BY prev = PRIOR curr AND questionid = PRIOR questionid
START WITH curr = 1;
SQL Query to concatenate column values from multiple rows in Oracle
There are a few ways depending on what version you have - see the oracle documentation on string aggregation techniques. A very common one is to use LISTAGG
:
SELECT pid, LISTAGG(Desc, ' ') WITHIN GROUP (ORDER BY seq) AS description
FROM B GROUP BY pid;
Then join to A
to pick out the pids
you want.
Note: Out of the box, LISTAGG
only works correctly with VARCHAR2
columns.
SQL to concatenate all Query rows into one row
Why do you want a one row query if you want each row to be printed on a different line?
If you just wan to run a query that returns your data with no header or page separator use this:
set pagesize 0
select question_id || '|' || element_message || '|' || date from your_table;
Oracle SQL - Multiple rows into one field
Use listagg
after getting the distinct delivery types per product id. (Note that there is a 4000 character limit for the aggregated string.)
select product_id,listagg(delivery_type,'/') within group (order by delivery_type)
from (select distinct product_id,delivery_type from tbl) t
group by product_id
How to concatenate text from multiple rows into a single text string in SQL Server
If you are on SQL Server 2017 or Azure, see Mathieu Renda answer.
I had a similar issue when I was trying to join two tables with one-to-many relationships. In SQL 2005 I found that XML PATH
method can handle the concatenation of the rows very easily.
If there is a table called STUDENTS
SubjectID StudentName
---------- -------------
1 Mary
1 John
1 Sam
2 Alaina
2 Edward
Result I expected was:
SubjectID StudentName
---------- -------------
1 Mary, John, Sam
2 Alaina, Edward
I used the following T-SQL
:
SELECT Main.SubjectID,
LEFT(Main.Students,Len(Main.Students)-1) As "Students"
FROM
(
SELECT DISTINCT ST2.SubjectID,
(
SELECT ST1.StudentName + ',' AS [text()]
FROM dbo.Students ST1
WHERE ST1.SubjectID = ST2.SubjectID
ORDER BY ST1.SubjectID
FOR XML PATH (''), TYPE
).value('text()[1]','nvarchar(max)') [Students]
FROM dbo.Students ST2
) [Main]
You can do the same thing in a more compact way if you can concat the commas at the beginning and use substring
to skip the first one so you don't need to do a sub-query:
SELECT DISTINCT ST2.SubjectID,
SUBSTRING(
(
SELECT ','+ST1.StudentName AS [text()]
FROM dbo.Students ST1
WHERE ST1.SubjectID = ST2.SubjectID
ORDER BY ST1.SubjectID
FOR XML PATH (''), TYPE
).value('text()[1]','nvarchar(max)'), 2, 1000) [Students]
FROM dbo.Students ST2
Oracle SQL Split Row Into Multiple Entries Based on Start and End
One method is a recursive CTE:
with tab1 as (
select 'Bob' as usr, 87 as start_num, 98 as end_num from dual
union all
select 'Sue' as usr, 55 as start_num, 64 as end_num from dual
),
cte (usr, num, end_num) as (
select usr, start_num, end_num
from tab1
union all
select user, num + 1, end_num
from cte
where num < end_num
)
select usr, num
from cte;
Here is a db<>fiddle.
Related Topics
Where Is |Datadirectory| Defined
Get the Last Inserted Row Id (With SQL Statement)
Does the Order of Where Clauses Matter in Sql
Dynamic SQL - Exec(@Sql) Versus Exec Sp_Executesql(@Sql)
What Are Covering Indexes and Covered Queries in SQL Server
Spark SQL Window Function With Complex Condition
How to Select from Subquery Using Laravel Query Builder
Transpose Latest Rows Per User to Columns
Combining Union and Limit Operations in MySQL Query
Get Join Table as Array of Results With Postgresql/Nodejs
Ora-00918: Column Ambiguously Defined in Select *
How to Restore a Dump File from MySQLdump
Optimise Postgresql For Fast Testing