Convert Rows into single column
Hi,
My employee details table has data like below,
employee_id position department
100 technician 50
100 IT 80
101 Accountant 60
101 Accounting Manager 70
Now i want to covert the rows into column. So my output should be like this.
output:
employee_id position department position1 department1
100 technician 50 IT 80
101 Accountant 60 Accounting Manager 70
Help me on this
Edited by: Vi on Mar 22, 2012 5:36 AM
Edited by: Vi on Mar 22, 2012 5:36 AM
Igor.M wrote:
http:// website link removed /t_converting_rows_columns.htm
Please don't post links to commercial websites that are only trying to sell their products and services. It breaches the terms of use of the forums.
There are usually more suitable websites giving much more valuable information without all the self promotion (google adwords etc. are acceptable), or in the case of this particular question there is a FAQ post that relates to it...
{message:id=9360005}
Similar Messages
-
How to convert rows into single columns in Oracle?
I have table with data like shown below in Oracle database.
P_COLUMN
COLUMN_1
COLUMN_2
COLUMN_3
COLUMN_ 4
COLUMN_5
COLUMN_6
COLUMN_7
COLUMN_8
COLUMN_9
COLUMN_10
1
A1
A2
A3
A4
A5
A6
A7
A8
A9
A10
1
B1
B2
B3
B4
B5
B6
B7
B8
B9
B10
1
C1
C2
C3
C4
C5
C6
C7
C8
C9
C10
2
AA1
AA2
AA3
AA4
AA5
AA6
AA7
AA8
AA9
AA10
2
BB1
BB2
BB3
BB4
BB5
BB6
BB7
BB8
BB9
BB10
I need a query to get one row based on P_COLUMN's value i.e. for P_COLUMN =1, below should be output :-
C_1
C_2
C_3
C_4
C_5
C_6
C_7
C_8
C_9
C_10
C_11
C_12
C_13
C_14
C_15
C_16
C_17
C_18
C_19
C_20
C_21
C_22
C_23
C_24
C_25
C_26
C_27
C_ 28
C_29
C_30
C_31
1
A1
A2
A3
A4
A5
A6
A7
A8
A9
A10
B1
B2
B3
B4
B5
B6
B7
B8
B9
B10
C1
C2
C3
C4
C5
C6
C7
C8
C9
C10
2
AA1
AA2
AA3
AA4
AA5
AA6
AA7
AA8
AA9
AA10
BB1
BB2
BB3
BB4
BB5
BB6
BB7
BB8
BB9
BB10
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
i searched google and found PIVOT, CROSS JOIN etc but could not use those keyword properly.
Thanks in advance.
Note - My DB client version is 11g.Since you have 11G, here's an alternative with the PIVOT clause.
First, set up test data with up to 10 rows:
CREATE TABLE T(P1,C1,C2,C3,C4,C5,C6,C7,C8,C9,C10) AS SELECT
1,1,2,3,4,5,6,7,8,9,10 FROM DUAL;
INSERT INTO T
WITH DATA AS (SELECT LEVEL*10 N FROM DUAL CONNECT BY LEVEL <= 9)
SELECT P1, C1+N, C2+N, C3+N, C4+N, C5+N, C6+N, C7+N, C8+N, C9+N, C10+N
FROM T, DATA;
INSERT INTO T
SELECT P1+1,C1,C2,C3,C4,C5,C6,C7,C8,C9,C10
FROM T
WHERE C1 <= 11;
select * from t order by p1,c1;
P1
C1
C2
C3
C4
C5
C6
C7
C8
C9
C10
1
1
2
3
4
5
6
7
8
9
10
1
11
12
13
14
15
16
17
18
19
20
1
21
22
23
24
25
26
27
28
29
30
1
31
32
33
34
35
36
37
38
39
40
1
41
42
43
44
45
46
47
48
49
50
1
51
52
53
54
55
56
57
58
59
60
1
61
62
63
64
65
66
67
68
69
70
1
71
72
73
74
75
76
77
78
79
80
1
81
82
83
84
85
86
87
88
89
90
1
91
92
93
94
95
96
97
98
99
100
2
1
2
3
4
5
6
7
8
9
10
2
11
12
13
14
15
16
17
18
19
20
Now the SELECT statement using PIVOT:
SELECT * FROM (
SELECT T.*,
ROW_NUMBER() OVER (
PARTITION BY P1
ORDER BY C1,C2,C3,C4,C5,C6,C7,C8,C9,C10
) RN
FROM T
PIVOT(
MAX(C1) C1, MAX(C2) C2, MAX(C3) C3, MAX(C4) C4, MAX(C5) C5,
MAX(C6) C6, MAX(C7) C7, MAX(C8) C8, MAX(C9) C9, MAX(C10) C10
FOR RN IN (1 R1,2 R2,3 R3,4 R4,5 R5,6 R6,7 R7,8 R8,9 R9,10 R10)
P1
R1_C1
R1_C2
R1_C3
R1_C4
R1_C5
R1_C6
R1_C7
R1_C8
R1_C9
R1_C10
R2_C1
R2_C2
R2_C3
R2_C4
R2_C5
R2_C6
R2_C7
R2_C8
R2_C9
R2_C10
R3_C1
R3_C2
R3_C3
R3_C4
R3_C5
R3_C6
R3_C7
R3_C8
R3_C9
R3_C10
R4_C1
R4_C2
R4_C3
R4_C4
R4_C5
R4_C6
R4_C7
R4_C8
R4_C9
R4_C10
R5_C1
R5_C2
R5_C3
R5_C4
R5_C5
R5_C6
R5_C7
R5_C8
R5_C9
R5_C10
R6_C1
R6_C2
R6_C3
R6_C4
R6_C5
R6_C6
R6_C7
R6_C8
R6_C9
R6_C10
R7_C1
R7_C2
R7_C3
R7_C4
R7_C5
R7_C6
R7_C7
R7_C8
R7_C9
R7_C10
R8_C1
R8_C2
R8_C3
R8_C4
R8_C5
R8_C6
R8_C7
R8_C8
R8_C9
R8_C10
R9_C1
R9_C2
R9_C3
R9_C4
R9_C5
R9_C6
R9_C7
R9_C8
R9_C9
R9_C10
R10_C1
R10_C2
R10_C3
R10_C4
R10_C5
R10_C6
R10_C7
R10_C8
R10_C9
R10_C10
1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 -
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. -
Hi All
Need some assistance, i have a table where i want the output to be a single column
ex: select from t1;*
query output_
rownum col_1
1 8217
2 6037
3 5368
4 5543
5 5232
i would like the result to be : *8217,6037,5368,5543,5232*
thanks for your help in advance.
i did look on the web but can't find a solution that is easily understood.Hi,
855161 wrote:
thanks for responding quickly.
the link example seems not to work for me:
below is the information you have requested:Below is some of what I requested. The CREATE TABLE and INSERT statements seem to be missing.
1. duplicate values have no effect
2. list doesn't need any order
3. Version 10.2.0.5.0 For that, I recommend the user-defined aggregate function called STRING_AGG in the Oracle Base page, but called STRAGG by most of the people who use it. You have to copy and run about 60 lines of code from the Oracle Base page, or from AskTom one time, but once you have it installed, the job is as simple as
SELECT MIN (rnum) AS rnum -- ROWNUM isn't a good column name, since it's the same as a pseudo-column
, STRAGG (count_1) AS count_1_list
FROM table_x
;Hundreds of other jobs you have in the future will be just as easy, and you won't have to go through the installation process again.
Relevant information:
1. table columns : count_1
all rows should become a single row with ',' in between
example:
select * from t1;
output:
Rownum count_1
1 8217
2 6037
3 5368
4 5543
5 5232
Desired results :
Rownum count_1
1 8217, 6037, 5368, 5543, 5232
Edited by: 855161 on Dec 11, 2012 1:25 PMThe main problem with STRAGG is that it doesn't create a list in order. You said that's not an issue in this case, but if you ever do need output in order, then the best option in Oracle 10 is the SYS_CONNECT_BY_PATH technique. The main Oracle-Base page shows how to use SYS_CONNECT_BY_PATH in Oracle 9. Of course, that works in all later versions too, but in version 10 a simpler way, using CONNECT_BY_ISLEAF, became available. See the Oracle-Base Comments Page for the eaiser Oracle 10 technique. -
Converting multiple column rows into single column
Hi all Below is my requirement, can you anybody help me solve it. CREATE TABLE TYPE ( c1_type VARCHAR2 (10), c2_type VARCHAR2 (10), c3_type VARCHAR2 (10), c4_type VARCHAR2 (10), c5_type VARCHAR2 (10), c6_type VARCHAR2 (10), c7_type VARCHAR2 (10), c8_type VARCHAR2 (10), c9_type VARCHAR2 (10)); INSERT INTO TYPE VALUES ('Region_D', 'Region_E', 'Region_F', 'Region_D', 'Region_E', 'Region_D', 'Region_M', 'Region_D', 'Region_E'); commit; Acutal output on the below query select * from type; C1_TYPE C2_TYPE C3_TYPE C4_TYPE C5_TYPE C6_TYPE C7_TYPE C8_TYPE C9_TYPE Region_D Region_E Region_F Region_D Region_E Region_D Region_M Region_D Region_E Acutal i am expecting the below output,how to make it using query or using oracle built in function. Region_D Region_D Region_D Region_D Region_E Region_E Region_E Region_F Region_M
Hi,
So, you want to put the column values in alphabetic order, with the earliest value in c1_type and the last in c9_type, is that it?
The fact that you even want to do this says that this is a bad table design. If sorting the values makes sense, then putting them all in the same column on 9 different rows (rather than 9 different columns on 1 row) makes sense.
If you're stuck with the current design, then use SELECT ... UNPIVOT to put them on separate rows, then use the analytic ROW_NUMBER function to assign numbers 1-9 to each value, and finally use SELECT ... PIVOT to put them back into 1 row in that order. If you're not familiar with the built-in UNPIVOT and PIVOT features, look them up in the SQL Language manual, and see the forum FAQ:
https://forums.oracle.com/message/9362005
If you get stuck, post your best attempt (formatted), the exact results you want (formatted), and your Oracle version (e.g. 11.2.0.2.0).
Also, TYPE is an Oracle keyword, so it's not a good table name. Use something like location_type instead. -
Convert rows to single column using t-sql
Hi All,
I have a table with 7 columns, et say..
ID ,PatientName,Date,Time,Room_Num ,Specialized, DoctorName
My source date looks like this..
ID PatientName Date Time Room_Num Specialized DoctorName
1 Sam 10/02/2010 10:00 4 Heart
John
1 Sam 10/02/2010 10:00 4 Lungs
Harris
2 Jones 11/12/2011 11:00 1 Lungs
Bob
3 Jim 12/05/2001 01:00 2 Kidney
Greg
3 Jim 12/05/2001 09:00 2 Eye
Roby
1 Sam 12/22/2010 11:00 1 Heart
John
1 Sam 12/22/2010 11:00 1 Lungs
Harris
My out put should look like this..
ID PatientName Date DoctorName
1 Sam 10/02/2010 John,Harris ( Need to be in 1 row because - ID, PatientName, Date is same)
2 Jones 11/12/2011 Bob ( Need to show as it is due to no repeatition
of ID)
3 Jim 12/05/2001 Greg,Roby ( Need to be in 1 row because - ID, PatientName,
Date is same)
1 Sam 12/22/2010 John,Harris ( Need to be in 1 row because - ID, PatientName, Date is same)
I am using below query to get above results but I am getting wrong results..Need some help in tweaking the query...
SELECT DISTINCT ID ,PatientName , Date
,STUFF(( SELECT ',' + DoctorName
FROM SampleTable ST1
WHERE ST1.ID=ST2.ID
FOR XML PATH('')),1,1,' ') AS DoctorName
FROM SampleTable1 ST2
GROUP BY ID ,PatientName , Date
With above query I am getting results like this...
ID PatientName Date DoctorName
1 Sam 10/02/2010 John,Harris ,John,Harris ( here John,Harris are repeating twice because of 4th row)
2 Jones 11/12/2011 Bob
3 Jim 12/05/2001 Greg,Roby
1 Sam 12/22/2010 John,Harris ,John,Harris ( here John,Harris are repeating twice because of 1st row)
Create table statement:
Create table SampleTable
ID Int null, PatientName varchar(25) null, Date datetime null, Time varchar(10) null, Room_Num int null, Specialized varchar(20) null, DoctorName varchar(20) null
Insert statement:
Insert into sampletable(ID,PatientName,Date,Time,Room_Num,Specialized,DoctorName) Values ( 1,'Sam','10/02/2010' ,'10:00',4 ,'Heart','John')
Insert into sampletable(ID,PatientName,Date,Time,Room_Num,Specialized,DoctorName) Values ( 1, 'Sam','10/02/2010' ,'10:00' , 4 , 'Lungs','Harris')
Insert into sampletable(ID,PatientName,Date,Time,Room_Num,Specialized,DoctorName) Values ( 2, 'Jones' ,'11/12/2011' , '11:00',1, 'Lungs', 'Bob')
Insert into sampletable(ID,PatientName,Date,Time,Room_Num,Specialized,DoctorName) Values ( 3,'Jim' ,'12/05/2001' , '01:00',2,'Kidney' , 'Greg')
Insert into sampletable(ID,PatientName,Date,Time,Room_Num,Specialized,DoctorName) Values ( 3,'Jim' ,'12/05/2001' , '09:00',2,'Eye' , 'Roby')
Insert into sampletable(ID,PatientName,Date,Time,Room_Num,Specialized,DoctorName) Values ( 1,'Sam' ,'12/22/2010' , '11:00',1,'Heart', 'John')
Insert into sampletable(ID,PatientName,Date,Time,Room_Num,Specialized,DoctorName) Values ( 1,'Sam', '12/22/2010' , '11:00',1 , 'Lungs','Harris')
I need help to tweak my query.
Thanks,
RH
sqlWITH cte AS(
SELECT [ID]
,[PatientName]
,[Date]
,[Time]
,[Room_Num]
,[Specialized]
,[DoctorName]
,ROW_NUMBER() OVER(PARTITION BY [ID],[PatientName],[Date] ORDER BY Time) as RowID
FROM [test].[dbo].[SampleTable]
SELECT
cte.[ID]
,cte.[PatientName]
,CAST(cte.[Date] as DATE) as [Date]
,STUFF((SELECT ',' + c.[DoctorName]
FROM cte c
WHERE c.ID=cte.ID AND c.PatientName=cte.[PatientName] AND c.[Date]=cte.[Date]
FOR XML PATH(''),TYPE).value('.','varchar(max)')
,1,1,'') AS [DoctorName]
FROM cte
WHERE cte.RowID=1 -
Rows into single column with comma seperator
Hi Friends,
I have the following Query
select A.tradeid||','||A.TICKER||'|'||A.SUBORDINATION||'|'||A.CUSIP||'|'||A.DOCCLAUSE||'|'||A.CURRENCY from table A
where A.ticker in ('LYME','GAADF') and A.tradeid in('456777')
which is returning
456777,LYME|SENIOR UNSECURED|Z1700990|MM|USD
456777,GAADF|SENIOR UNSECURED|Z1246790|MM|USD
I want the result set as:
456777,LYME|SENIOR UNSECURED|Z1700990|MM|USD,456777,GAADF|SENIOR UNSECURED|Z1246790|MM|USD
Please help me to get the result set
Thanks,
ragu.
Edited by: user533548 on Apr 3, 2009 12:54 AM
Edited by: user533548 on Apr 3, 2009 12:55 AMyou could do
select max (ltrim (sys_connect_by_path (tradeid||'|'||TICKER||'|'||SUBORDINATION||'|'||CUSIP||'|'||DOCCLAUSE||'|'||CURRENCY, ','), ','))
from (
select tradeid
, ticker
, subordination
, cusip
, docclause
, currency
, row_number() over (partition by tradeid
order by null
) rn
from x)
start with rn = 1
connect by rn = prior rn + 1like in
SQL> with x as
2 (
3 select 456777 tradeid ,'LYOE' ticker ,'SENIOR UNSECURED' subordination,'Z1830990' cusip,'MM' docclause,'USD' currency from dual union all
4 select 456777 tradeid ,'GAZDF','SENIOR UNSECURED','Z8446790','MM','USD' from dual
5 )
6 select max (
7 ltrim
8 (sys_connect_by_path (tradeid||'|'||TICKER||'|'||SUBORDINATION||'|'||CUSIP||'|'||DOCCLAUSE||'|'||CURRENCY
9 , ',')
10 , ',')) str
11 from (
12 select tradeid
13 , ticker
14 , subordination
15 , cusip
16 , docclause
17 , currency
18 , row_number() over (partition by tradeid
19 order by null
20 ) rn
21 from x)
22 start with rn = 1
23 connect by rn = prior rn + 1
24
SQL> /
STR
456777|GAZDF|SENIOR UNSECURED|Z8446790|MM|USD,456777|LYOE|SENIOR UNSECURED|Z1830990|MM|USD -
Trying to convert multiple rows into multipe columns within a single row
I am trying to convert data from multiple rows into multiple columns. Let me see if I can paint the picture for you.
Here is a sample of the table i am trying to read from:
Company Name Account
1 Sam 123
1 Sam 234
1 Joe 345
1 Sue 789
1 Sue 987
1 Sue 573
I am trying to put this into a View that would have the data represented as such:
Company Name Acct1 Acct2 Acct3 Acct4
1 Sam 123 234 <null> <null>
1 Joe 345 <null> <null> <null>
1 Sue 789 987 573 <null>
Many thanks in advance for your help!test@XE> --
test@XE> with t as (
2 select 1 as company, 'Sam' as name, 123 as account from dual union all
3 select 1, 'Sam', 234 from dual union all
4 select 1, 'Joe', 345 from dual union all
5 select 1, 'Sue', 789 from dual union all
6 select 1, 'Sue', 987 from dual union all
7 select 1, 'Sue', 573 from dual)
8 --
9 select company,
10 name,
11 max(case when rn = 1 then account else null end) as acct1,
12 max(case when rn = 2 then account else null end) as acct2,
13 max(case when rn = 3 then account else null end) as acct3,
14 max(case when rn = 4 then account else null end) as acct4
15 from (select company,
16 name,
17 account,
18 row_number() over (partition by company, name order by 1) as rn
19 from t)
20 group by company, name;
COMPANY NAM ACCT1 ACCT2 ACCT3 ACCT4
1 Joe 345
1 Sam 234 123
1 Sue 573 789 987
3 rows selected.
test@XE>
test@XE>isotope -
Converting Rows into Column in Oracle 10g
Hi All,
I m using Oracle Version 10.1.0.2.0 - Production
I have requirement to convert rows into column wise as per the following:
My Query is:
WITH t
AS ( SELECT 'A' AS x, 100 AS y FROM DUAL
UNION ALL
SELECT 'B',200 FROM DUAL
SELECT X, Y
FROM t;
X Y
A 100
B 200
My Requirement is
A B
100 200
So any one could help me that how I resolve this.
Regards,
PrasantaDear frank,
Thanks for your support,.
It's working fine for static cases.If the first column is dynamic then how come i will resolve it.
Example:
Create table mytab (ID_C Varchar2(15),Value_N Number);
Records Population into MyTab table is dynamic.
Insert into mytab values('HO',5000);
Insert Into mytab values('PG1',2400);
Insert Into mytab values('PG2',3000);
Insert Into mytab values('PG3',800);
Commit;
SQL> Select * From MyTab;
IDC_ ValueN_
HO 5000
PG1 2400
PG2 3000
PG3 800
Then My expected result will be as follows
HO PG1 PG2 PG3
5000 2400 3000 800
Thanks and Regards,
Prasanta -
How to convert row into column
Hi All,
My oracle apps version is r12 and db is 10 and i am using Bi publisher version 10g.
Is it possible to convert row into column in Rtf template,
My Query is
SELECT distinct pvs.vendor_site_code,sum(aia.invoice_amount)
FROM ap_invoices_all aia, po_vendors po, po_vendor_sites_all pvs
WHERE aia.org_id = pvs.org_id
AND aia.vendor_id = po.vendor_id
AND aia.vendor_site_id = pvs.vendor_site_id
AND aia.org_id=204
group by pvs.vendor_site_code
And output is like this
Vendor sitecode Invoiceamt
EAM-ERS 79240
STAR GATE - PAY 3245902.31
UPS - HQ 10792040.9
Like this
So in template i need the output like this
Vendor sitecode EAM-ERS STAR GATE - PAY UPS - HQ
Invoiceamt 79240 3245902.31 10792040.9
I tried to achieve the output using sql query but by hardcoding only i have achieved it, so i have tried to convert directly in RTF template.
can any one tell me is it possible.
And if new project is added from the front end ie(now the query will produce 4 rows but now in template i have created only three columns)
Is it possible to add a new column dynamically.
Can any one please guide me and tell me is there any example.
Thanks & regards
SrikkanthTake a look at this post: http://blogs.oracle.com/roller-ui/bsc/spider.jsp?entry=MT%3aENTRY%3a5001
Thanks,
Bipuser -
How to convert rows into columns with decode function
Hi,
How to convert rows into columns with the help of decode function in oracle.
thanks and regards
P Prakashsay
col1 col2
1 10
2 20
3 30
then use
select col1,
sum(decode(col2,10,10)) "new1"
sum(decode(col2,20,20))"new2"
sum(decode(col2,30,30))"new3"
from table_name
group by col1;
we used sum u can use ny function if wont u have to give the column name i.e col2 name also
so i think u got it nw
regards -
CONVERT ROWS INTO COLUMNS IN INTERNAL TABLE
Hi Experts,
I want to convert rows into coloumns in final internal table.
How to do that one. Can any one help me its very urgent.
Regards,
PBS.hi,
Find the below code for changing rows into colums.
data: begin of itab1 occurs 0,
fld,
end of itab1.
data: begin of itab2 occurs 0,
fld1,
fld2,
fld3,
end of itab2.
itab1-fld = 1.
append itab1.
itab1-fld = 2.
append itab1.
itab1-fld = 3.
append itab1.
read table itab1 index 1.
if sy-subrc eq 0.
itab2-fld1 = itab1-fld.
endif.
read table itab1 index 2.
if sy-subrc eq 0.
itab2-fld2 = itab1-fld.
endif.
read table itab1 index 3.
if sy-subrc eq 0.
itab2-fld3 = itab1-fld.
endif.
append itab2.
loop at itab1.
write:/ itab1.
endloop.
loop at itab2.
write:/ itab2.
endloop.
refer the below link for further information
internal table rows to columns
in the final display list how can i change rows to columns and vice versa -
Convert rows into columns nad vice versa in 10g
how to convert rows into columns in 10g??
Qwerty wrote:
see below for rows to column case
SQL> WITH t as
2 (
3 SELECT 'US' test_string FROM DUAL UNION
4 SELECT 'AMERICA' FROM DUAL UNION
5 SELECT'HOLLYWOOD' FROM DUAL UNION
6 SELECT 'WASHINGTON' FROM DUAL
7 )
8 select ltrim (sys_connect_by_path(test_string,','),',') test_string
9 from (
10 SELECT row_number() over(order by test_string) rno, test_string
11 FROM t)
12 WHERE connect_by_isleaf = 1 and rownum=1
13 connect by rno = prior rno+1;
TEST_STRING
AMERICA,HOLLYWOOD,US,WASHINGTONI hope you can do it for column to rows now.That's not really rows to columns. That's rows to a column, which is more commonly called string aggregation.
Rows to columns (or pivot) is more like:
SQL> ed
Wrote file afiedt.buf
1 WITH t as
2 (
3 SELECT 'US' test_string FROM DUAL UNION
4 SELECT 'AMERICA' FROM DUAL UNION
5 SELECT'HOLLYWOOD' FROM DUAL UNION
6 SELECT 'WASHINGTON' FROM DUAL
7 )
8 --
9 select max(decode(rn,1,test_string)) as col_1
10 ,max(decode(rn,2,test_string)) as col_2
11 ,max(decode(rn,3,test_string)) as col_3
12 ,max(decode(rn,4,test_string)) as col_4
13* from (select test_string, row_number() over (order by test_string) as rn from t)
SQL> /
COL_1 COL_2 COL_3 COL_4
AMERICA HOLLYWOOD US WASHINGTON
SQL>And columns to rows (or unpivot) is like:
SQL> ed
Wrote file afiedt.buf
1 WITH t as
2 (
3 SELECT 'US' col_1, 'AMERICA' col_2, 'HOLLYWOOD' col_3, 'WASHINGTON' col_4 FROM DUAL
4 )
5 --
6 select col_1 as col from t union all
7 select col_2 from t union all
8 select col_3 from t union all
9* select col_4 from t
SQL> /
COL
US
AMERICA
HOLLYWOOD
WASHINGTONor...
SQL> ed
Wrote file afiedt.buf
1 WITH t as
2 (
3 SELECT 'US' col_1, 'AMERICA' col_2, 'HOLLYWOOD' col_3, 'WASHINGTON' col_4 FROM DUAL
4 )
5 --
6 select decode(rownum,1,col_1,2,col_2,3,col_3,4,col_4) as col
7* from t, (select * from dual connect by rownum <= 4)
SQL> /
COL
US
AMERICA
HOLLYWOOD
WASHINGTON
SQL> -
Convert multiple rows into single rows for the respective index name
Dear Experts,
I want to convert multiple rows into single rows for the respective index name,
Here is my query.
SELECT user_tables.table_name, user_indexes.index_name, user_ind_columns.column_name
FROM user_tables
JOIN user_indexes on user_indexes.table_name = user_tables.table_name
join USER_IND_COLUMNS on USER_INDEXES.INDEX_NAME = USER_IND_COLUMNS.INDEX_NAME
where user_indexes.index_name not like '%PK%' AND user_ind_columns.column_name NOT LIKE '%SYS%'
ORDER BY user_tables.table_name,user_indexes.index_name;
Result of previous query
TABLE_NAME
INDEX_NAME
COLUMN_NAME
T1
IDX_ACCNTYPCFG1
ENABLE_SERVICE
T1
IDX_ACCTTYPCFG1
ACC_CODE
T1
IDX_ACCTTYPCFG1
ACCTYPE
T2
IDX_ACCTTYPCFGAPP1
ACCTYPE
T3
IDX_ACTLG1
MOBILE_NO
T3
IDX_ACTLG1
ID
Desired output required is
TABLE_NAME
INDEX_NAME
COLUMN_NAME
T1
IDX_ACCNTYPCFG1
ENABLE_SERVICE,ACC_CODE,ACCTYPE
T2
IDX_ACCTTYPCFGAPP1
ACCTYPE
T3
IDX_ACTLG1
ACCTYPE,MOBILE_NO
please help.Maybe
with
user_tables as
(select 'T1' table_name,'IDX_ACCNTYPCFG1' index_name,'ENABLE_SERVICE' column_name from dual union all
select 'T1','IDX_ACCTTYPCFG1','ACC_CODE' from dual union all
select 'T1','IDX_ACCTTYPCFG1','ACCTYPE' from dual union all
select 'T2','IDX_ACCTTYPCFGAPP1','ACCTYPE' from dual union all
select 'T3','IDX_ACTLG1','MOBILE_NO' from dual union all
select 'T3','IDX_ACTLG1','ID' from dual
select table_name,
case index_name when 'IDX_ACCNTYPCFG1' then 'IDX_ACCTTYPCFG1' else index_name end index_name,
listagg(case column_name when 'ID' then 'ACCTYPE' else column_name end,',') within group (order by null) column_name
from user_tables
group by table_name,case index_name when 'IDX_ACCNTYPCFG1' then 'IDX_ACCTTYPCFG1' else index_name end
TABLE_NAME
INDEX_NAME
COLUMN_NAME
T1
IDX_ACCTTYPCFG1
ACCTYPE,ACC_CODE,ENABLE_SERVICE
T2
IDX_ACCTTYPCFGAPP1
ACCTYPE
T3
IDX_ACTLG1
ACCTYPE,MOBILE_NO
Regards
Etbin -
Merge multiple rows into single row (but multiple columns)
How to merge multiple rows into single row (but multiple columns) efficiently.
For example
IDVal IDDesc IdNum Id_Information_Type Attribute_1 Attribute_2 Attribute_3 Attribute_4 Attribute_5
23 asdc 1 Location USA NM ABQ Four Seasons 87106
23 asdc 1 Stats 2300 91.7 8.2 85432
23 asdc 1 Audit 1996 June 17 1200
65 affc 2 Location USA TX AUS Hilton 92305
65 affc 2 Stats 5510 42.7 46 9999
65 affc 2 Audit 1996 July 172 1100
where different attributes mean different thing for each Information_type.
For example for Information_Type=Location
Attribute_1 means Country
Attribute_2 means State and so on.
For example for Information_Type=Stats
Attribute_1 means Population
Attribute_2 means American Ethnicity percentage and so on.
I want to create a view that shows like below:
IDVal IDDesc IDNum Country State City Hotel ZipCode Population American% Other% Area Audit Year AuditMonth Audit Type AuditTime
23 asdc 1 USA NM ABQ FourSeasons 87106 2300 91.7 46 85432 1996 June 17 1200
65 affc 2 USA TX AUS Hilton 92305 5510 42.7 46 9999 1996 July 172 1100
ThanksHi,
That's called Pivoting . The forum FAQ has a section on this subject: {message:id=9360005}
I hope this answers your question.
If not, post your best attempt, along with a little sample data (CREATE TABLE and INSERT statements, relevant columns only), and also post the results you want from that data. (You did post the results you wanted, but they're very hard to read because they're not formatted. Use \ tags, as described in the forum FAQ, below.)
Explain, using specific examples, how you get the results you want from the data given.
Always say which version of Oracle you're using (e.g., 11.2.0.2.0). This is always important, but especially so with pivots.
See the forum FAQ {message:id=9360002}
Maybe you are looking for
-
Error while generating export datasource in Test and Prod system
Hi experts, I tried to generate export datasorce on a cube in teh testing system so that i can use this as a datasource for another cube. I did this so that i can transport all teh relevnt objects from D to T. But Iam gettin teh following erro msg <b
-
I have a 17" mid 2010 macbook pro . I have been running two partitions one with Mavericks 10.9.4 and one with snow leopard 10.6.8. I have recently lost the ability to startup on the snow leopard partition. When booted to mavericks all seem to be fine
-
Sry about this amount of code lines.... but i realy need help... I must insert into a table, the date, like 10/12/2009, and the time, like 22:34:12... but, when i select the date from a datepicker, and insert the time into a text field... i got this
-
XFCE icon issues... [solved]
Hello I recently switched from Arch Gnome to Arch XFCE on my laptop due to the catalyst driver not working with the gnome 3 (i don't like the open source driver - it over heats my cpu even with cpu scaling)... Anyway, my panel applet icons will not a
-
So this goes like this: In the mobile app we can choose the streaming and download quality, but in the desktop we only have the Premium High Quality option. What happens is that if I have a very low internet connection of lets say 128kb, the Desktop