Convert single column into rows
hi Gurus,
I have one table test colums are id and name.
id number
name varchar2
data is like
id name
1 xy
2 xyy
3 mm
4 pp
Now my requirement is to convert single column id into rows
i,e my output should be of singel rows like :- 1,2,3,4
How to achive this result .
I dont have any idea to do this query.
Please help guys.
Thanks in advance.
Vijay
Well,
As long as your code doesn't have to run in production, simplest way is:
WM_CONCAT (but it's not documented)
or use XMLAGG, it's simpler than a connect by:
MHO%xe> with t as (
2 select 1 col, 'xy' str from dual union all
3 select 2, 'xyy' from dual union all
4 select 3, 'mm'from dual union all
5 select 4, 'pp' from dual union all
6 select 8, 'pp' from dual union all
7 select 12, 'pp' from dual union all
8 select 40, 'pp' from dual
9 )-- actual query, based on id's generated above:
10 select rtrim(xmlagg(xmlelement(e,col||',')).extract('//text()'),',') col
11 from t;
COL
1,2,3,4,8,12,40
1 rij is geselecteerd.
Similar Messages
-
How to convert single column into single row
I need to convert single column into single row having n no.of.values in a column. without using case or decode. I need a query to display as below.
emp_id
100
101
102
102
103
200
I need output like 100,101,102,103,104.........200.I assume you want to convert 200 rows with one column into one row with 200 columns. If so, this is called pivot. If you know number of rows (max possible number of rows) and you are on 11G you can use PIVOT operator (on lower versions GROUP BY + CASE). Otherwise, if row number isn't known, you can use dynamic SQL or assemble select on clent side. Below is example emp_id = 1,2,..5 (to give you idea) and you are on 11G:
with emp as (
select level emp_id
from dual
connect by level <= 5
select *
from emp
pivot(
max(emp_id) for emp_id in (1 emp_id1,2 emp_id2,3 emp_id3,4 emp_id4,5 emp_id5)
EMP_ID1 EMP_ID2 EMP_ID3 EMP_ID4 EMP_ID5
1 2 3 4 5
SQL>
SY. -
Dear all....I need to convert all columns into rows in a table. For example table has following columns:
Emp_Cod........Val1......Val2......Val3
1 a b c
Now I wish that each column should display as a value like:
Emp_Cod........Val1
1 a
1 b
1 c
Now the one way to solve this job is to write a union statement for each column but for this I'll have to write equal number of select statements as there are columns.
What I need that is there anyway to write minimum code for this job, is there any alternate way???SQL> with t as(select 1 emp_code, 'a' val1, 'b' val2, 'c' val3 from dual)
2 select*from t unpivot(v for c in(val1,val2,val3));
EMP_CODE C V
1 VAL1 a
1 VAL2 b
1 VAL3 c
SQL> col COLUMN_VALUE for a20
SQL> with t as(select 1 emp_code, 'a' val1, 'b' val2, 'c' val3 from dual)
2 select*from t,table(sys.odcivarchar2list(val1,val2,val3));
EMP_CODE V V V COLUMN_VALUE
1 a b c a
1 a b c b
1 a b c c -
Hi All,
I need help in building view which actually can show columns data as row.
e.g.
row is as follows
Name Age Salary
ABC 25 10000
BBC 28 12000
The above tables data I want to get as
Name ABC BBC
Age 25 28
Salary 10000 12000
Thanks in advance.Even if I don't really understand such requirement, I wrote some times ago such function to play around that :
Re: Converting Columns into rows
Nicolas. -
How to display the rows in to columns and columns into rows?
DES:- I know by using pivot and unpivot you can convert rows into columns and columns into rows but i don't know how to write the syntax?
Hi,
Besides the places Martin mentioned above, there are many examples in the forum FAQ:
https://community.oracle.com/message/9362005#9362005
For an example of both PIVOT and UNPIVOT in the same query, see
http://forums.oracle.com/forums/thread.jspa?threadID=920854&tstart=0 -
How to split columns into rows
Hi All,
Below is my table structure:=
SQL> create table split(id number,value varchar2(200));
Table created.
SQL> insert into split values(1,'X,Y,Z');
1 row created.
SQL> insert into split values(2,'A,B,C');
1 row created.
SQL> commit;
Commit complete.
Expected Output
ID Value
1 X
1 Y
1 Z
2 A
2 B
3 C
I know the feature of converting rows into columns by listagg in Oracle 11g, but is there any feature to convert rows into columns based on a delemiter..."," in my case.
Please help....
Thanks
Arijit>
is there any feature to convert rows into columns based on a delemiter
>
Here is one way
VAR csv VARCHAR2(100)EXEC :csv := 'abc,de,fg,hij,klmn,o,pq,rst,uvw,xyz';
The query:
WITH data AS( SELECT SUBSTR(csv, INSTR(csv,',',1,LEVEL)+1, INSTR(csv,',',1,LEVEL+1) - INSTR(csv,',',1,LEVEL)-1 ) token FROM ( SELECT ','||:csv||',' csv FROM SYS.DUAL ) CONNECT BY LEVEL < LENGTH(:csv)-LENGTH(REPLACE(:csv,',',''))+2 )SELECT token FROM data;See http://projectwownow.blogspot.com/2010/02/oracle-convert-csv-string-into-rows.html -
Converting multiple columns into one.
All,
I have a requirement to convert multiple columns into one. Following is the pseudo code of current implementation. It unions the various columns ( col1, col2........) into a new column new_col. But perforamnce is extrmely slow owing to multiple unions. It may be noted that tables and where conditions of all these queries are same.
Can you help me create a more efficient query?
select col1 , col2, col3, col4 from my_tables where some_cols = my_condition;Below is the query used to convert these columns into one.
select col1 new_col from my_tables where some_cols = my_condition
union all
select col2 from my_tables where some_cols = my_condition
union all
select col3 from my_tables where some_cols = my_condition
union all
select col4 from my_tables where some_cols = my_conditionWithout looking at those things you could be barking up the wrong tree by assuming the issue relates to the unioning of queries or other such things.Well, might be.........
Execution time of this query (just the execution time not the retrival) is around 34 seconds with connect-by clause for returning 22,000 records.
Here's the plan
Execution Plan
| Id | Operation | Name | Rows
| Bytes | Cost (%CPU)|
| 0 | SELECT STATEMENT | | 326
| 33904 | 135 (2)|
| 1 | SORT ORDER BY | | 326
| 33904 | 135 (2)|
| 2 | MAT_VIEW ACCESS BY INDEX ROWID | MV_COMQ_RM_DEAL | 1
| 62 | 1 (0)|
| 3 | NESTED LOOPS | | 326
| 33904 | 134 (1)|
| 4 | MERGE JOIN CARTESIAN | | 326
| 13692 | 3 (0)|
| 5 | VIEW | | 1
| 13 | 2 (0)|
| 6 | COUNT | |
| | |
| 7 | CONNECT BY WITHOUT FILTERING| |
| | |
| 8 | FAST DUAL | | 1
| | 2 (0)|
| 9 | BUFFER SORT | | 326
| 9454 | 3 (0)|
| 10 | TABLE ACCESS BY INDEX ROWID | SF_SEARCH_IDS_TMP_COMQ | 326
| 9454 | 1 (0)|
| 11 | INDEX RANGE SCAN | IDX1_SF_SEARCH_IDS_TMP_COMQ | 349
| | 1 (0)|
| 12 | INDEX RANGE SCAN | IDX_MV_COMQ_RM_DEAL | 9
| | 1 (0)|
----------------------- Hope, It's readable.......
I would have posted sample data and query but........I cant do that...:( -
Problem in displaying the data of columns into rows in sap script
hi,
i am working on a sap script and i have to display the dat which is displayed in column into rows but it is not displaying it properly.
eg, C
12.1
Si
5.5
it is displaying the data right now like this but i want to display the data like this:-
eg, C Si
12.1 5.5
plzzprovide me guidelines how to solve this problem.hi,
i am using this code to display the data:-
plzz provide me guidelines where i am getting wrong?
TOPparCOMPONENT DESP,,,,,, INS. LOT #, , , , , , MIC,,,,,,,,,, MIC VALUEparENDTOPparFINAL
PROTECT
IF &I_FINAL-PRUEFLOS& NE '000000000000'
&I_FINAL-MAKTX(23)&&i_final-prueflos(12Z)&
&I_FINAL-kurztext(25)&
&I_FINAL-original_input(8)&
ELSE
&I_FINAL-MAKTX(23)&
&I_FINAL-kurztext(25)&
&I_FINAL-original_input(8)&
ENDIF
ENDPROTECT
ITEMHEAD
POSITION WINDOW
SIZE WIDTH +0 . 4 CH HEIGHT +1 LN
BOX FRAME 10 TW
BOX HEIGHT '1.35' LN INTENSITY 20
IF &PAGE& = '1'
BOX XPOS '0' CH YPOS '0' CM WIDTH '0' CM HEIGHT '43' LN FRAME '10' TW
For horizontal line at top
BOX XPOS '0' CH YPOS '0' CM WIDTH '75' CH HEIGHT '0' LN FRAME '10' TW
COLUMN LINES...
END OF COLUMN LINES...
BOX XPOS '0' CH YPOS '43' LN WIDTH '75' CH HEIGHT '0' LN FRAME '10'TW
BOX XPOS '75' CH YPOS '0' LN WIDTH '0' CH HEIGHT '43' LN FRAME '10'TW
ELSE
COLUMN LINES...
END OF COLUMN LINES...
BOX XPOS '0' CH YPOS '0' CM WIDTH '0' CM HEIGHT '47' LN FRAME '10' TW
BOX XPOS '0' CH YPOS '0' CM WIDTH '75' CH HEIGHT '0' LN FRAME '10' TW
BOX XPOS '0' CH YPOS '0' CM WIDTH '45' CM HEIGHT '0' LN FRAME '10' TW
BOX XPOS '20' CH YPOS '0' CM WIDTH '0' CM HEIGHT '47' LN FRAME '10' TW
BOX XPOS '0' CH YPOS '47' LN WIDTH '75' CH HEIGHT '0' LN FRAME '10'TW
BOX XPOS '75' CH YPOS '0' LN WIDTH '0' CH HEIGHT '47' LN FRAME '10'TW
ENDIF
LINEFEED
NEWPAGE
NEW-PAGE
provide me guidelines to solve this problem.
Edited by: ricx .s on Mar 13, 2009 5:58 AM -
Hi. Does anyone know how to turn columns into rows ie:
select field1, field2, field3, field4, field5 from table
desired result:
field1 field2
field1 field3
field1 field4
field1 field5
Thank you!Something like this ?
select field1
, case n.l
when 1 then field2
when 2 then field3
when 3 then field4
when 4 then field5
end field
from table
, (select level l from dual connect by level <= 4) n -
hi ,
i need to transpose columns into rows ,
i know i can use the UNION ALL but my num of columns will most likely not be fixed so how can i do that ?
pls adviseThis is from one of the forms link,, i reallyy dont know the link, but i guess this is "adrains" code
SQL> WITH ilv AS (
2 SELECT str || ',' AS str
3 , (LENGTH(str) - LENGTH(REPLACE(str, ','))) + 1 AS no_of_elements
4 FROM t
5 )
6 SELECT RTRIM(str, ',') AS original_string
7 , SUBSTR(str, start_pos, (next_pos-start_pos)) AS single_element
8 , element_no
9 FROM (
10 SELECT ilv.str
11 , nt.column_value AS element_no
12 , INSTR(
13 ilv.str,
14 ',',
15 DECODE(nt.column_value, 1, 0, 1),
16 DECODE(nt.column_value, 1, 1, nt.column_value-1)) + 1 AS start_pos
17 , INSTR(
18 ilv.str,
19 ',',
20 1,
21 DECODE(nt.column_value, 1, 1, nt.column_value)) AS next_pos
22 FROM ilv
23 , TABLE(
24 CAST(
25 MULTISET(
26 SELECT ROWNUM FROM dual CONNECT BY ROWNUM < ilv.no_of_elements
27 ) AS number_ntt )) nt
28 );
ORIGINAL_STRING SINGLE_ELEMENT ELEMENT_NO
X,Y,Z X 1
X,Y,Z Y 2
X,Y,Z Z 3
XXX,Y,ZZ,AAAAA,B,CCC,D,E,F,GGG XXX 1
XXX,Y,ZZ,AAAAA,B,CCC,D,E,F,GGG Y 2
XXX,Y,ZZ,AAAAA,B,CCC,D,E,F,GGG ZZ 3
XXX,Y,ZZ,AAAAA,B,CCC,D,E,F,GGG AAAAA 4
XXX,Y,ZZ,AAAAA,B,CCC,D,E,F,GGG B 5
XXX,Y,ZZ,AAAAA,B,CCC,D,E,F,GGG CCC 6
XXX,Y,ZZ,AAAAA,B,CCC,D,E,F,GGG D 7
XXX,Y,ZZ,AAAAA,B,CCC,D,E,F,GGG E 8
XXX,Y,ZZ,AAAAA,B,CCC,D,E,F,GGG F 9
XXX,Y,ZZ,AAAAA,B,CCC,D,E,F,GGG GGG 10
13 rows selected.
Note that the above SQL performs the following steps:
* determines how many elements are in each string (WITH clause);
* for each string, generates a collection of n elements (TABLE expression), where n is the derived number of elements in the string. Note in particular the use of "less than" in the "CONNECT BY ROWNUM < ilv.no_of_elements" on line 26. In all versions other than 10.1.x, this will need to be "CONNECT BY ROWNUM <= ilv.no_of_elements" (i.e. "less than or equal to"). There is an unusual bug with this row-generation technique in 10.1 that generates an additional row from the CONNECT BY;
* uses the generated rows in a Cartesian Product with the original data to generate n rows per string, based on the above definition of n;
* calculates the start and end position of each element in each string (INSTR); and
* cuts each element from each string (SUBSTR). -
I have data in a table that looks like below:
ColumnA ColumnB
123 abc|cde|fgr
345 def|ght|sew
890 deq|nmk|lop|lip|fre|dwsThere is no limit on how many values you can have in ColumnB and they are pipe delimited.
I need to split this one column into rows as:
ColumnA ColumnB
123 abc
123 cde
123 fgr
345 def
345 ght
890 fre
890 dwsThanks in advance!with sample_data as (
select 123 columna,'abc|cde|fgr' columnb from dual union all
select 345,'def|ght|sew' from dual union all
select 890,'deq|nmk|lop|lip|fre|dws' from dual
select columna,
regexp_substr(columnb,'[^|]+',1,column_value) columnb
from sample_data,
table(
cast(
multiset(
select level
from dual
connect by level <= length(regexp_replace(columnb || '|','[^|]'))
as sys.OdciNumberList
order by columna,
column_value
COLUMNA COLUMNB
123 abc
123 cde
123 fgr
345 def
345 ght
345 sew
890 deq
890 nmk
890 lop
890 lip
890 fre
COLUMNA COLUMNB
890 dws
12 rows selected.
SQL> SY. -
How to convert columns into rows using transpose function
Hi
anybody tell me how to convert columns values into rows using transpose function.Since BluShadow went to all the trouble to put it together, someone should use it.
See the post titled How do I convert rows to columns? here SQL and PL/SQL FAQ
John -
Converting single column content into rows
Hi All,
I have a table containing data in the following format
SNO Content
1 a,ab,aab,b,c
2 a,aac,aab,c,ccb,ee
3 bb,b,c
I have a requirement to convert this into following format
SNO Content
1 a
1 ab
1 aab
1 b
1 c
2 a
3 bb
3 b
3 c
How to acheive this in 10g? Is it possible in 9i?
Please share your thoughts.
Thanks in advance
Regards,
Subbu Stest@ORA92>
test@ORA92> @ver
BANNER
Oracle9i Enterprise Edition Release 9.2.0.1.0 - Production
PL/SQL Release 9.2.0.1.0 - Production
CORE 9.2.0.1.0 Production
TNS for 32-bit Windows: Version 9.2.0.1.0 - Production
NLSRTL Version 9.2.0.1.0 - Production
5 rows selected.
test@ORA92>
test@ORA92> select * from t;
SNO CONTENT
1 a,ab,aab,b,c
2 a,aac,aab,c,ccb,ee
3 bb,b,c
3 rows selected.
test@ORA92>
test@ORA92> select sno,
2 rtrim(substr(lst,
3 instr(lst, ',', 1, iter.pos) + 1,
4 instr(lst, ',', 1, iter.pos + 1) - instr(lst, ',', 1, iter.pos)),
5 ',') content
6 from (select sno, ',' || content || ',' lst from t) csv,
7 (select rownum pos from all_objects) iter
8 where iter.pos <= ((length(csv.lst) - length(replace(csv.lst, ','))) / length(',')) - 1
9 order by sno;
SNO CONTENT
1 a
1 ab
1 aab
1 c
1 b
2 a
2 aab
2 ee
2 ccb
2 c
2 aac
3 bb
3 c
3 b
14 rows selected.
test@ORA92>
test@ORA92>Haven't tried this with a large number of rows, but I suspect it would be slow in that case. Regular expressions should make this simpler and more efficient in 10g.
Cheers,
pratz -
Converting column header and a single record into rows
Lets suppose the table with only one record
col1 col2 col3-----Column Header
d1 d2 d3
i want it in following manner
column_name Column_data----Column Header
col1 d1
col2 d2
col3 d3
for this i have written the query as
select column_name,column_data from (select * from XXX where col1=123)
MODEL RETURN UPDATED ROWS
dimension by(col1)
measures (col1,0 column_name,0 column_data )
ignore nav
rules ITERATE(167)
column_name[iteration_number]=cast( iteration_number as varchar2(3)),
column_data[iteration_number]=cast( iteration_number as int)
here i am just assigning iteration_number but i want to assign name of the column and its value;
Note:--table contains nearly 200 columnsSomething like (using one of my own tables):
select column_name colname,
decode(column_name,'RUN_NO',to_char(run_no),
'SEARCH_ID',to_char(search_id),
'PROCESSING_START_DATE',to_char(processing_start_date,'DDMMYYYYHH24MISS'),
'PROCESSING_END_DATE',to_char(processing_end_date,'DDMMYYYYHH24MISS'),
'NO_OF_MATCHES',to_char(no_of_matches),
'ERROR_DETAILS',error_details,
'ADVREP_TIMESTAMP',to_char(advrep_timestamp,'DDMMYYYYHH24MISS')
) colvalue
from partial_search_history, (select rownum r, column_name
from user_tab_columns
where table_name = 'PARTIAL_SEARCH_HISTORY')
where run_no = 356You can build the decode by doing
SELECT ''''||COLUMN_NAME||''',TO_CHAR('||COLUMN_NAME||'),'
FROM user_tab_columns
where table_name = 'PARTIAL_SEARCH_HISTORY'A decode on DATA_TYPE field would allow you to add a format mask for DATE fields if necessary. -
Convert string Column into set or Rows.
Hi,
I have a difficult time converting a string column in the source table to be spitted and insert them as individual rows in target table. The source sting is separated by "-" character. And the source sting can have any number of "-" which may result in any number of out put rows.
e.g. "- IL - NY - NJ - KY - TX" "RAY" should look like
RAY IL
RAY NY
RAY NJ
RAY KY
RAY TX
in the target table.
Thanks,
Ashvin.
Edited by: user591315 on Mar 13, 2009 8:03 AMThis is working.
All you need to do in owb is pull the table function operator , config it take a ref-cursor and also give the attribute names that are declared in the type record to each of the out put variables in the table function operator.(in this case-8 variables).
Thanks,
Ashvin.
CREATE OR REPLACE
type RDW.STAFF_MEMBER_BREAKDOWN_RECORD is object
( survey_i_id number,
survey_id number,
client number,
company number,
csg number,
contact number,
pred_mode_type number,
pred_mode_string varchar2(2000));
CREATE OR REPLACE
type RDW.STAFF_MEMBER_BREAKDOWN_TABLE
is table of staff_member_breakdown_record;
CREATE OR REPLACE package RDW.refcur_pkg
as
type staff_teams_cur_type is ref cursor;
end refcur_pkg;
CREATE OR REPLACE function RDW.parse_teams
(staff_teams_cur refcur_pkg.staff_teams_cur_type)
return staff_member_breakdown_table
pipelined
is
v_survey_i_id NUMBER;
v_survey_id NUMBER;
v_client NUMBER;
v_company NUMBER;
v_csg NUMBER;
v_contact NUMBER;
v_pred_mode_type NUMBER;
v_team_members_r VARCHAR2(2000);
v_team_members VARCHAR2(2000);
cnt NUMBER;
exit_flag NUMBER;
begin
loop
fetch staff_teams_cur into v_survey_i_id,v_survey_id ,v_client,v_company,v_csg,v_contact,v_pred_mode_type,v_team_members;
exit when staff_teams_cur%notfound;
exit_flag := 0;
v_team_members_r := ltrim(v_team_members,'#') ;
loop
cnt := instr( v_team_members_r, '#' );
if ( cnt > 0 )
then
v_team_members := substr(v_team_members_r,1,instr(v_team_members_r,'#')-1);
v_team_members_r := substr(v_team_members_r,instr(v_team_members_r,'#')+1);
else
v_team_members := v_team_members_r;
exit_flag := 1;
end if;
pipe row (staff_member_breakdown_record(
to_number(v_survey_i_id),v_survey_id ,v_client,v_company,v_csg,v_contact,v_pred_mode_type, v_team_members));
exit when exit_flag = 1;
end loop;
end loop;
end parse_teams;
/
Maybe you are looking for
-
Report Parameter Default Value at runtime
Post Author: DMiller CA Forum: .NET Hi, I need to be able to create new parameters at runtime and have been somewhat successful in this endeavor. I have one remaining issue that I am hoping that somebody can help me with. Let me explain. When you cre
-
Hello all, I am currently using JCO 2.1.6 oN HP UX 11i to connect to a SAP WAS 6.40. I see that my application is leaking in memory. After quite some troubleshooting, I managed to build a testbed showing that it seems that it is leaking in the memory
-
New IE 11 not correctly identified as IE broswer?
This issue came up today when one person in our group upgraded their browser from IE 10 to IE 11. In our environment we only allow Internet Explorer with some limited Firefox Users as well. The user agent string for IE 11 is different from IE 10 so i
-
Safari won't open in ipad mini ios7 ?
safari on ipad mini ios7 won't launch. in usage data it using 1.1 space. checked restrictions re booting etc. any help is après hated
-
Install Mac OS X Tiger 10.4.11 on a pc
I recently bought a new laptop (not Mac) and I was wondering if I could install Mac OS X Tiger 10.4.11 version (the same version i am running on my mac mini). Or i can only enjoy mac world through dual boot process???? Thanks a lot in advance!!