Display rows into columns in table
Hi,
I have a table name Ebiz_Upgrade_Task_Status. Here in table there are 8 rows of data with
pro_id,pobj_id,cemli_id and confirmation.
I am trying to convert all the CONFIRMATION into column wise using the query below
SELECT
PRO_ID,
POBJ_ID,
CEMLI_ID,
max(decode(rownum,1,CONFIRMATION,null)) "CP1",
max(decode(rownum,2,CONFIRMATION,null)) "CP2",
max(decode(rownum,3,CONFIRMATION,null)) "CP3",
max(decode(rownum,4,CONFIRMATION,null)) "CP4"
FROM Ebiz_Upgrade_Task_Status
GROUP BY PRO_ID,POBJ_ID,CEMLI_ID
Am able to see first 2 rows of data as columns, and not able to see the next rows. Please suggest me in modifyin the query
tabel strutuce and data
pro_id obj_id cemli_id confirmation
1 2 3 Yes
1 2 3 No
1 2 3 NA
11 22 33 Yes
11 22 33 NO
11 22 33 NA
Please suggest me in modifying the code
Thanks
Sudhir
Thanks for posting more details.
Using rownum caused your confirmation fields to be null:
SQL> select * from temp1;
PRO_ID POBJ_ID CEMLI_ID CONFIRMATION
111 222 333 yes
111 222 333 no
111 222 333 na
11 22 33 na
11 22 33 no
11 22 33 yes
6 rows selected.
SQL> col cp1 format a10
SQL> col cp2 format a10
SQL> col cp3 format a10
SQL> select pro_id
2 , pobj_id
3 , cemli_id
4 , max(decode(rownum,1,confirmation,null)) "cp1"
5 , max(decode(rownum,2,confirmation,null)) "cp2"
6 , max(decode(rownum,3,confirmation,null)) "cp3"
7 from temp1
8 group by pro_id
9 , pobj_id
10 , cemli_id;
PRO_ID POBJ_ID CEMLI_ID cp1 cp2 cp3
11 22 33
111 222 333 yes no na
SQL> select pro_id
2 , pobj_id
3 , cemli_id
4 , max(decode(confirmation, 'yes',confirmation,null)) "cp1"
5 , max(decode(confirmation, 'no',confirmation,null)) "cp2"
6 , max(decode(confirmation, 'na',confirmation,null)) "cp3"
7 from temp1
8 group by pro_id
9 , pobj_id
10 , cemli_id;
PRO_ID POBJ_ID CEMLI_ID cp1 cp2 cp3
11 22 33 yes no na
111 222 333 yes no na
SQL>
Similar Messages
-
How can I display the rows into columns.
How can I display the rows into columns. I mean
Create table STYLE_M
(Master varchar2(10), child varchar2(10));
Insert itno style_m
('MASTER1','CHILD1');
Insert itno style_m
('MASTER2','CHILD1');
Insert itno style_m
('MASTER2','CHILD2');
Insert itno style_m
('MASTER3','CHILD1');
Insert itno style_m
('MASTER3','CHILD2');
Insert itno style_m
('MASTER3','CHILD3');
Note : The Master may have any number of childs.
I want to display like this..
Master child1, child2, child3, .......(dynamic)
MASTER1 CHILD1
MASTER2 CHILD1 CHILD2
MASTER3 CHILD1 CHILD2 CHILD3
Sorry for disturbing you. Please hlp me out if you have any slution.
Thanks alot.
Ram DontineniHere's a straight SQL "non-dynamic" approach.
This would be used if you knew the amount of children.
SELECT
master,
MAX(DECODE(r, 1, child, NULL)) || ' ' || MAX(DECODE(r, 2, child, NULL)) || ' ' || MAX(DECODE(r, 3, child, NULL)) children
FROM
SELECT
master,
child,
ROW_NUMBER() OVER(PARTITION BY master ORDER BY child) r
FROM
style_m
GROUP BY
master
MASTER CHILDREN
MASTER1 CHILD1
MASTER2 CHILD1 CHILD2
MASTER3 CHILD1 CHILD2 CHILD3 Since you said that the number of children can vary, I incorporated the same logic into a dynamic query.
SET AUTOPRINT ON
VAR x REFCURSOR
DECLARE
v_sql VARCHAR2(1000) := 'SELECT master, ';
v_group_by VARCHAR2(200) := 'FROM (SELECT master, child, ROW_NUMBER() OVER(PARTITION BY master ORDER BY child) r FROM style_m) GROUP BY master';
v_count PLS_INTEGER;
BEGIN
SELECT
MAX(COUNT(*))
INTO v_count
FROM
style_m
GROUP BY
master;
FOR i IN 1..v_count
LOOP
v_sql := v_sql || 'MAX(DECODE(r, ' || i || ', child, NULL))' || ' || '' '' || ';
END LOOP;
v_sql := RTRIM(v_sql, ' || '' '' ||') ||' children ' || v_group_by;
OPEN :x FOR v_sql;
END;
PL/SQL procedure successfully completed.
MASTER CHILDREN
MASTER1 CHILD1
MASTER2 CHILD1 CHILD2
MASTER3 CHILD1 CHILD2 CHILD3I'll point your other thread to this one. -
Pivoting rows into columns in Oracle 10g
Hi,
I want to pivot rows into column in some optimal way.
I don't want to go with the DECODE option as the number of columns can be more than 200.
i have also tried the transpose logic which is making the pl/sql block too huge.
can i directly query the database for the desired output instead of storing the data into some arrays and displaying rows as columns?Hi,
Here's a dynamic way to do this is Oracle 10, using theSQL*Plus @ command to handle the dynamic parts.
First, let's see how we would do this using a static query:
WITH col_cntr AS
SELECT column_name
FROM all_tab_columns
WHERE owner = 'FKULASH'
AND table_name = 'TEST_EMP'
AND column_name NOT IN ('EMP_ID', 'TYPE_VAL')
, unpivoted_data AS
SELECT e.type_val
, c.column_name
, CASE c.column_name
WHEN 'X_AMT' THEN x_amt -- ***** Dynamic section 1 *****
WHEN 'Y_AMT' THEN y_amt -- ***** Dynamic section 1 *****
WHEN 'Z_AMT' THEN z_amt -- ***** Dynamic section 1 *****
END AS v
FROM test_emp e
CROSS JOIN col_cntr c
SELECT column_name AS type_val
, SUM (CASE WHEN type_val = 'Q1' THEN v ELSE 0 END) AS q1 -- ***** Dynamic section 2 *****
, SUM (CASE WHEN type_val = 'Q2' THEN v ELSE 0 END) AS q2 -- ***** Dynamic section 2 *****
, SUM (CASE WHEN type_val = 'Q3' THEN v ELSE 0 END) AS q3 -- ***** Dynamic section 2 *****
, SUM (CASE WHEN type_val = 'Q4' THEN v ELSE 0 END) AS q4 -- ***** Dynamic section 2 *****
FROM unpivoted_data
GROUP BY column_name
ORDER BY column_name
;Column names are hard-coded in two places:
(1) in the sub-query unpivoted_data, we had to know that there were 3 columns to be unpivoted, and that they were called x_amt, y_amt and z_amt. You want to derive all of that from all_tab_columns.
(2) in the main query, we had to know that there would be 4 pivoted columns in the rsult set, and that they would be called q1, q2, q3 and q4. You want to derive all that from the data actually in test_emp.
Instead of hard-coding those 2 dynamic sections, have Preliminary Queries write them for you, a split second before you run the main query, by running this script:
-- Before writing sub-scripts, turn off features designed for human readers
SET FEEDBACK OFF
SET PAGESIZE 0
PROMPT ***** Preliminary Query 1 *****
SPOOL c:\temp\sub_script_1.sql
SELECT ' WHEN '''
|| column_name
|| ''' THEN '
|| LOWER (column_name) AS txt
FROM all_tab_columns
WHERE owner = 'FKULASH'
AND table_name = 'TEST_EMP'
AND column_name NOT IN ('EMP_ID', 'TYPE_VAL')
ORDER BY column_name
SPOOL OFF
PROMPT ***** Preliminary Query 2 *****
SPOOL c:\temp\sub_script_2.sql
SELECT DISTINCT ', SUM (CASE WHEN type_val = '''
|| type_val
|| ''' THEN v ELSE 0 END) AS '
|| LOWER (type_val) AS txt
FROM test_emp
ORDER BY txt
SPOOL OFF
-- After writing sub-scripts, turn on features designed for human readers
SET FEEDBACK 5
SET PAGESIZE 50
-- Main Query:
WITH col_cntr AS
SELECT column_name
FROM all_tab_columns
WHERE owner = 'FKULASH'
AND table_name = 'TEST_EMP'
AND column_name NOT IN ('EMP_ID', 'TYPE_VAL')
, unpivoted_data AS
SELECT e.type_val
, c.column_name
, CASE c.column_name
@c:\temp\sub_script_1
END AS v
FROM test_emp e
CROSS JOIN col_cntr c
SELECT column_name AS type_val
@c:\temp\sub_script_2
FROM unpivoted_data
GROUP BY column_name
ORDER BY column_name
;As you can see, the main query looks exactly like the static query, except that the two dynamic sections have been replaced by sub-scripts. These 2 sub-scripts are written by 2 prelimiary queries, right before the main query.
As others have said, the fact that you're asking this question hints at a poor table design. Perhaps the table should be permanently stored in a form pretty much like unpivoted_data, above. When you need to display it with columns x_amt, y_amt, ..., then pivot it, using GROUP BY type_col. When you need to display it with columns q1, q2, ..., then pivot it using GROUP BY column_name. -
I have table having below records.
empno ename deptno
101 a 10
102 b 20
103 c 10
104 d 20
105 e 30
Normal Output using Group by.
deptno count(*)
10 2
20 2
30 1
I want to display like below(rows into columns)
Requiredl Output
10 20 30
2 2 1
Deptnos are in first row and cout of deptno nos are second row.Check out this thread from the FAQ: {message:id=9360005}
-
Coverting a Row into Columns values
Hi SQL Expert Friends,
I have a row which contains 6 columns where I want that data to be shown in the form of columns as shown here:
From:
select item1, item2, item3, amt1, amt2, amt3 from item_table where sno=1; <----- returns 1 row as below
ITEM1 ITEM2 ITEM3 AMT1 AMT2 AMT3
AAA BBB CCC 10.00 20.00 15.00
Data explanation: item1's (AAA) price is amt1 (10.00), item2's (BBB) price is amt2 (20.00) and item3's (CCC) price is amt3 (15.00). OK.
Now I want that data to convert into columns as shown here:
To:
ITEMS AMT
AAA 10.00
BBB 20.00
CCC 30.00
Please help me friends, I want a SQL to display this data.
I found one query which converts a row into columns, but this does not serve my requirement: [for your reference only]
SQL> select substr( the_string
, decode( level, 1, 1, instr(the_string,',',1,level-1)+1)
, decode( instr(the_string,',',1,level), 0, length(the_string), instr(the_string,',',1,level) - decode( level, 1, 0, instr(the_string,',',1,level-1))-1)
) the_value
from ( select (select item1||','||item2||','|| item3 from item_table where sno=1) ITEMS
from DUAL)
connect by level <= length(the_string)-length(replace(the_string,','))+1
Thanks and REgards,
KiranHi, Kirtan,
kiran wrote:
... I want to learn this query, If you could explain me this how it works, then it will definitely helps me.If you don't understand a query that involves sub-queries, make sure you understand the sub-queries first. Run them separately, and study the results.
In this case, the only sub-query is cntr. Run it separately, like this:
WITH cntr AS
SELECT LEVEL AS n
FROM dual
CONNECT BY LEVEL <= 3
SELECT *
FROM cntr;Output:
` N
1
2
3Why did I use the number 3 in "LEVEL <= 3"? Because each row will be unpivoted onto 3 rows. You could make this number 2, or 5, or 25 if that's how many rows you want in the output.
Once you understand the sub-queries, try running the main query. Include the raw values of all columns that play any part in the output. For example:
WITH cntr AS
SELECT LEVEL AS n
FROM dual
CONNECT BY LEVEL <= 3
SELECT CASE cn.n
WHEN 1 THEN co.city1
WHEN 2 THEN co.city2
WHEN 3 THEN co.city3
END AS cities
, CASE cn.n
WHEN 1 THEN co.population1
WHEN 2 THEN co.population2
WHEN 3 THEN co.population3
END AS population
, co.* -- For testing/understanding only
, cn.* -- For testing/understanding only
FROM continent co
CROSS JOIN cntr cn
WHERE co.country = 'America'
;Output:
` POPUL POPUL POPUL POPUL
CITIES ATION COUNTRY CITY1 CITY2 CITY3 ATION1 ATION2 ATION3 N
Phoenix 20000 America Phoenix Los Angeles Chicago 20000 15000 10000 1
Los Angeles 15000 America Phoenix Los Angeles Chicago 20000 15000 10000 2
Chicago 10000 America Phoenix Los Angeles Chicago 20000 15000 10000 3Why are there 3 rows of output? There are 3 rows in the cntr "table", and 3 rows in the continent table. Of these 3 * 3 = 9 rows, only the 3 rows shown above meet the condition in the WHERE clause. (Comment out the WHERE clause and try it again, if you want to see how that works.) Can you see how you get the output (the first 2 columns above) given all the data in the original columns? If not, ask a more specific question about the part you don't understand. -
Changeing the rows into columns..
Hello Experts,
I have a one requirement which is display rows as columns
for Example:
material
plant
quantity
amount
1000
100
30
60
200
50
100
300
60
120
2000
200
50
100
300
60
120
400
32
64
3000
100
40
80
400
20
40
I need output like this below..
material
100
200
300
400
1000
30
50
60
90
100
120
2000
50
60
32
100
120
64
3000
40
20
80
40
I created the ALV table dynamically using at end of event..
but I am getting the
material
100
200
300
400
1000
30
50
60
90
100
120
2000
60
32
120
64
3000
20
40
the values are missing ..?
I wrote like this:
LOOP AT lt_final INTO ls_final.
ASSIGN COMPONENT 'matnr' OF STRUCTURE <dyn_wa> TO <fs1>.
<fs1> = ls_final-matnr.
ASSIGN COMPONENT ls_final-vkorg OF STRUCTURE <dyn_wa> TO <fs1>.
<fs1> = ls_final-qunaty.
ASSIGN COMPONENT ls_final-vkorg OF STRUCTURE <dyn_wa> TO <fs1>.
<fs1> = ls_final-amount.
AT END OF matnr.
APPEND <dyn_wa> TO <dyn_table>.
CLEAR : <dyn_wa>.
ENDAT.
ENDLOOP.I have written a snippet whose output is:
Take a look at the snippet.
TYPES:
BEGIN OF ty,
a1 TYPE char10,
a2 TYPE char10,
a3 TYPE char10,
a4 TYPE char10,
END OF ty,
BEGIN OF ty2,
a1 TYPE char10,
100 TYPE char10,
200 TYPE char10,
300 TYPE char10,
400 TYPE char10,
no TYPE char10,
END OF ty2.
DATA: lt TYPE TABLE OF ty,
ls TYPE ty,
lt2 TYPE TABLE OF ty2,
ls2 TYPE ty2,
lr TYPE REF TO cl_salv_table.
FIELD-SYMBOLS: <lv> TYPE char10,
<ls2> TYPE ty2.
*material plant quantity amount
ls-a1 = 1000.
ls-a2 = 100. ls-a3 = 30. ls-a4 = 60. APPEND ls TO lt.
ls-a2 = 200. ls-a3 = 50. ls-a4 = 100. APPEND ls TO lt.
ls-a2 = 300. ls-a3 = 60. ls-a4 = 120. APPEND ls TO lt.
ls-a1 = 2000.
ls-a2 = 200. ls-a3 = 50. ls-a4 = 100. APPEND ls TO lt.
ls-a2 = 300. ls-a3 = 60. ls-a4 = 120. APPEND ls TO lt.
ls-a2 = 400. ls-a3 = 32. ls-a4 = 64. APPEND ls TO lt.
ls-a1 = 3000.
ls-a2 = 100. ls-a3 = 40. ls-a4 = 80. APPEND ls TO lt.
ls-a2 = 400. ls-a3 = 20. ls-a4 = 40. APPEND ls TO lt.
TRY.
LOOP AT lt INTO ls.
* index 1 for filling quantity in a3 field
READ TABLE lt2 ASSIGNING <ls2>
WITH KEY a1 = ls-a1
no = 1.
IF sy-subrc NE 0.
APPEND INITIAL LINE TO lt2 ASSIGNING <ls2>.
<ls2>-a1 = ls-a1. <ls2>-no = 1.
ENDIF.
ASSIGN COMPONENT ls-a2 OF STRUCTURE <ls2> TO <lv>.
<lv> = ls-a3.
* index 2 for filling quantity in a4 field
READ TABLE lt2 ASSIGNING <ls2>
WITH KEY a1 = ls-a1
no = 2.
IF sy-subrc NE 0.
APPEND INITIAL LINE TO lt2 ASSIGNING <ls2>.
<ls2>-a1 = ls-a1. <ls2>-no = 2.
ENDIF.
ASSIGN COMPONENT ls-a2 OF STRUCTURE <ls2> TO <lv>.
<lv> = ls-a4.
ENDLOOP.
cl_salv_table=>factory(
IMPORTING
r_salv_table = lr
CHANGING
t_table = lt2
lr->display( ).
CATCH cx_root.
ENDTRY. -
How to display row to columns without using pivot keyword
hi,
could someone help me how to dispaly rows into columns without using pivot keyword and actuall my scenario is,iam having two tables with names and sample data is shown below
ID PROJECT MID MINAME TASKID TASKNAME
1 PROJ1 1 AA 100 PR1_TASK1
1 PROJ1 3 CC 102 PR1_TASK3
1 PROJ1 4 DD 103 PR1_TASK4
1 PROJ1 5 EE 104 PR1_TASK5
1 PROJ1 6 FF 105 PR1_TASK6
2 PROJ2 5 EE 114 PR2_TASK1
2 PROJ2 6 FF 115 PR2_TASK2
2 PROJ2 7 GG 116 PR2_TASK3
2 PROJ2 8 HH 117 PR2_TASK4
2 PROJ2 9 JJ 118 PR2_TASK5
2 PROJ2 10 KK 119 PR2_TASK6
2 PROJ2 1 AA 120 PR2_TASK7
The output should display project and count of tasks in particular milestone as shown below
project AA BB CC DD EE FF GG HH JJ KK
1 2 0 1 5 3 2 0 2 1 0
2 1 2 0 2 1 0 2 4 3 1
Thanks in advance ,
vvrWITH t1 AS
(SELECT 1 ID,
'PROJ1' PROJECT,
1 MID,
'AA' MINAME,
100 TASKID,
'PR1_TASK1' TASKNAME
FROM DUAL
UNION
SELECT 1, 'PROJ1', 3, 'CC', 102, 'PR1_TASK3'
FROM DUAL
UNION
SELECT 1, 'PROJ1', 4, 'DD', 103, 'PR1_TASK4'
FROM DUAL
UNION
SELECT 1, 'PROJ1', 5, 'EE', 104, 'PR1_TASK5'
FROM DUAL
UNION
SELECT 1, 'PROJ1', 6, 'FF', 105, 'PR1_TASK6'
FROM DUAL
UNION
SELECT 2, 'PROJ2', 5, 'EE', 114, 'PR2_TASK1'
FROM DUAL
UNION
SELECT 2, 'PROJ2', 6, 'FF', 115, 'PR2_TASK2'
FROM DUAL
UNION
SELECT 2, 'PROJ2', 7, 'GG', 116, 'PR2_TASK3'
FROM DUAL
UNION
SELECT 2, 'PROJ2', 8, 'HH', 117, 'PR2_TASK4'
FROM DUAL
UNION
SELECT 2, 'PROJ2', 9, 'JJ', 118, 'PR1_TASK5'
FROM DUAL
UNION
SELECT 2, 'PROJ2', 10, 'KK', 119, 'PR1_TASK6'
FROM DUAL
UNION
SELECT 2, 'PROJ2', 1, 'AA', 120, 'PR1_TASK7' FROM DUAL)
SELECT id project,
NVL((SELECT mid
FROM t1
WHERE miname = 'AA'
AND id = t_out.id),
0) AA,
NVL((SELECT mid
FROM t1
WHERE miname = 'BB'
AND id = t_out.id),
0) BB,
NVL((SELECT mid
FROM t1
WHERE miname = 'CC'
AND id = t_out.id),
0) CC,
NVL((SELECT mid
FROM t1
WHERE miname = 'DD'
AND id = t_out.id),
0) DD,
NVL((SELECT mid
FROM t1
WHERE miname = 'EE'
AND id = t_out.id),
0) EE,
NVL((SELECT mid
FROM t1
WHERE miname = 'FF'
AND id = t_out.id),
0) FF,
NVL((SELECT mid
FROM t1
WHERE miname = 'GG'
AND id = t_out.id),
0) GG,
NVL((SELECT mid
FROM t1
WHERE miname = 'HH'
AND id = t_out.id),
0) HH,
NVL((SELECT mid
FROM t1
WHERE miname = 'JJ'
AND id = t_out.id),
0) JJ,
NVL((SELECT mid
FROM t1
WHERE miname = 'KK'
AND id = t_out.id),
0) KK
FROM (SELECT DISTINCT id FROM t1) t_out
PROJECT AA BB CC DD EE FF GG HH JJ KK
1 1 0 3 4 5 6 0 0 0 0
2 1 0 0 0 5 6 7 8 9 10As I understand, you want MID of MINAMEs displayed ? But output is not like yours.. What is exactly your requirements? -
Need help in displaying Rows to Columns
Hi,
I am facing problem in displaying Rows to Columns
I am using pivot function:
select *
from
(select vendor_name
from tablea)
pivot
(count(vendor_name)
for vendor_name in ('a,b,'c'));
its working fine showing vendor_name and count
but when i want to display the output as:(How to include the Salalry column in the query?)
Name:{a b c}
Sal Total:(400,600,800}
Any help will be needful for meNot sure what you mean:
select *
from (select deptno,sal from emp)
pivot(sum(sal) for deptno in (10,20,30))
10 20 30
8750 10875 9400
SQL> SY. -
How to display Rows as Columns in JSF?
I am using dataTable component to get data for my Menu, i have one column in it which returns me the data, because its automatically adding <tr> and <td> tags to it, its showing data in tabular form.
I want to show output Horizontally instead of Vertically the way its showing.
Is their any way i can display Rows as Columns?
Code:
==============================================
<div class="bodyarea">
<div id="location">
<h:dataTable value="#{menuItem.breadCrumb}" var="bread" >
<f:verbatim><ol></f:verbatim>
<h:column>
<f:verbatim><li></f:verbatim>
<h:outputLink id="crumbID" value="#{bread.menuLink}">
<h:outputText id="crumpName" value="#{bread.menuLabel}"/>
</h:outputLink>
<f:verbatim></li></f:verbatim>
</h:column>
<f:verbatim></ol></f:verbatim>
</h:dataTable>
</div>
</div>
Thank youTable is not the html element for you, in this situation.
I would use a dataList component (distributed with myfaces implementation).
dataList is able to return a list of item, and using css you will be able display in a horizontal grid. (otherwise you can use layout="grid" in dataList).
Anyway...my impression is that who made JSF was not really understanding how a html programmer like to write his code.
Why do JSF gives us only the possibility to write list of items throught tables?
Why do JSF prints error messages throught tables?
Why div is not a standard component?
This is an incredible lack...
I see also other incredible lasks...but they don't concern html ;) . -
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 -
Insert multiple rows into a same table from a single record
Hi All,
I need to insert multiple rows into a same table from a single record. Here is what I am trying to do and I need your expertise. I am using Oracle 11g
DataFile
1,"1001,2001,3001,4001"
2,"1002,2002,3002,4002"
The data needs to be loaded as
Field1 Field2
1 1001
1 2001
1 3001
1 4001
2 1002
2 2002
2 3002
2 4002
ThanksYou could use SQL*Loader to load the data into a staging table with a varray column, then use a SQL insert statement to distribute it to the destination table, as demonstrated below.
SCOTT@orcl> host type test.dat
1,"1001,2001,3001,4001"
2,"1002,2002,3002,4002"
SCOTT@orcl> host type test.ctl
load data
infile test.dat
into table staging
fields terminated by ','
( field1
, numbers varray enclosed by '"' and '"' (x))
SCOTT@orcl> create table staging
2 (field1 number,
3 numbers sys.odcinumberlist)
4 /
Table created.
SCOTT@orcl> host sqlldr scott/tiger control=test.ctl log=test.log
SQL*Loader: Release 11.2.0.1.0 - Production on Wed Dec 18 21:48:09 2013
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
Commit point reached - logical record count 2
SCOTT@orcl> column numbers format a60
SCOTT@orcl> select * from staging
2 /
FIELD1 NUMBERS
1 ODCINUMBERLIST(1001, 2001, 3001, 4001)
2 ODCINUMBERLIST(1002, 2002, 3002, 4002)
2 rows selected.
SCOTT@orcl> create table destination
2 (field1 number,
3 field2 number)
4 /
Table created.
SCOTT@orcl> insert into destination
2 select s.field1, t.column_value
3 from staging s, table (s.numbers) t
4 /
8 rows created.
SCOTT@orcl> select * from destination
2 /
FIELD1 FIELD2
1 1001
1 2001
1 3001
1 4001
2 1002
2 2002
2 3002
2 4002
8 rows selected. -
Hi,
I have a table service_request_master where a column has data like
Srid function_activity_id
1001 12,13,14
1002 11,12
1003 null
1004 19
I need to convert function_activity_ids for each row into columns i.e.
for 1001 it would be 12
13
14
for 1002 11
12
and so on..
I wrote this query but it is not workig for multiple rows:
WITH row_count as (
select length(
ltrim(
rtrim(
translate(function_activity_ids, ',1234567890 ', ',')
) + 1 rc,
',' || function_activity_ids || ',' function_activity_ids
from sop_service_request)
select substr(function_activity_ids,
instr(function_activity_ids, ',', 1, rownum) + 1, -- start_pos
instr(function_activity_ids, ',', 1, rownum + 1) -
instr(function_activity_ids, ',', 1, rownum) - 1 -- data_length
) new_data,
function_activity_ids,
rc
from row_count,
all_tables
where rownum <= rccan you pls help..
a generic function would also do...
Thx,..
JPJP,
I don't know exactly what you want with that null column, but try this:
SQL> create table service_request_master
2 as
3 select 1001 srid, '12,13,14' function_activity_id from dual union all
4 select 1002, '11,12' from dual union all
5 select 1003, null from dual union all
6 select 1004, '19' from dual;
Tabel is aangemaakt.
SQL>
SQL> select srm.srid
2 , case numbers.n
3 when 1 then
4 case instr(srm.function_activity_id,',',1,1)
5 when 0 then srm.function_activity_id
6 else substr(srm.function_activity_id,1,instr(srm.function_activity_id,',',1,1)-1)
7 end
8 else substr
9 ( srm.function_activity_id
10 , 1 + instr(srm.function_activity_id,',',1,numbers.n-1)
11 , case instr(srm.function_activity_id,',',1,numbers.n)
12 when 0 then length(srm.function_activity_id)
13 - instr(srm.function_activity_id,',',1,numbers.n-1) + 1
14 else instr(srm.function_activity_id,',',1,numbers.n)
15 - instr(srm.function_activity_id,',',1,numbers.n-1) - 1
16 end
17 )
18 end single_id
19 from service_request_master srm
20 , ( select level n from dual connect by level <= 3 ) numbers
21 where length(srm.function_activity_id) - length(replace(srm.function_activity_id,',')) + 1
22 >= numbers.n
23 order by srm.srid
24 , single_id
25 /
SRID SINGLE_I
1001 12
1001 13
1001 14
1002 11
1002 12
1004 19
6 rijen zijn geselecteerd.Regards,
Rob. -
How to transpose rows into columns?
How do I transpose all rows into columns like how excel does?
For example:
declare @t table (ProductID int, Type varchar(20), Name varchar(20), isAvailable bit)
insert into @t
select 1, 'Type1', 'Product1',1
union
select 2, 'Type1', 'Product2',0
union
select 3, 'Type2', 'Product3',1
union
select 4, 'Type2', 'Product4',0
Results to:
ProductID Type
Name isAvailable
1 Type1
Product1 1
2 Type1
Product2 0
3 Type2
Product3 1
4 Type2
Product4 0
What I need is:
1
2
3
4
Type1
Type1
Type2
Type2
Product1
Product2
Product3
Product4
1
0
1
0
Where 1,2,3,4 at the top are the columns.select
max(case when Type='Type1' and name ='Product1' then Type end) [1],
max(case when Type='Type1' and name ='Product2' then Type end) [2],
max(case when Type='Type2' and name ='Product3' then Type end) [3],
max(case when Type='Type2' and name ='Product4' then Type end) [4]
from @t
union all
select
max(case when Type='Type1' and name ='Product1' then Name end) [1],
max(case when Type='Type1' and name ='Product2' then Name end) [2],
max(case when Type='Type2' and name ='Product3' then Name end) [3],
max(case when Type='Type2' and name ='Product4' then Name end) [4]
from @t
union all
select
max(case when Type='Type1' and name ='Product1' then cast(isAvailable as varchar(5)) end) [1],
max(case when Type='Type1' and name ='Product2' then cast(isAvailable as varchar(5)) end) [2],
max(case when Type='Type2' and name ='Product3' then cast(isAvailable as varchar(5)) end) [3],
max(case when Type='Type2' and name ='Product4' then cast(isAvailable as varchar(5)) end) [4]
from @t
Best Regards,Uri Dimant SQL Server MVP,
http://sqlblog.com/blogs/uri_dimant/
MS SQL optimization: MS SQL Development and Optimization
MS SQL Consulting:
Large scale of database and data cleansing
Remote DBA Services:
Improves MS SQL Database Performance
SQL Server Integration Services:
Business Intelligence -
Hi,
I am trying to change rows into columns, and I have come up with a package that should do this. However, it doesn't work properly, so am wondering if someone can help me out finding out what is wrong.
I am using the emp table owned by Scott in an Oracle database, and I want my output to look like this:
SAL 10 20 30
800 SMITH
950 JAMES
1100 ADAMS
1250 WARD
1250 MARTIN
1300 MILLER
1500 TURNER
1600 ALLEN
2000
2000 beth
2000 Anne
SAL 10 20 30
2450 CLARK
2850 BLAKE
2975 JONES
3000 SCOTT
3000 FORD
5000 KING
I want the deptno to be the header of the column, except from the first column where I want the salary, and then I want all the employees with the specific salary in the specific department number in the row of the right salary and deptno. I saw on the preview screen that you guys will not see the result like I actually have pasted it in here, but I think you will understand what I want. Out of my result I want to read all the employees in deptno 10,20,30,..... and what the salary is.
So far I have come up with this:
create or replace package pivot
as
type rc is ref cursor;
procedure data(p_cursor in out rc );
end;
create or replace package body pivot
as
procedure data(p_cursor in out rc )
is
l_stmt long;
begin
l_stmt := 'select sal';
for x in ( select distinct deptno from emp order by 1 )
loop
l_stmt := l_stmt ||
', max(decode(deptno,' || x.deptno ||', ename)) "x.deptno"';
end loop;
l_stmt := l_stmt || ' from emp group by sal order by sal';
open p_cursor for l_stmt;
end;
end;
variable x refcursor
set autoprint on
exec pivot.data( :x );
The package gets created without any errors, but when I execute it, it returns the following error:
SQL> exec pivot.data( :x );
BEGIN pivot.data( :x ); END;
ERROR at line 1:
ORA-00936: missing expression
ORA-06512: at "SCOTT.PIVOT", line 14
ORA-06512: at line 1
And I can't see what is wrong with it. Can anyone help?
Thank you.
Regards,
AnneJust like this...and a dbms_output after the open cursor to show you the query begin formed.
create or replace package body pivot
as
procedure data(p_cursor in out rc )
is
l_stmt long;
begin
l_stmt := 'select sal';
for x in ( select distinct deptno from emp order by 1 )
loop
l_stmt := l_stmt ||
', max(decode(deptno,''' || x.deptno ||''', ename)) "'||x.deptno||'"';
end loop;
l_stmt := l_stmt || ' from emp group by sal order by sal';
open p_cursor for l_stmt;
dbms_output.put_line(l_stmt);
end;
end;
Package body created.SQL> set serveroutput on
SQL> exec pivot.data(:x);
select sal, max(decode(deptno,'10', ename)) "10", max(decode(deptno,'20',
ename)) "20", max(decode(deptno,'30', ename)) "30" from emp group by sal order
by sal
PL/SQL procedure successfully completed.
SAL 10 20 30
800 SMITH
950 JAMES
1100 ADAMS
1250 WARD
1300 MILLER
1500 TURNER
1600 ALLEN
2450 CLARK
2850 BLAKE
2975 JONES
3000 SCOTT
SAL 10 20 30
5000 KING
12 rows selected.
SQL>
Think that solves the single quote problem. :) -
How to convert rows into column
Hi,
can any one help me how to convert rows into column by pl/sql procedure.
Thanks and Regardshttp://www.oracle.com/technology/oramag/code/tips2004/050304.html
-- dropping the sample table if exists
drop table rowstocol
-- create sample table
create table rowstocol ( name varchar2(20));
-- Inserting rows into sample table
insert into rowstocol values('Amit Zhankar');
insert into rowstocol values('Piyu Yawalkar');
insert into rowstocol values('Piyu Yawalkar');
insert into rowstocol values('Ashish Ghelani');
insert into rowstocol values('Aditi Zhankar');
insert into rowstocol values('Tom Kyte');
insert into rowstocol values('Oracle');
-- Following query should be run to create a sql. This result sql should be run to convert rows to column.
-- The following query uses just the tablename (whose data is to be converted) and name of the column (which is to be converted).
-- Example taken here is table rowstocol, column name.
SELECT cc
FROM (select decode(rn ,1 ,'Select ',null) ||' MAX (CASE WHEN dr = '|| rownum||' THEN DECODE (rn,1, col1) END) '||
decode(rn,maxr,' col1 from ','||'||chr(39)||','||chr(39)||'|| ') cc,rn,maxr
from (SELECT ROWNUM rn,count(0) over() maxr FROM rowstocol) order by rn) trows
union all
select '(SELECT tabs.col1, DENSE_RANK () OVER (ORDER BY col1,rowid) dr,dense_rank() OVER (order by 1) rn
FROM (SELECT NAME col1 FROM rowstocol) tabs ) group by rn' cc from dual;
-- The result of this query will do the reqd conversion from row to column.
-- Replace table rowstocol by your table, column name by your column.
CC
Select MAX (CASE WHEN dr = 1 THEN DECODE (rn,1, col1) END) ||','||
MAX (CASE WHEN dr = 2 THEN DECODE (rn,1, col1) END) ||','||
MAX (CASE WHEN dr = 3 THEN DECODE (rn,1, col1) END) ||','||
MAX (CASE WHEN dr = 4 THEN DECODE (rn,1, col1) END) ||','||
MAX (CASE WHEN dr = 5 THEN DECODE (rn,1, col1) END) ||','||
MAX (CASE WHEN dr = 6 THEN DECODE (rn,1, col1) END) ||','||
MAX (CASE WHEN dr = 7 THEN DECODE (rn,1, col1) END) ||','||
MAX (CASE WHEN dr = 8 THEN DECODE (rn,1, col1) END) col1 from
(SELECT tabs.col1, DENSE_RANK () OVER (ORDER BY col1,rowid) dr,dense_rank() OVER (order by 1) rn
FROM (SELECT NAME col1 FROM rowstocol) tabs ) group by rn
Select MAX (CASE WHEN dr = 1 THEN DECODE (rn,1, col1) END) ||','||
MAX (CASE WHEN dr = 2 THEN DECODE (rn,1, col1) END) ||','||
MAX (CASE WHEN dr = 3 THEN DECODE (rn,1, col1) END) ||','||
MAX (CASE WHEN dr = 4 THEN DECODE (rn,1, col1) END) ||','||
MAX (CASE WHEN dr = 5 THEN DECODE (rn,1, col1) END) ||','||
MAX (CASE WHEN dr = 6 THEN DECODE (rn,1, col1) END) ||','||
MAX (CASE WHEN dr = 7 THEN DECODE (rn,1, col1) END) ||','||
MAX (CASE WHEN dr = 8 THEN DECODE (rn,1, col1) END) col1 from
(SELECT tabs.col1, DENSE_RANK () OVER (ORDER BY col1,rowid) dr,dense_rank() OVER (order by 1) rn
FROM (SELECT NAME col1 FROM rowstocol) tabs ) group by rn;
COL1
Aditi Zhankar,Amit Zhankar,Ashish Ghelani,Oracle,Oracle,Piyu Yawalkar,Piyu Yawalkar,Tom Kyte
Edited by: bhooma on Jan 20, 2009 2:44 AM
Maybe you are looking for
-
Oracle 10g on Windows Server 2003 Standard 64bit
We are running Oracle 10g on Windows Server 2003 Standard 64bit Edition. I am NOT a DBA, so forgive my ignorance. We have 16GB of physical memory in this server. I was told by a DBA that Oracle runs on "a single process", and because of that, he said
-
Horizontal scroll in TreeTable
Hi everybody Is there a possiblity to have a horizontal scrollbar in a JTreeTable? I ran the program from java.sun.com and a personal one, and both never display a horizontal scrollbar. If you program it as always displayed, it is not scrollable anyw
-
When I try rotating the page, it would rotate all of the pages in Adobe Reader. How can I rotate a single page out of the multiple page PDF file?
-
Hi, While editing the unapplied payment advice in FLB1, there is an option for Fast entry, when we double click on any item which needs to be edited, it goes to Change payment advice :line item data, there is an option for fast entry again, in this s
-
Dear Expert I have imported in the .REF file of the Studio the CodeList defined in the Cloud Application. How can I read the content of the Codelist? I need sample code. My code list is defined in the BO Material with the name: this.ToMaterial.Common