Oracle rows to columns/pivot function
Does anyone have any experience using the oracle pivot function? I have two tables:
Table1:Users
Username
jsmith
jjohnson
jbeck
Table2:Job Codes
Username,Job Code
jsmith,JC1
jsmith,JC2
jsmith,JC3
jbeck,JC2
I'm looking to formulate a query that will allow me to join the two tables and at the same time have the rows from table2 represented as columns (assuming a max of 3 job codes):
Username,JobCode1,JobCode2,JobCode3
jsmith,JC1,JC2,JC3
jbeck,JC2,,
It seems like pivot is the function I want to use for this but I can't seem to correlate the examples online back to what we're trying to do.
Hi,
Welcome to the forum!
Whenever you have a problem, please post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) for all tables involved, and also post the results you want from that data.
Explain, using specific examples, how you get those results from that data.
Always say which version of Oracle you're using.
See the forum FAQ {message:id=9360002}
Since I don;t have a version of your table, I'll use the scott.emp table.
I think you want something like this:
WITH got_r_num AS
SELECT job, ename
, ROW_NUMBER () OVER ( PARTITION BY job
ORDER BY ename
) AS r_num
FROM scott.emp
SELECT *
FROM got_r_num
PIVOT ( MIN (ename)
FOR r_num IN ( 1 AS name_1
, 2 AS name_2
, 3 AS name_3
ORDER BY job
;This shows up to 3 names from each job
JOB NAME_1 NAME_2 NAME_3
ANALYST FORD SCOTT
CLERK ADAMS JAMES MILLER
MANAGER BLAKE CLARK JONES
PRESIDENT KING
SALESMAN ALLEN MARTIN TURNERIf there are more than 3 rows with the same job, only the first 3 (in alphabetic order; that's what the analytic ORDER BY clause is doing) will be shown. For example, there's another SALESMAN named WARD who is not shown above.
This query requires Oracle 11.1 (or higher).
For other pivot techniques, including ones for earlier versions, see the forum FAQ {message:id=9360005}
Edited by: Frank Kulash on May 25, 2012 10:20 AM
Added output
Similar Messages
-
Oracle row to column transformation
RDBMS :10.2
Hi,
I have a query that composed of there tables and have joins in them. The final result query is giving is as folllow
ProductId ComProductId description
055X 035X Ladies Companion
055X 055X Adult Companion
112008 112009 Large Companion
112008 112008 Medium Companion
112009 112009 Medium Companion
112009 112008 Large Companion
I want to transform the output in following way, so every product may have its comProductID + Description in single row e.g. Product -ComProductId1 description1 ComProductId2 description2 .....
ProductId ComProductId description ComProductId description
055X 035X Ladies Companion 055X Adult Companion
112008 112009 Large Companion 112008 Medium Companion
Do we have any built in function or way to achieve this goal ?thank you Hoek. it was really great to read. But as per post, i need to use "dynamic Pivot" but it is in 11g and i am using 10 g :(
Following query I am using to get results and that query I wan to transform
SELECT mainitem.productid as ProductId,
compitem.productid as ComProductId,
comp.header as description
FROM comp
JOIN item mainitem ON comp.Id = mainitem.id
JOIN item compitem ON comp.Companion = compitem.id;
this query produce following results
ProductId ComProductId description
055X 035X Ladies Companion
055X 055X Adult Companion
112008 112009 Large Companion
112008 112008 Medium Companion
112009 112009 Medium Companion
112009 112008 Large Companion
I want to transform the output in following way, so every product may have its comProductID + Description in single row e.g. Product -ComProductId1 description1 ComProductId2 description2 .....
ProductId ComProductId description ComProductId description
055X 035X Ladies Companion 055X Adult Companion
112008 112009 Large Companion 112008 Medium Companion
thanks -
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. -
Dynamic SQL Pivoting(Converting Row to Columns)
Hi All,
I am using Oracle 9i (Oracle9i Enterprise Edition Release 9.2.0.1.0 - Production)
and also 10g version
I am facing difficulties to find out the logic for
converting the set of values in one of the columns into the column headings for the entire query.
create TABLE my_tab ( deptno VARCHAR2(5), job VARCHAR2(50), sal NUMBER);
insert into my_tab ( deptno,JOB, sal) values ( 10, 'ANALYST', 23000);
insert into my_tab ( deptno,JOB, sal) values ( 10, 'SALESMAN', 1500);
insert into my_tab ( deptno,JOB, sal) values ( 10, 'CLERK', 3550);
insert into my_tab ( deptno,JOB, sal) values ( 20, 'SALESMAN', 700);
insert into my_tab ( deptno,JOB, sal) values ( 20, 'ANALYST', 4200);
insert into my_tab ( deptno,JOB, sal) values ( 30, 'SALESMAN', 5600);
insert into my_tab ( deptno,JOB, sal) values ( 30, 'CLERK', 12000);
insert into my_tab ( deptno,JOB, sal) values ( 30, 'ANALYST', 19000);
COMMIT;
SELECT * FROM my_tab
DEPTNO ______ JOB ________ SAL
10 ______ ANALYST ________ 23000
10 ______ SALESMAN ________ 1500
10 _______ CLERK ________ 3550
20 _______ SALESMAN ________ 700
20 _______ ANALYST ________ 4200
30 _______ SALESMAN ________ 5600
30 _______ CLERK _______ 12000
30 _______ ANALYST _______ 19000
--And I wish to convert it into this structure:
DEPTNO ________ ANALYST ________ SALESMAN _________ CLERK
10 ________ 23000 ________ 1500 _________ 3550
20 ________ 4200 ________ 700 _________ NULL
30 ________ 19000 ________ 5600 _________ 12000
It may be dynamic. i.e Later i inserted more two records into My_tab.
insert into my_tab ( deptno,JOB, sal) values ( 20, 'CLERK', 3400);
insert into my_tab ( deptno,JOB, sal) values ( 30, 'MANAGER', 48000);
So it should be dynamic.
output is like this.
DEPTNO ________ ANALYST ______ SALESMAN ______ CLERK ______ MANAMGER
10 ________ 23000 ______ 1500 ______ 3550 ______ NULL
20 ________ 4200 ______ 700 ______ 3400 ______ NULL
30 ________ 19000 ______ 5600 ______ 12000 ______ 48000
Please help me regarding this.
With warm regards,
PrasantaHi, Prasanta,
Displaying one column from many rows as many columns on one row is called Pivoting . The following thread shows the basics of how to pivot:
Help for a query to add columns
That example uses the aggregate COUNT function; you'll want SUM (or possibly MIN or MAX) instead.
Getting a dynamic number of columns requires Dynamic SQL . As a simpler alternative to pivoting and dynamic SQL, you might consider String Aggregation , where you concatenate a column from many rows into one big string, to be displayed on one row.
See the following thread for more about string aggregation and other options on pivoting into a variable number of columns:
Re: Report count and sum from many rows into many columns -
Hi
I have folowing scenario. I am getting the result with some query like below :
STATE DAY AVG
NY 02/02 5
NY 02/03 10
NY 02/04 20
NY 02/05 15
IL 02/02 23
IL 02/03 34
IL 02/04 29
IL 02/05 9
FL 02/02 15
FL 02/03 8
FL 02/04 9
FL 02/05 10
and so on..Now I want to convert it from rows to column, but still want to keep STATE column group by :
so the result would be :
STATE 02/02 02/03 02/04 02/05
NY 5 10 20 15
IL 23 34 29 9
FL 15 8 9 10
and so on...I tried using pivot function, but it didnt work. is it even possible? or I will have to do it in code (.net c#) side.
Thanks1 WITH t AS (
2 SELECT 'NY' state,'02/02' mydt,5 myavg FROM dual
3 UNION all
4 SELECT 'NY','02/03',10 FROM dual
5 UNION all
6 SELECT 'NY','02/04',20 FROM dual
7 UNION all
8 SELECT 'NY','02/05',15 FROM dual
9 UNION all
10 SELECT 'IL','02/02',23 FROM dual
11 UNION all
12 SELECT 'IL','02/03',34 FROM dual
13 UNION all
14 SELECT 'IL','02/04',29 FROM dual
15 UNION all
16 SELECT 'IL','02/05',9 FROM dual
17 UNION all
18 SELECT 'FL','02/02',15 FROM dual
19 UNION all
20 SELECT 'FL','02/03',8 FROM dual
21 UNION all
22 SELECT 'FL','02/04',9 FROM dual
23 UNION all
24 SELECT 'FL','02/05',10 FROM dual
25 )
26 SELECT * FROM t
27 PIVOT
28 (
29 SUM(myavg)
30 FOR mydt IN('02/02','02/03','02/04','02/05')
31 )
32* ORDER BY state DESC
SQL> /
ST '02/02' '02/03' '02/04' '02/05'
NY 5 10 20 15
IL 23 34 29 9
FL 15 8 9 10Am I missing some thing here ? -
How to use Pivot function for group range in oracle SQL
Hi,
Good Morning !!!
I need to show the data in the below format. There is 2 columns 1 is State and another one is rate.
State <100 100-199 200-299 300-399 400-499 500-599 600-699 700-799 800-899 900-999 >=1000 Total
AK 1 2 0 4 1 4 4 35 35 4 1 25
AL 0 0 2 27 10 17 35 2 2 35 0 103
AR 0 0 1 0 0 2 2 13 13 2 0 6
AZ 0 1 2 14 2 14 13 3 3 13 0 57
CA 0 0 1 6 2 7 3 4 4 3 0 34
Developed the below query but unable to use the range on pivot function . Please help on this.
(select (SELECT SHORT_DESCRIPTION
FROM CODE_VALUES
WHERE CODE_TYPE_CODE = ad.STATE_TYPE_IND_CODE
AND VALUE = ad.STATE_CODE
) STATE,
nr.rate
FROM neutrals n,
contacts c,
addresses ad,
xref_contacts_addresses xca,
neutral_rates nr
where n.contact_id=c.contact_id
and n.address_id = ad.address_id
and xca.address_id=ad.address_id
and xca.contact_id=c.contact_id
and nr.contact_id = n.contact_id
and nr.rate_frequency='HOUR' )user8564931 wrote:
This solutions is useful and Thanks for your reply.
How can i get the Min value and Max value for each row ?
State <100 100-199 200-299 300-399 400-499 500-599 600-699 700-799 800-899 900-999 >=1000 Total Min Max
IL 0 0 1 5 1 5 40 1 1 40 0 53 $10 $2,500
IN 0 0 0 0 0 0 1 49 49 1 0 3 $70 $1,500This?
WITH t AS
(SELECT 'AL' state, 12 VALUE FROM DUAL
UNION ALL
SELECT 'AL' state, 67 VALUE FROM DUAL
UNION ALL
SELECT 'AL' state, 23 VALUE FROM DUAL
UNION ALL
SELECT 'AL' state, 12 VALUE FROM DUAL
UNION ALL
SELECT 'AL' state, 12 VALUE FROM DUAL
UNION ALL
SELECT 'AL' state, 78 VALUE FROM DUAL
UNION ALL
SELECT 'AL' state, 34 VALUE FROM DUAL
UNION ALL
SELECT 'AL' state, 4 VALUE FROM DUAL
UNION ALL
SELECT 'AL' state, 12 VALUE FROM DUAL
UNION ALL
SELECT 'AL' state, 15 VALUE FROM DUAL
UNION ALL
SELECT 'AZ' state, 6 VALUE FROM DUAL
UNION ALL
SELECT 'AZ' state, 123 VALUE FROM DUAL
UNION ALL
SELECT 'AZ' state, 123 VALUE FROM DUAL
UNION ALL
SELECT 'MA' state, 23 VALUE FROM DUAL
UNION ALL
SELECT 'MA' state, 120 VALUE FROM DUAL
UNION ALL
SELECT 'MA' state, 456 VALUE FROM DUAL
UNION ALL
SELECT 'MA' state, 11 VALUE FROM DUAL
UNION ALL
SELECT 'MA' state, 24 VALUE FROM DUAL
UNION ALL
SELECT 'MA' state, 34 VALUE FROM DUAL
UNION ALL
SELECT 'MA' state, 87 VALUE FROM DUAL
UNION ALL
SELECT 'MA' state, 23 VALUE FROM DUAL
UNION ALL
SELECT 'MA' state, 234 VALUE FROM DUAL
UNION ALL
SELECT 'MA' state, 789 VALUE FROM DUAL
UNION ALL
SELECT 'MH' state, 54321 VALUE FROM DUAL),
-- End of test data
t1 AS
( SELECT state,
NVL (COUNT (DECODE (VALUE, 0, 0)), 0) "<100",
NVL (COUNT (DECODE (VALUE, 1, 1)), 0) "100-199",
NVL (COUNT (DECODE (VALUE, 2, 2)), 0) "200-299",
NVL (COUNT (DECODE (VALUE, 3, 3)), 0) "300-399",
NVL (COUNT (DECODE (VALUE, 4, 4)), 0) "400-499",
NVL (COUNT (DECODE (VALUE, 5, 5)), 0) "500-599",
NVL (COUNT (DECODE (VALUE, 6, 6)), 0) "600-699",
NVL (COUNT (DECODE (VALUE, 7, 7)), 0) "700-799",
NVL (COUNT (DECODE (VALUE, 8, 8)), 0) "800-899",
NVL (COUNT (DECODE (VALUE, 9, 9)), 0) "900-999",
NVL (COUNT (DECODE (VALUE, 10, 10)), 0) ">=1000"
FROM (SELECT state,
CASE
WHEN VALUE < 100 THEN 0
WHEN VALUE BETWEEN 100 AND 199 THEN 1
WHEN VALUE BETWEEN 200 AND 299 THEN 2
WHEN VALUE BETWEEN 300 AND 399 THEN 3
WHEN VALUE BETWEEN 400 AND 499 THEN 4
WHEN VALUE BETWEEN 500 AND 599 THEN 5
WHEN VALUE BETWEEN 600 AND 699 THEN 6
WHEN VALUE BETWEEN 700 AND 799 THEN 7
WHEN VALUE BETWEEN 800 AND 899 THEN 8
WHEN VALUE BETWEEN 900 AND 999 THEN 9
WHEN VALUE >= 1000 THEN 10
END
VALUE
FROM t)
GROUP BY state)
SELECT STATE,
"<100",
"100-199",
"200-299",
"300-399",
"400-499",
"500-599",
"600-699",
"700-799",
"800-899",
"900-999",
">=1000",
"<100"
+ "100-199"
+ "200-299"
+ "300-399"
+ "400-499"
+ "500-599"
+ "600-699"
+ "700-799"
+ "800-899"
+ "900-999"
+ ">=1000"
total,
least("<100",
"100-199",
"200-299",
"300-399",
"400-499",
"500-599",
"600-699",
"700-799",
"800-899",
"900-999",
">=1000") min_val,
greatest("<100",
"100-199",
"200-299",
"300-399",
"400-499",
"500-599",
"600-699",
"700-799",
"800-899",
"900-999",
">=1000") max_val
FROM t1
/ -
Navigation on row and column total in pivot report
Hi All,
I have created a pivot report with 2 dimension columns and one measure and defined row and column total. Now, users wants to have a navigation report on the row/column total so that once he clicks on total value, he can see the detail report. While doing this, he also wants to pass on the 2 dimension column values to detail report.
Is there any workaround for this?
Thanks in advance.use a formula column/row. use RANK function in that. (e.g. Rank([A], asc) will sort the rows based on column A values in ascending order)
you can use this rank in your heading.
But frankly this is not so easy. You have to do it in a very intelligent way, so that rank gives you column number/row number any how.
Have a try and let see if you find a appropriate solution.
Regards,
Rahul -
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 -
Need help with Pivoting rows to columns
Hi,
I need help with pivoting rows to columns. I know there are other posts regarding this, but my requirement is more complex and harder. So, please give me a solution for this.
There are two tables say Table 1 and Table 2.
Table1
name address email identifier
x e g 1
f s d 2
h e n 3
k l b 4
Table2
identifier TRno zno bzid
1 T11 z11 b11
1 T12 z12 b12
1 T13 z13 b13
2 T21 z21 b21
2 T22 z22 b22
As you can see the identifier is the column that we use to map the two tables. The output should be like below
output
name address email identifier TRno1 zno1 bzid1 TRno2 zno2 bzid2 TRno3 zno3 bzid3
x e g 1 T11 z11 b11 T12 z12 b12 T13 z13 b13
f s d 2 T21 z21 b21 t22 z22 b22
Also we do not know exactly how many TRno's, zno's, etc each value in the identifier will have. There may be only 1 TRNO, zno and bzid, or there may be four.
All the values must be in separate columns, and not be just comma delimitted. There are also other conditions that i have to add to restrict the data.
So, can you please tell me what is should use to get the data in the required format? We are using Oracle 10g. Please let me know if u need any more informationSomething like this ?
SCOTT@orcl> ed
Wrote file afiedt.buf
1 select a.name,
2 a.address,
3 a.email,
4 b.* from (
5 select distinct identifier
6 ,max(trno1) trno1
7 ,max(zno1) zno1
8 ,max(bzid1) bzid1
9 ,max(trno2) trno2
10 ,max(zno2) zno2
11 ,max(bzid2) bzid2
12 ,max(trno3) trno3
13 ,max(zno3) zno3
14 ,max(bzid3) bzid3
15 ,max(trno4) trno4
16 ,max(zno4) zno4
17 ,max(bzid4) bzid4
18 from (select identifier
19 ,decode(rn,1,trno,null) trno1
20 ,decode(rn,1,zno,null) zno1
21 ,decode(rn,1,bzid,null) bzid1
22 ,decode(rn,2,trno,null) trno2
23 ,decode(rn,2,zno,null) zno2
24 ,decode(rn,2,bzid,null) bzid2
25 ,decode(rn,3,trno,null) trno3
26 ,decode(rn,3,zno,null) zno3
27 ,decode(rn,3,bzid,null) bzid3
28 ,decode(rn,4,trno,null) trno4
29 ,decode(rn,4,zno,null) zno4
30 ,decode(rn,4,bzid,null) bzid4
31 from (select identifier,
32 trno,bzid,zno,
33 dense_rank() over(partition by identifier order by trno,rownum) rn
34 from table2)
35 order by identifier)
36 group by identifier) b,table1 a
37* where a.identifier=b.identifier
SCOTT@orcl> /
NAME ADDRESS EMAIL IDENTIFIER TRNO1 ZNO1 BZID1 TRNO2 ZNO2 BZID2 TRNO3 ZNO3 BZID3 TRNO4 ZNO4 BZID4
x e g 1 T11 z11 b11 T12 z12 b12 T13 z13 b13
f s d 2 T21 z21 b21 T22 z22 b22
SCOTT@orcl> select * from table1;
NAME ADDRESS EMAIL IDENTIFIER
x e g 1
f s d 2
h e n 3
k l b 4
SCOTT@orcl> select * from table2;
IDENTIFIER TRNO ZNO BZID
1 T11 z11 b11
1 T12 z12 b12
1 T13 z13 b13
2 T21 z21 b21
2 T22 z22 b22
SCOTT@orcl>Regards
Girish Sharma -
Dynamic rows to columns oracle 10g
Hi,
I'm using oracle 10g. I have a requiriement where I need to show rows to columns. I have read through many posts but I couldn't find what I wanted to solve. Lots of them are using decode, since they know the values in one column.
My table has name, value and date. I need to get values for each name at certain date (each distinct date needs to become column). I don't know the name beforehand.
Name, Value, Date
T1 100 06-27-2011 09:00:00
T2 100 06-27-2011 09:00:00
T1 200 06-27-2011 09:15:00
T2 150 06-27-2011 09:15:00
I would also need to use a date range. Someone can request date between 09:00:00 and 09:05:00 or 09:00:00 and 09:30:00 etc.
I need to print
Name, 06-27-2011 09:00:00, 06-27-2011 09:15:00
T1 100 200
T2 100 150
Appreciate any advice on this.
Edited by: user8801143 on 28-Jun-2011 08:23With 10g
see http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:766825833740#2989343200346664698
search for post on February 11, 2011 and reply on February 14, 2011
with 11g see the pivot clause -
Pivot function in Oracle 10g???
Hello everybody,
at the beginning of the week I had a simple problem (I thought that...), but now after trying and trying, I can't find a solution for it. First of all I'm working on Oracle 10g with the version 10.2.0.4.0. I can't change the version, it's standard in the whole company...
At the beginning I have a table like the following one, but please note, that the compartment, the type and the amount are flexible and can change at any time:
comp type amount
a1 6280 10
a2 6280 20
a2 4810 15
a2 1147 12
a3 6280 33
Now I want the table to look like this:
a1 a2 a3
1147 0 12 0
4810 0 15 0
6280 10 20 33
A simple question in Excel for example, I just use the pivot function and have it fixed within 10seconds. But how can I do sth. like this in Oracle with simple SQL? Or it can be PL/SQL too, cause I will use this in an APEX application.
Can you please give me a hint or a solution? But as stated before a1, a2, a3 are just examples it is possible that tomorrow a4, a5 and so on are coming. If it is necessary I can also create additional tables and views of course!
Thanks for your help!
Regards
hogeHi Hoge!
Here is your solution:
SELECT TYPE,
sum(a1) AS a1,
sum(a2) AS a2,
sum(a3) AS a3
FROM (SELECT TYPE,
decode(comp, 'a1', amount, 0) AS a1,
decode(comp, 'a2', amount, 0) AS a2,
decode(comp, 'a3', amount, 0) AS a3
FROM test)
GROUP BY TYPE
ORDER BY TYPE; And here is my test case setup:
CREATE TABLE test
(comp VARCHAR2(255),
TYPE NUMBER,
amount NUMBER);
INSERT INTO test(comp, TYPE, amount) VALUES('a1', 6280, 10);
INSERT INTO test(comp, TYPE, amount) VALUES('a2', 6280, 20);
INSERT INTO test(comp, TYPE, amount) VALUES('a2', 4810, 15);
INSERT INTO test(comp, TYPE, amount) VALUES('a2', 1147, 12);
INSERT INTO test(comp, TYPE, amount) VALUES('a3', 6280, 33);
commit;Best regards,
Matt -
Convert rows to Columns in Oracle
Hi,
The table like
Trx Date
PO121 23/11/2008
PO122 24/11/2008
PO123 25/11/2008
I want to convert all the rows to columns like the below table
PO121 23/11/2008 PO122 24/11/2008 PO123 25/11/2008
Is it possible in Oracle ?Like this
SQL> WITH T
2 AS
3 (
4 SELECT 'PO121 23/11/2008' VAL FROM DUAL
5 UNION ALL
6 SELECT 'PO122 24/11/2008' FROM DUAL
7 UNION ALL
8 SELECT 'PO123 25/11/2008' FROM DUAL
9 )
10 SELECT MAX(DECODE(RNO,1,VAL)) VAL1, MAX(DECODE(RNO,2,VAL)) VAL2, MAX(DECODE(RNO,3,VAL)) VAL3
11 FROM (SELECT ROW_NUMBER() OVER(ORDER BY VAL) RNO, VAL
12 FROM T)
13 /
VAL1 VAL2 VAL3
PO121 23/11/2008 PO122 24/11/2008 PO123 25/11/2008But beware the number of column must be known. without that you cant do it in a static SQL.
Edited by: Karthick_Arp on Nov 10, 2008 1:41 AM -
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 -
Pivot Table Row and Column extension OBIEE 11g
Hi,
I have pivot view report, I have 3 columns,
1) User Name
2) Date
3) Login Count
User Name will be row(Vertical)
Date will be Column heading (Horizontal)
Login Count Measures
So I will get login Count of user wrt date.
Now When I give Wide range of filter i.e, from 1st Jan 2010 to 31st Dec 2010.
There is increase in row also column, when it reaches certain extent it gives error as *'Exceeded configured maximum number of allowed input records.'*
How to change the limit? or there is any fix?
Thanks,
SatheeshHi,
I have also faced the same issue, i have solved by changing DefaultRowsDisplayed count in instance config file.
<Views>
<pivot table>
<DefaultRowsDisplayed>75</DefaultRowsDisplayed>
<pivot table>
</Views>
Paste that code (which i mentioned in my last post) in instanceconfig.xml before these tags
</ServerInstance>
</WebConfig>
But remember you need to restart the presentation service once this is done....
Thanks
Deva -
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?
Maybe you are looking for
-
Fusion drive and backup/restore issues
Hi, since I installed a fusion drive on my early 2008 iMac, consisting of an internal 256GB SSD and an (external FW800) 320GB hard disk, I occassionally run into trouble when system updates are pending. In the past, at worst the time machine backup c
-
Activating Windows 7 & 8.1 when they are virtualized?
Hi Everyone I'm looking at creating to VMs on an ESXi host in my home lab in order to reduce the amount of machines I have lying around. The only reason I am hesitant is because I need to buy individual windows licences which may require me to re-act
-
I've recently completed the design of a postcard and poster for print with Fireworks MX 2004. After speaking with the printer I've now discovered that they only accept CMYK files. Fireworks, as far as I can tell, only outputs to RGB. Is this true? I'
-
Cx_Oracle performance issues
I have a query I execute with Toad that returns ~80k records in less than a minute, the same query executed in Python with cx_Oracle yields abysmal performance and takes over two hours. I have tried a fetchall(), setting the arraysize high and iterat
-
External authentication with OID
I know that OID 10g is capable of performing external authentication against AD, Sun OneDirectory, Novell eDirectory and openLDAP, but what about something else like Oracle Virtual Directory? As I understand, there is an out of the box script that wi