Concat rows values into single column
Hi All,
How can I concat values of a multiple rows into a single column by separating them by comma.
Eg:
SELECT empno FROM emp;
empno
1
2
3
4
I want output should be:
Empnos
1,2,3,4
Thanks & Regards,
Danish
MichaelS wrote:
Or
SQL> select rtrim(xmlagg(xmlelement(e, empno || ',')).extract('//text()').extract('//text()') ,',') empnos from emp
EMPNOS
7369,7499,7521,7566,7654,7698,7782,7788,7839,7844,7876,7900,7902,7934
Hi Michael,
is it an error or is it correct to put extract 2 times? This is giving to me the same result:
select rtrim(xmlagg(xmlelement(e, empno || ',')).extract('//text()'),',') empnos from emp;
EMPNOS
7369,7499,7521,7566,7654,7698,7782,7788,7839,7844,7876,7900,7902,7934 Regards.
Al
Similar Messages
-
Concatenate multiple row values into single column value
Hello,
Can anyone please refresh my memory on how to concatenate multiple row values into a single column value.
In the following query, I will get multiple denial reasons per application and I would rather return all denial reasons on one line.
SELECT a.application_id, a.membership_number,
r.reason_text AS denial_reason,
a.appl_receipt_date AS application_receipt_date,
a.plan_request_1 AS application_plan_code,
a.adjudication_date AS application_denial_date
FROM application a, PLAN p, application_reason ar, reason r
WHERE a.plan_request_1 = p.plan_cd
AND a.application_id = ar.application_id
AND ar.reason_id = r.reason_id
AND a.adjudication_cd = 'D'
AND a.appl_receipt_date BETWEEN '01-jan-2006' AND '31-dec-2006'
AND p.plan_type_id = 12 and a.application_id = :appId
ORDER BY application_id
Any help is greatly appreciated.
Thanks,
-Christinefound the following
SELECT deptno,
LTRIM(MAX(SYS_CONNECT_BY_PATH(ename,','))
KEEP (DENSE_RANK LAST ORDER BY curr),',') AS employees
FROM (SELECT deptno,
ename,
ROW_NUMBER() OVER (PARTITION BY deptno ORDER BY ename) AS curr,
ROW_NUMBER() OVER (PARTITION BY deptno ORDER BY ename) -1 AS prev
FROM emp)
GROUP BY deptno
CONNECT BY prev = PRIOR curr AND deptno = PRIOR deptno
START WITH curr = 1;
DEPTNO EMPLOYEES
10 CLARK,KING,MILLER
20 ADAMS,FORD,JONES,SCOTT,SMITH
30 ALLEN,BLAKE,JAMES,MARTIN,TURNER,WARD
3 rows selected.at http://www.oracle-base.com/articles/10g/StringAggregationTechniques.php -
Multiple values into single column
Hi ,
Im having the query like as below
with t as ( select 'Approved' as "Code" from dual union all
select 'Historically Relevant' from dual union all
select 'In Progress' from dual union all
select 'Internal Review' from dual )
select "Code" from t where "Code" in ('Approved,Historically Relevent')
in this i need the output like below . ie, what are the things i have selected in where clause based on that the output need to come
'Approved,Historically Relevent'
Thanks in Advance ...Always remember to post your database version as it can make a difference to the answer.
In 10g you would use sys_connect_by_path to aggregate strings together...
SQL> ed
Wrote file afiedt.buf
1 with t as (select 'Approved' as code from dual union all
2 select 'Historically Relevant' from dual union all
3 select 'In Progress' from dual union all
4 select 'Internal Review' from dual )
5 --
6 -- end of test data
7 --
8 select ltrim(sys_connect_by_path(code,','),',') as codes
9 from (
10 select code, rownum r
11 from t
12 where code in ('Approved','Historically Relevant')
13 )
14 where connect_by_isleaf = 1
15 connect by r = prior r + 1
16* start with r = 1
SQL> /
CODES
Approved,Historically Relevant
In 11gR2 onwards you can use the new LISTAGG function
SQL> ed
Wrote file afiedt.buf
1 with t as (select 'Approved' as code from dual union all
2 select 'Historically Relevant' from dual union all
3 select 'In Progress' from dual union all
4 select 'Internal Review' from dual )
5 --
6 -- end of test data
7 --
8 select listagg(code,',') within group (order by rownum) as codes
9 from t
10* where code in ('Approved','Historically Relevant')
SQL> /
CODES
Approved,Historically Relevant -
How to insert parameter value into multiple columns and rows
Hi All,
I have one procedure insert_tab and I am passing
100~101~102:103~104~105:106~107~108 as a parameter to that procedure. I wanted to insert each numeric value into one column. The output of the table should contain
Table:
Col1 Col2 Col3
100 101 102
103 104 105
106 107 108
Awaiting for your reply..That's not more clear for me...
Anyway, if you really want a procedure for that, try :
SQL> create table tblstr (col1 number,col2 number,col3 number);
Table created.
SQL>
SQL> create or replace procedure insert_fct (p_string IN varchar2)
2 as
3 v_string varchar2(4000):=p_string||':';
4 v_substring varchar2(4000);
5
6 begin
7 while instr(v_string,':') > 0 loop
8 v_substring := substr(v_string,1,instr(v_string,':')-1)||'~';
9 insert into tblstr(col1,col2,col3)
10 values (substr(v_substring,1,instr(v_substring,'~',1,1)-1),
11 substr(v_substring,instr(v_substring,'~',1,1)+1,instr(v_substring,'~',1,2)-instr(v_substring,'~',1,1)-1),
12 substr(v_substring,instr(v_substring,'~',1,2)+1,instr(v_substring,'~',1,3)-instr(v_substring,'~',1,2)-1));
13 v_string:=substr(v_string,instr(v_string,':')+1);
14 end loop;
15 end;
16 /
Procedure created.
SQL>
SQL> show err
No errors.
SQL>
SQL> select * from tblstr;
no rows selected
SQL> exec insert_fct('100~101~102:103~104~105:106~107~108')
PL/SQL procedure successfully completed.
SQL> select * from tblstr;
COL1 COL2 COL3
100 101 102
103 104 105
106 107 108
SQL> exec insert_fct('109~~')
PL/SQL procedure successfully completed.
SQL> exec insert_fct('~110~')
PL/SQL procedure successfully completed.
SQL> exec insert_fct('~~111')
PL/SQL procedure successfully completed.
SQL> select * from tblstr;
COL1 COL2 COL3
100 101 102
103 104 105
106 107 108
109
110
111
6 rows selected.
SQL> Nicolas. -
Convert different rows into single column
DB : 11.1.0.7
OS : Solaris Sparc 5.10
I have one query which is joining few tables and giving me output like below.
personnum orgnm
======= =======
6 Keyholder
9 Sales
3 Mgmt
I would like to convert that into single column like below.
col1
========
6,Keyholder,9,Sales,3,Mgmt
I have tried with pivot and decode, but not getting resule which I am exepcting. Any suggesstions ?yashwanth437 wrote:
listagg() function could work.LISTAGG is not available in 11.1. It was introduced in 11.2.
Anyway, XML solution:
with sample_table as (
select 6 personnum,'Keyholder' orgnm from dual union all
select 9,'Sales' from dual union all
select 3,'Mgmt' from dual
select rtrim(xmlagg(xmlelement(e,personnum || ',' || orgnm,',').extract('//text()')),',') col1
from sample_table
COL1
6,Keyholder,9,Sales,3,Mgmt
SQL> SY. -
How to get rows values in a column only ?
How to get all returned values in rows in a column.
e.g., a query gives output as :
123
234
233
12121
all in different rows. But i want the result like this:
123,234,233,12121.All in a single row and a single column .
How is this possible ?prakash wrote:
hi ,
Use the following example
CREATE or replace FUNCTION fn_return_row
RETURN VARCHAR2
IS
v_row VARCHAR2 (32767);
BEGIN
FOR curr_row IN (SELECT ename
FROM emp11)
LOOP
v_row := v_row ||','|| curr_row.ename;
END LOOP;
RETURN v_row;
EXCEPTION
WHEN OTHERS
THEN
RETURN NULL;
END fn_return_row;
select fn_return_row from dual ;Thanks,
P PrakashThere's no need to use PL/SQL when SQL provides adequate functionality to do it.
The title of the thread is misleading as turning rows into columns is called pivoting, but the description of the issue wanting to join all the rows together into a single column, is actually called string aggregation.
As already demonstrated it can be done several ways.
1. with XML functionality
2. using the SYS_CONNECT_BY_PATH method
and if you have 11gR2, there's...
3. the new LISTAGG analytical function.
There is also an undocumented WMSYS.WM_CONCAT function, though this isn't as flexible as other methods in that you can't control the order of the aggregated data so easily, and using such undocumented functions in production code is dangerous, as the functionality could change in future versions of Oracle, as well as it's use making your code such that Oracle will not provide support if the code with issue is using it. -
Hide row values for certain column in GRR2
Hi Experts,
Looking for some help in report painter. I need to hide row values for certain columns in report painter. The requirement is I have 5 columns in the report, the 5 th column is the sum of col 1 to 4 and in my row i have a formula setup to sum of values for each column, what i would like to do is for column 1 thru 4 i didnt want to display the total values in row total but i wanted to dispaly value for column 5 in row total. I have been trying my best with putting formula, but couldnt succeed.
Could somebody let me know is there an way to get this addressed.
Thanks in advance
Best Regards,
gjHow was it achieved ? Did you use sections for the columns for which rows needed to be hidden?
I have a smiliar issue of hiding certain rows for few columns. -
How to insert an upper value into a column?
Hi!,
I need to insert an upper value into a column.
The data is on a flat file and it could be lowercase or uppercase?
Do I have to create a trigger to solve this problem?
How could the trigger be?
Thanks,
AlexIf you are using SQL*Loader to load the data from flat file into the db table, then you can achieve inserting the UPPER Case of column value in your sqlloader ctl file itself as in below example
LOAD DATA
INFILE *
APPEND INTO TABLE XXX
( field1 position(1:7) char "UPPER(:field1)",
field2 position(8:15) char "UPPER(:field2)"
BEGINDATA
Phil Locke
Jason Durbin
So in this case the Names Phil Locke and Jason Durbin will be inserted as PHIL LOCKE and JASON DURBIN into the target table.
Regards,
Murali Mohan -
How to copy one column BLOB value into another column of another database.
How to copy one column BLOB value into another column of another database.
BLOB value contains word document.
I thought of copy the BLOB value into a text file and then update the new column value by the same text in textfile. Will this work?
Is there any other better way to do this?You're welcome
BLOB fields contains binary data. I don't think you can do this
Also if I view the BLOB as text. Can I copy it and insert into the new database.
I think your options are as I said. Datapump or CTAS
Best Regards -
To populate values into single field in an internal table
Hi Friends,
How we need to populate values into single field in an internal table.
E.g itab consits of single field ( name)
i need to assign values to this field name .like
peter,
john,
abrahm,
daneyal
Pls tell me i how i need to code for this
Thanks ,
ParnithHi,
Please look at the below code :
DATA : BEGIN OF itab OCCURS 0 ,
name(20) TYPE c,
END OF itab.
START-OF-SELECTION.
itab-name = 'Peter'.
APPEND itab.
CLEAR itab.
itab-name = 'John'.
APPEND itab.
CLEAR itab.
itab-name = 'Abrahm'.
APPEND itab.
CLEAR itab.
itab-name = 'Daneyal'.
APPEND itab.
CLEAR itab.
LOOP AT itab.
WRITE : / itab.
ENDLOOP.
Thanks,
Sriram Ponna. -
To display comma separted value into multiple column
Hi,
I want to display value into multiple column like below
data is like this
col1
res_menaHome:MenaHome
res_menaHomeEmp:MenaHome Employee
res_MDSpecialSer:MD Special Services
res_Smart:Smart
now i want to display like
col1 col2
res_menaHome MenaHome
res_menaHomeEmp MenaHome Employee
res_MDSpecialSer MD Special Services
res_Smart Smart
Thanks in advance.You mean like this?
with q as (select 'res_menaHome:MenaHome' myString from dual)
select substr(myString, 1, instr(myString, ':') - 1) col1,
substr(myString, instr(myString, ':') + 1) col2
from q
COL1,COL2
res_menaHome,MenaHome -
How to Splilit The String Into Single Column using Comma As Delimiter ?
How to Splilit The String Into Single Column using Comma As Delimiter ?
using Functionrefer my thread ,code is also available see if that helps you
error while executing the sp ORA-21779: duration not active -
Merging values of CKFs in two columns into single column
Hi ,
I want to merge values present in two different columns.The values present in rows of one column are not present in rows of other column, I have to merge the values into a single column.so that in the single column i can get all the values.how to do it in BEx.
regards ,
man.Hi,
Dear murali , the solution you gave has helped me a bit as i am able to get the values for both the formula KF's in the same column,But another problem has arisen.
For both the formula KF's I had to choose 'New Selection' (and not 'New formula')after R-cllick on the structure, as I had to use a Characteristic as restriction for Each of the KF's, and also to use the Characteristic I had to associate it with some KF so that the Characteristic wil be used as a KF in the formula.
Now what is happening is that I am getting the values of that associated KF also in the empty rows.(i.e. the rows where the value of both the KF1 and KF2 is not present)
What has to be done to remove them and keep the empty rows empty and not show the value of the KF taken with the Characteristic for restriction. Also its value is summing up with KF1 and KF2 in the coulmn which is having both KF1 and KF2 together.
Regards
man -
Row values into column....
Hi Experts,
Can i add column values into a single cell. Like my requirement is i want to add values of Name column in a single cell of some other table. For example if Table A contains A, B, C D names then in second table i want all these name in one cell as ABCD. I'm not able to find solution. please help me. i'm working on ODI. Any help or clue.
Regards
-KirtiHi,
2 suggestions:
1) If you have a constant number of columns you could put, at an interface, so much "instances" of the datastore as the number of columns, create the join and put the necessary filter at each "instance". The mapping will be the concatenation of the column from each instance
or
2) Create a procedure where you have the select column_to_be_concatenated at Source TAB and an update of that at Target TAB (if necessary you can define a dynamic PL/SQL to deal with insert update!)
Does it help you? -
Please help - Joining three tables and get row values into Column. Please help!
Hi,
There is a SourceTable1 (Employee) with Columns like EmployeeID,Name,DOB.
There is a sourcetable2 (EmployeeCode) with columns like EmployeeID,Code,Order.
There is a source table 3 #EmployeeRegioncode and its columns are (EmployeeID , RegionCode , [Order]
The target table 'EmployeeDetails' has the following details. EmployeeID,Name,DOB,Code1,Code2,Code3,Code4,regioncode1
regioncode2 ,regioncode3 ,regioncode4
The requirement is , the value of the target table columns the Code1,code2,code3 ,code4,code5 values should
be column 'Code' from Sourcetable2 where its 'Order' column is accordingly. ie) Code1 value should be the 'Code' value where [Order] column =1, and Code2 value should be the 'Code' value where [Order] =2, and so on.
Same is the case for Source table 3- 'Region code' column also for the columns regioncode1
regioncode2 ,regioncode3 ,regioncode4
Here is the DDL and Sample date for your ref.
IF OBJECT_ID('TEMPDB..#Employee') IS NOT NULL DROP TABLE #Employee;
IF OBJECT_ID('TEMPDB..#EmployeeCode') IS NOT NULL DROP TABLE #EmployeeCode;
IF OBJECT_ID('TEMPDB..#EmployeeDetails') IS NOT NULL DROP TABLE #EmployeeDetails;
---Source1
CREATE table #Employee
(EmployeeID int, Empname varchar(20), DOB date )
insert into #Employee VALUES (1000,'Sachin','1975-12-12')
insert into #Employee VALUES (1001,'Sara','1996-12-10')
insert into #Employee VALUES (1002,'Arjun','2000-12-12')
---Source2
CREATE table #EmployeeCode
(EmployeeID int, Code varchar(10), [Order] int)
insert into #EmployeeCode VALUES (1000,'AA',1)
insert into #EmployeeCode VALUES (1000,'BB',2)
insert into #EmployeeCode VALUES (1000,'CC',3)
insert into #EmployeeCode VALUES (1001,'AAA',1)
insert into #EmployeeCode VALUES (1001,'BBB',2)
insert into #EmployeeCode VALUES (1001,'CCC',3)
insert into #EmployeeCode VALUES (1001,'DDD',4)
insert into #EmployeeCode VALUES (1002,'AAAA',1)
insert into #EmployeeCode VALUES (1002,'BBBB',2)
insert into #EmployeeCode VALUES (1002,'CCCC',3)
insert into #EmployeeCode VALUES (1002,'DDDD',4)
insert into #EmployeeCode VALUES (1002,'EEEE',5)
---Source tbl 3
CREATE table #EmployeeRegioncode
(EmployeeID int, RegionCode varchar(10), [Order] int)
insert into #EmployeeRegioncode VALUES (1000,'xx',1)
insert into #EmployeeRegioncode VALUES (1000,'yy',2)
insert into #EmployeeRegioncode VALUES (1000,'zz',3)
insert into #EmployeeRegioncode VALUES (1001,'xx',1)
insert into #EmployeeRegioncode VALUES (1001,'yy',2)
insert into #EmployeeRegioncode VALUES (1001,'zz',3)
insert into #EmployeeRegioncode VALUES (1001,'xy',4)
insert into #EmployeeRegioncode VALUES (1002,'qq',1)
insert into #EmployeeRegioncode VALUES (1002,'rr',2)
insert into #EmployeeRegioncode VALUES (1002,'ss',3)
---Target
Create table #EmployeeDetails
(EmployeeID int, Code1 varchar(10), Code2 varchar(10),Code3 varchar(10),Code4 varchar(10),Code5 varchar(10) , regioncode1 varchar(10),
regioncode2 varchar(10),regioncode3 varchar(10),regioncode4 varchar(10))
insert into #EmployeeDetails VALUES (1000,'AA','BB','CC','','','xx','yy','zz','')
insert into #EmployeeDetails VALUES (1001,'AAA','BBB','CCC','DDD','','xx','yy','zz','xy')
insert into #EmployeeDetails VALUES (1002,'AAAA','BBBB','CCCC','DDDD','EEEE','qq','rr','ss','')
SELECT * FROM #Employee
SELECT * FROM #EmployeeCode
SELECT * FROM #EmployeeRegioncode
SELECT * FROM #EmployeeDetails
Can you please help me to get the desired /targetoutput? I have sql server 2008.
Your help is greatly appreciated.select a.EmployeeID,b.code1,b.code2,b.code3,b.code4,b.code5,c.Reg1,c.Reg2,c.Reg3,c.Reg4 from
#Employee a
left outer join
(select EmployeeID,max(case when [Order] =1 then Code else '' end) code1,
max(case when [Order] =2 then Code else '' end)code2,
max(case when [Order] =3 then Code else '' end)code3,
max(case when [Order] =4 then Code else '' end)code4,
max(case when [Order] =5 then Code else '' end)code5 from #EmployeeCode group by EmployeeID) b
on a.EmployeeID=b.EmployeeID
left outer join
(select EmployeeID,max(case when [Order] =1 then RegionCode else '' end) Reg1,
max(case when [Order] =2 then RegionCode else '' end)Reg2,
max(case when [Order] =3 then RegionCode else '' end)Reg3,
max(case when [Order] =4 then RegionCode else '' end)Reg4 from #EmployeeRegioncode group by EmployeeID) c
on a.EmployeeID=c.EmployeeID
Thanks
Saravana Kumar C
Maybe you are looking for
-
Is there a way to lock the yoga 2 pro in tablet mode? and in vertical?
It is beyond irritating. I can't seem to get it to stay in tablet mode and it switches from tablet to laptop to tent. and rotates even when I put the rotation lock on (from swiping the settings in from the right)
-
HT3819 laptop died, how do I get my itunes off of it?
my laptop died, how do I get my itunes off of it?
-
Hi, I have a strange client requirement, which I want to achieve without using a pivot table, if possible, as; - 1. Pivot tables may not be scaleable to the volume of data which I will ultimately have in the solution 2. I need to create two measures,
-
No messages in sxmb_moni in target machine
Hi, I have file to proxy scenario; and i see http 200 code in PI sxmb_moni; it means it is sucessful, returned back; but i dont see any messages in sxmb_moni of target R3 system. i did check logging; everything is set in sxmb_adm. and also in channel
-
Converting svg to pdf,eps
Hello, How i can programatically convert SVG file to PDF and EPS using Illustrator SDK or Illustrator Scripting API(if it's not same)? it would be great to do it in php script