Unpivot or pivot
Hi Guru's
I have to convert the following format into target table
in the source(oracle) i do have customer address and customer phones tables and i have to load the data by joining on customer_id
i/p
phone_number
123
456
789
o/p
home_phone --- mobile_phone -- work_phone
123 null null
null 456 null
null null 789
the type of the phone is there in type table, depending on the Type_id column with phone(phone.type_id=type.type_id) we will get the type of the phone.
any clues
Thanks
R
Edited by: user11410176 on Aug 10, 2011 11:10 AM
Edited by: user11410176 on Aug 11, 2011 8:07 AM
Check this
Re: How to load Matrix report data into basic table data using ODI
Re: Pivoting in ODI
Similar Messages
-
Change multiple column to rows using unpivot or pivot
is there any way I could use pivot or unpivot to get the result show in the graph? From the top table structure to the lower table structure. I could do activity hours and desc separately using unpivot but do not know how to join them together.
Looks like a pivot isn't the best bet, try this:
DECLARE @ActivityReport TABLE ([Employee_ID] [int] NULL, [Week] nvarchar(50) NULL, [Hours_Admin] [float] NULL, [Desc_Admin] [nvarchar](200) NULL, [Hours_HelpDesk] [float] NULL, [Desc_HelpDesk] [nvarchar](200) NULL, [Hours_Support] [float] NULL, [Desc_Support] [nvarchar](200) NULL)
insert into @ActivityReport VALUES (1, 'week1', 5, 'desc', 6, 'desc', 9, 'desc'), (1, 'week2', 5, 'desc1', 6, 'desc1', 9, 'desc1'), (2, 'week1', 5, 'desc', 6, 'desc', 9, 'desc'), (2, 'week2', 5, 'desc2', 6, 'desc2', 9, 'desc2')
SELECT employee_ID, week, hours_admin AS activityHours, 'admin_hours' AS activity_type
FROM @ActivityReport
UNION ALL
SELECT employee_ID, week, hours_helpdesk AS activityHours, 'helpdesk_hours' AS activity_type
FROM @ActivityReport
UNION ALL
SELECT employee_ID, week, hours_support AS activityHours, 'support_hours' AS activity_type
FROM @ActivityReport
ORDER BY Employee_ID, week
Don't forget to mark helpful posts, and answers. It helps others to find relevant posts to the same question. -
I have some tables, only one row, but many columns, like this:
Name Null? Type
column1 NUMBER
column2 VARCHAR2(9)
column3 VARCHAR2(9)
column99 VARCHAR2(9)
I want a SQL to generate this kind of result:
Column1 : 1
Column2 : Oracle
Column3 : DB2
Column99 : SQL Server
This SQL could run in 8i,9i,10g and 11g.
Thanks,In asktom you can search for a function called PRINT_TABLE. I have modified a little to use it as a table function.
drop type table_object
drop type column_object
drop function print_table
create type column_object as object(column_name varchar2(30), column_values varchar2(4000))
create type table_object as table of column_object
create or replace function print_table( p_query in varchar2 ) return table_object pipelined
is
l_theCursor integer default dbms_sql.open_cursor;
l_columnValue varchar2(4000);
l_status integer;
l_descTbl dbms_sql.desc_tab;
l_colCnt number;
begin
execute immediate 'alter session set nls_date_format=''dd-mon-yyyy hh24:mi:ss'' ';
dbms_sql.parse( l_theCursor, p_query, dbms_sql.native );
dbms_sql.describe_columns( l_theCursor, l_colCnt, l_descTbl );
for i in 1 .. l_colCnt loop
dbms_sql.define_column(l_theCursor, i, l_columnValue, 4000);
end loop;
l_status := dbms_sql.execute(l_theCursor);
while ( dbms_sql.fetch_rows(l_theCursor) > 0 ) loop
for i in 1 .. l_colCnt loop
dbms_sql.column_value ( l_theCursor, i, l_columnValue );
pipe row (column_object(l_descTbl(i).col_name,l_columnValue));
end loop;
end loop;
execute immediate 'alter session set nls_date_format=''dd-MON-rr'' ';
return;
exception
when others then
execute immediate 'alter session set nls_date_format=''dd-MON-rr'' ';
raise;
end;
show errSo this would work like this.
SQL> select * from table(print_table('select * from emp'))
2 /
COLUMN_NAME COLUMN_VALUES
EMPNO 1
DEPTNO 1
ENAME Karthick
SAL 80
DOJ 03-dec-2011 04:31:17
JOB
MGR
EMPNO 2
DEPTNO 1
ENAME Karthick_1
SAL 90
DOJ 23-nov-2011 04:31:17
JOB
MGR 1
EMPNO 3
DEPTNO 2
ENAME 1
SAL 80
DOJ 03-dec-2011 04:31:17
JOB
MGR
EMPNO 4
DEPTNO 2
ENAME Ram_1
SAL 90
DOJ 23-nov-2011 04:31:17
JOB
MGR 3
28 rows selected.
SQL>
This SQL could run in 8i,9i,10g and 11g.Oh boy i cant say that :) -
Hi all,
One thing i'm struggling with is the use of the pivot function to pivot a column within PQ. I'm confused on why this happens and why I can't clear it up...but here goes:
I unpivot a set of columns which then gives me two columns (Attribute/Value). I do this because I have repeated columns of data, but it should all fall under one column (think Hours, Hours1, Hours2, etc) - basically the input is coming from a form
that has repeating fields but everything is the same data type.
I make some adjustments to the Attribute columns (Replace items within the Attribute column) then Pivot the two columns again...theoretically I would be able to do this since i just unpivoted the same data, but:
I get tons of errors (usually "There were too many elements in the enumeration to complete the operation".). I'm not entirely sure why this happens and it doesn't seem to make sense to me that I can't UnPivot and Pivot and get the same results
as I originally had.
Any recommendations would be greatly appreciated!
EDIT: I'm choosing "don't aggregate" during the Pivot operation...I get values if I keep some sort of aggregate function, but not the right values (I get a count of the fields instead of the data within the fields)Try this solution:
Once you are to step 3, add an index column that starts at 0 and another index column that starts at 1.
Transform by Pivot Column using the names in Attribute to create new columns and the Values Column set to Value. Under advanced options select Don't Aggregate.
Fill Down the Date column
Filter rows to remove null from Work Time.
Here's the M code from the advanced editor:
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Added Index" = Table.AddIndexColumn(Source, "Index", 0, 1),
#"Added Index1" = Table.AddIndexColumn(#"Added Index", "Index.1", 1, 1),
#"Pivoted Column" = Table.Pivot(#"Added Index1", List.Distinct(#"Added Index1"[Attribute]), "Attribute", "Value"),
#"Filled Down" = Table.FillDown(#"Pivoted Column",{"Date"}),
#"Filtered Rows" = Table.SelectRows(#"Filled Down", each ([Work Time] <> null)),
#"Changed Type" = Table.TransformColumnTypes(#"Filtered Rows",{{"Work Time", type time}})
in
#"Changed Type"
If that doesn't work, this example might do the trick:
http://www.excelguru.ca/blog/2015/01/07/transpose-stacked-tables/ -
Hello All,
I'm trying to get unpivot and pivot to work and can't quite wrap my head around it.
I created a test table which contains DAY, TYPE, DB_ID. In short the backup schedule for a database instance.
Here is my SQL and output.
select b.day_abbrev, c.bck_type_abbrev
from ori_bck_schedules a
left join ori_days b on b.day_id = a.day_id
left join ori_bck_types c on c.bck_type_id = a.bck_type_id
left join ori_databases d on d.database_id = a.database_id
where a.database_id = 369;
DAY_ABBREV BCK_TYPE_ABBREV
SUN FULL
MON INCR
TUE INCR
WED INCR
THU INCR
FRI INCR
SAT FULL I would like to unpivot this table to get 1 rows showing the day of the weekend and the type of backup? And I ultimately would like to have 1 row showing the day of the week and then just rows showing the DB_ID.
Here is what I mean:
SUN MON TUE WED
DB1 FULL FULL INCR
DB2 INCR INCR FULL
DB3 ......The below code is not working for me.
select b.day_abbrev as "day", c.bck_type_abbrev as "type"
from ori_bck_schedules a
left join ori_days b on b.day_id = a.day_id
left join ori_bck_types c on c.bck_type_id = a.bck_type_id
left join ori_databases d on d.database_id = a.database_id
unpivot (a for day_abbrev in (b.day_abbrev as 'D',c. bck_type_abbrev as 'T'))
ORA-01748: only simple column names allowed here
01748. 00000 - "only simple column names allowed here"
*Cause:
*Action:
Error at Line: 45 Column: 31Can anybody help? Thanks in advance for any assistance
Jantry like this. if you are on 11g
SQL> WITH T
2 AS (SELECT 1 ID, TO_CHAR ( (SYSDATE - 6) + LEVEL, 'DY') day_of_week, 10 * LEVEL expense
3 FROM DUAL
4 CONNECT BY LEVEL <= 7)
5 SELECT *
6 FROM T;
ID DAY EXPENSE
1 SUN 10
1 MON 20
1 TUE 30
1 WED 40
1 THU 50
1 FRI 60
1 SAT 70
7 rows selected.
SQL> WITH T
2 AS (SELECT 1 ID, TO_CHAR ( (SYSDATE - 6) + LEVEL, 'DY') day_of_week, 10 * LEVEL expense
3 FROM DUAL
4 CONNECT BY LEVEL <= 7)
5 SELECT *
6 FROM T PIVOT (MAX (expense)
7 FOR day_of_week
8 IN ('SUN', 'MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT'));
ID 'SUN' 'MON' 'TUE' 'WED' 'THU' 'FRI' 'SAT'
1 10 20 30 40 50 60 70
SQL> on 10g
SQL> WITH T
2 AS (SELECT 1 ID, TO_CHAR ( (SYSDATE - 6) + LEVEL, 'DY') day_of_week, 10 * LEVEL expense
3 FROM DUAL
4 CONNECT BY LEVEL <= 7)
5 SELECT ID,
6 MAX (DECODE (day_of_week, 'SUN', expense)) AS sun,
7 MAX (DECODE (day_of_week, 'MON', expense)) AS mon,
8 MAX (DECODE (day_of_week, 'TUE', expense)) AS tue,
9 MAX (DECODE (day_of_week, 'WED', expense)) AS wed,
10 MAX (DECODE (day_of_week, 'THU', expense)) AS thu,
11 MAX (DECODE (day_of_week, 'FRI', expense)) AS fri,
12 MAX (DECODE (day_of_week, 'SAT', expense)) AS sat
13 FROM T
14 GROUP BY ID;
ID SUN MON TUE WED THU FRI SAT
1 10 20 30 40 50 60 70
SQL> G. -
Transpose Data in CLR Stored procedure
We have huge data table(600 columns and 800 rows approx) which we want to transpose 600 rows and 800 columns in a CLR stored procedure.
Currently am using a datatable to the transpose. Could anyone suggest any better options other than datatable which gives better performance?Hi Hari,
If my understand is correct, please consider using unpivot or pivot sql:
http://msdn.microsoft.com/en-us/library/ms177410(v=sql.105).aspx
http://blog.sqlauthority.com/2008/06/07/sql-server-pivot-and-unpivot-table-examples/
Best Regards,
Iric
Please remember to mark the replies as answers if they help and unmark them if they provide no help. -
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. -
Remove duplicay amoung columns
i have the following table
create table cust_phone
cust_id number,
phone1 varchar2(20),
phone2 varchar2(20),
phone3 varchar2(20),
phone4 varchar2(20),
phone5 varchar2(20),
phone6 varchar2(20),
phone7 varchar2(20),
phone8 varchar2(20)
my current data is like:
select * from cust_phone where cust_id=1
1 2784819 2784818 2784816 2784819 2784818 2784819 2784815 2784816
i wnat a update query to nullfy the columns which are duplicate with other in same row, only unique should exist.If you have 11g you can try this.. I used Unpivot and Pivot.
UPDATE cust_phone a SET (phone1,phone2,phone3,phone4,phone5,phone6,phone7,phone8) =
SELECT phone1,phone2,phone3,phone4,phone5,phone6,phone7,phone8 FROM (
select cust_id,ph,nullif(phone_nos,lag(phone_nos,1,0) over (partition by cust_id order by cust_id,phone_nos,ph)) new_phone
from cust_phone
unpivot include nulls(phone_nos for ph in (phone1,phone2,phone3,phone4,phone5,phone6,phone7,phone8))
order by cust_id,phone_nos,ph
pivot (MAX(new_phone) FOR ph IN ('PHONE1' phone1,'PHONE2' phone2,'PHONE3' phone3,'PHONE4' phone4,'PHONE5' phone5,'PHONE6' phone6,'PHONE7' phone7,'PHONE8' phone8))
WHERE cust_id=a.cust_id
)Test run...
PRAZY@11gR2> desc cust_phone;
Name
Null? Type
CUST_ID
NUMBER
PHONE1
VARCHAR2(20)
PHONE2
VARCHAR2(20)
PHONE3
VARCHAR2(20)
PHONE4
VARCHAR2(20)
PHONE5
VARCHAR2(20)
PHONE6
VARCHAR2(20)
PHONE7
VARCHAR2(20)
PHONE8
VARCHAR2(20)
PPRAZY@11gR2> select * from cust_phone;
CUST_ID PHONE1 PHONE2 PHONE3 PHONE4 PHONE5 PHONE6 PHONE7 PHONE8
1 2784819 2784818 2784816 2784819 2784818 2784819 2784815 2784816
Elapsed: 00:00:00.00
PRAZY@11gR2> UPDATE cust_phone a SET (phone1,phone2,phone3,phone4,phone5,phone6,phone7,phone8) =
2 (
3 SELECT phone1,phone2,phone3,phone4,phone5,phone6,phone7,phone8 FROM (
4 select cust_id,ph,nullif(phone_nos,lag(phone_nos,1,0) over (partition by cust_id order by cust_id,phone_nos,ph)) new_phone
5 from cust_phone
6 unpivot include nulls(phone_nos for ph in (phone1,phone2,phone3,phone4,phone5,phone6,phone7,phone8))
7 order by cust_id,phone_nos,ph
8 )
9 pivot (MAX(new_phone) FOR ph IN ('PHONE1' phone1,'PHONE2' phone2,'PHONE3' phone3,'PHONE4' phone4,'PHONE5' phone5,'PHONE6' phone6,'PHONE7' phone7,'PHONE8' phone8))
10 WHERE cust_id=a.cust_id
11 );
1 row updated.
Elapsed: 00:00:00.05
PRAZY@11gR2> select * from cust_phone;
CUST_ID PHONE1 PHONE2 PHONE3 PHONE4 PHONE5 PHONE6 PHONE7 PHONE8
1 2784819 2784818 2784816 2784815
Elapsed: 00:00:00.00
PRAZY@11gR2>And Nimish Garg, I see you have posted around 323 post and still not using \ tag. Thats weird. ?:|
HTH,
Prazy
Edited by: Prazy on Jun 25, 2010 3:47 PM
Made a slight change to order by clause to go with Phone1 first. -
Evaluated order of Pivot and UnPivot in select statement
My research which evaluated order of select statement is below.
1 from
2 where (Join condition)
3 start with
4 connect by
5 where (filter of rows)
6 group by
7 having
8 model
9 select
10 order byMy question is Where Pivot clause and UnPivot clause ?
http://download.oracle.com/docs/cd/E16338_01/server.112/e10592/statements_10002.htmProvided that you can specify columns created by the PIVOT clause both in the select and in the Order By clause, I think the pivot must be executed before them:
SQL> r
1 select job, d10,d20,d30 from emp
2 pivot (sum(sal) for deptno in (10 as D10, 20 as d20, 30 as d30))
3* order by d20
JOB D10 D20 D30
CLERK 800
CLERK 1100
MANAGER 2975
ANALYST 3000
ANALYST 3000
SALESMAN 1600
PRESIDENT 5000
MANAGER 2450
SALESMAN 1500
SALESMAN 1250
CLERK 1300
MANAGER 2850
SALESMAN 1250
CLERK 950
Selezionate 14 righe.
Piano di esecuzione
Plan hash value: 1739977809
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | 14 | 518 | 5 (40)| 00:00:01 |
| 1 | SORT ORDER BY | | 14 | 518 | 5 (40)| 00:00:01 |
| 2 | HASH GROUP BY PIVOT| | 14 | 518 | 5 (40)| 00:00:01 |
| 3 | TABLE ACCESS FULL | EMP | 14 | 518 | 3 (0)| 00:00:01 |
-----------------------------------------------------------------------------Max -
ORA-56901: non-constant expression is not allowed for pivot|unpivot values
Getting following errors
ORA-56901: non-constant expression is not allowed for pivot|unpivot values
ORA-06512: at "APPS.PIVOT_AWARD", line 16
ORA-06512: at line 5
when i run the following function it is giving error as above.
can you please help me
create or replace
Function Pivot_award return sys_refcursor
IS
v_dept VARCHAR2 (20000);
v_query VARCHAR2 (1000);
op_rs sys_refcursor;
BEGIN
SELECT LISTAGG(award_number,',') WITHIN GROUP (ORDER BY award_id)
INTO V_DEPT FROM xxdl.XXDL_CD_SCHEDULE_K_GTT ;
v_query :=
'SELECT *
FROM (
select award_name, award_id,award_number from xxdl.XXDL_CD_SCHEDULE_K_GTT)
PIVOT(max(VAL) for award_number in ('||v_dept||'))';
OPEN op_rs FOR v_query;
return op_rs;
END;
SELECT LISTAGG(award_number,',') WITHIN GROUP (ORDER BY award_id)
INTO V_DEPT FROM xxdl.XXDL_CD_SCHEDULE_K_GTT ;
Result of 1st query is PPE_T_CAPITAL,XIBNG,XIABP,XIABQ,XIABR,XIABS,XIABT,XIABU,XIABV,XIABW,XIAAE,XIAAF,XIAAG,XIAAH,XIAAI,XIAAJ,XIAAK,XIAAL,XIAAM,XIAAN,XIAAO,XIAAP,XIAAQ,XIAAR,XIAAS,XIAAU,XIAAU,XIAAV,XIAAZ,XIABD,XIABE,XIABF,XIABG,XIABH,XIABI,XIABJ,XIABK,XIABL,XIABM,XIABN,XIAAA,XIAAB,XIAAC,XIAAD,XIABY,XIABZ,XIACA,XIACB,XIACC,XIACD,XIACE,XIACF,XIACG,XIACH,XIACI,XIABA,XIAAW,XIAAX,XIAAY,XIACN,XIACT,XIACU,XIACP,AAAEX,XIACW,XIADCHi Frank,
Here is the create table and insert script. This is needed for me to show in report rows to columns.
create table award_test(
AWARD_NUMBER VARCHAR2 (15) ,
AWARD_NAME VARCHAR2(30) ,
TOTAL_PROCEEDS NUMBER ,
EARNING_PROCS NUMBER ,
TOT_PROCS_EARNINGS NUMBER ,
GROSS_PROCS NUMBER ,
PROC_REF_DEF_ESCR NUMBER ,
OTH_UNSP_PROCS NUMBER ,
ISSUANCE_COST NUMBER ,
WORK_CAP_EXP NUMBER
--insert script
Insert into "award_test" (AWARD_NUMBER,AWARD_NAME,TOTAL_PROCEEDS,EARNING_PROCS,TOT_PROCS_EARNINGS,GROSS_PROCS,PROC_REF_DEF_ESCR,OTH_UNSP_PROCS,ISSUANCE_COST,WORK_CAP_EXP) values ('XIAAE','CEFA CP',300000000,200,300000200,300,500,600,0,700);
Insert into "award_test" (AWARD_NUMBER,AWARD_NAME,TOTAL_PROCEEDS,EARNING_PROCS,TOT_PROCS_EARNINGS,GROSS_PROCS,PROC_REF_DEF_ESCR,OTH_UNSP_PROCS,ISSUANCE_COST,WORK_CAP_EXP) values ('XIABG','CEFA K',0,null,0,null,null,null,0,null);
Insert into "award_test" (AWARD_NUMBER,AWARD_NAME,TOTAL_PROCEEDS,EARNING_PROCS,TOT_PROCS_EARNINGS,GROSS_PROCS,PROC_REF_DEF_ESCR,OTH_UNSP_PROCS,ISSUANCE_COST,WORK_CAP_EXP) values ('XIAAS','Escondido Village #3',0,null,0,null,null,null,0,null);
Insert into "award_test" (AWARD_NUMBER,AWARD_NAME,TOTAL_PROCEEDS,EARNING_PROCS,TOT_PROCS_EARNINGS,GROSS_PROCS,PROC_REF_DEF_ESCR,OTH_UNSP_PROCS,ISSUANCE_COST,WORK_CAP_EXP) values ('AAAEX','SU2009A',801806000,null,801806000,null,null,null,1806000,null);
Insert into "award_test" (AWARD_NUMBER,AWARD_NAME,TOTAL_PROCEEDS,EARNING_PROCS,TOT_PROCS_EARNINGS,GROSS_PROCS,PROC_REF_DEF_ESCR,OTH_UNSP_PROCS,ISSUANCE_COST,WORK_CAP_EXP) values ('XIABI','CEFA L-6',17815000,null,17815000,null,null,null,0,null);
Insert into "award_test" (AWARD_NUMBER,AWARD_NAME,TOTAL_PROCEEDS,EARNING_PROCS,TOT_PROCS_EARNINGS,GROSS_PROCS,PROC_REF_DEF_ESCR,OTH_UNSP_PROCS,ISSUANCE_COST,WORK_CAP_EXP) values ('XIAAP','CEFA R',115050508.15,null,115050508.15,null,null,null,0,null);
Insert into "award_test" (AWARD_NUMBER,AWARD_NAME,TOTAL_PROCEEDS,EARNING_PROCS,TOT_PROCS_EARNINGS,GROSS_PROCS,PROC_REF_DEF_ESCR,OTH_UNSP_PROCS,ISSUANCE_COST,WORK_CAP_EXP) values ('XIACG','CEFA D',53150000,null,53150000,null,null,null,0,null);
Insert into "award_test" (AWARD_NUMBER,AWARD_NAME,TOTAL_PROCEEDS,EARNING_PROCS,TOT_PROCS_EARNINGS,GROSS_PROCS,PROC_REF_DEF_ESCR,OTH_UNSP_PROCS,ISSUANCE_COST,WORK_CAP_EXP) values ('XIAAB','Stu Union-1962',0,null,0,null,null,null,0,null);
Insert into "award_test" (AWARD_NUMBER,AWARD_NAME,TOTAL_PROCEEDS,EARNING_PROCS,TOT_PROCS_EARNINGS,GROSS_PROCS,PROC_REF_DEF_ESCR,OTH_UNSP_PROCS,ISSUANCE_COST,WORK_CAP_EXP) values ('XIAAA','Notes Payable-Commercial Paper',350000000,null,350000000,null,null,null,0,null);
Insert into "award_test" (AWARD_NUMBER,AWARD_NAME,TOTAL_PROCEEDS,EARNING_PROCS,TOT_PROCS_EARNINGS,GROSS_PROCS,PROC_REF_DEF_ESCR,OTH_UNSP_PROCS,ISSUANCE_COST,WORK_CAP_EXP) values ('XIABZ','CEFA L-3',9840000,null,9840000,null,null,null,0,null);
Insert into "award_test" (AWARD_NUMBER,AWARD_NAME,TOTAL_PROCEEDS,EARNING_PROCS,TOT_PROCS_EARNINGS,GROSS_PROCS,PROC_REF_DEF_ESCR,OTH_UNSP_PROCS,ISSUANCE_COST,WORK_CAP_EXP) values ('XIAAV','CEFA B',18106540,null,18106540,null,null,null,0,null);
Insert into "award_test" (AWARD_NUMBER,AWARD_NAME,TOTAL_PROCEEDS,EARNING_PROCS,TOT_PROCS_EARNINGS,GROSS_PROCS,PROC_REF_DEF_ESCR,OTH_UNSP_PROCS,ISSUANCE_COST,WORK_CAP_EXP) values ('XIAAI','Medium Term Notes - Tranche 3',50000000,null,50000000,null,null,null,0,null);
Insert into "award_test" (AWARD_NUMBER,AWARD_NAME,TOTAL_PROCEEDS,EARNING_PROCS,TOT_PROCS_EARNINGS,GROSS_PROCS,PROC_REF_DEF_ESCR,OTH_UNSP_PROCS,ISSUANCE_COST,WORK_CAP_EXP) values ('XIAAZ','Recycling Pool',473379904.44,null,473379904.44,null,null,null,0,null);
Insert into "award_test" (AWARD_NUMBER,AWARD_NAME,TOTAL_PROCEEDS,EARNING_PROCS,TOT_PROCS_EARNINGS,GROSS_PROCS,PROC_REF_DEF_ESCR,OTH_UNSP_PROCS,ISSUANCE_COST,WORK_CAP_EXP) values ('XIAAY','CEFA T2',187550000,null,187550000,null,null,null,0,null);
Insert into "award_test" (AWARD_NUMBER,AWARD_NAME,TOTAL_PROCEEDS,EARNING_PROCS,TOT_PROCS_EARNINGS,GROSS_PROCS,PROC_REF_DEF_ESCR,OTH_UNSP_PROCS,ISSUANCE_COST,WORK_CAP_EXP) values ('XIAAM','GMAC',0,null,0,null,null,null,0,null);
Insert into "award_test" (AWARD_NUMBER,AWARD_NAME,TOTAL_PROCEEDS,EARNING_PROCS,TOT_PROCS_EARNINGS,GROSS_PROCS,PROC_REF_DEF_ESCR,OTH_UNSP_PROCS,ISSUANCE_COST,WORK_CAP_EXP) values ('XIAAU','CEFA A/K',16922982,null,16922982,null,null,null,0,null);
Insert into "award_test" (AWARD_NUMBER,AWARD_NAME,TOTAL_PROCEEDS,EARNING_PROCS,TOT_PROCS_EARNINGS,GROSS_PROCS,PROC_REF_DEF_ESCR,OTH_UNSP_PROCS,ISSUANCE_COST,WORK_CAP_EXP) values ('XIAAC','SU TB 2002A - PARS',50000000,null,50000000,null,null,null,0,null);
Insert into "award_test" (AWARD_NUMBER,AWARD_NAME,TOTAL_PROCEEDS,EARNING_PROCS,TOT_PROCS_EARNINGS,GROSS_PROCS,PROC_REF_DEF_ESCR,OTH_UNSP_PROCS,ISSUANCE_COST,WORK_CAP_EXP) values ('XIABL','CEFA L-9',15490000,null,15490000,null,null,null,0,null);
Insert into "award_test" (AWARD_NUMBER,AWARD_NAME,TOTAL_PROCEEDS,EARNING_PROCS,TOT_PROCS_EARNINGS,GROSS_PROCS,PROC_REF_DEF_ESCR,OTH_UNSP_PROCS,ISSUANCE_COST,WORK_CAP_EXP) values ('XIABY','CEFA L-2',8775000,null,8775000,null,null,null,0,null);
Insert into "award_test" (AWARD_NUMBER,AWARD_NAME,TOTAL_PROCEEDS,EARNING_PROCS,TOT_PROCS_EARNINGS,GROSS_PROCS,PROC_REF_DEF_ESCR,OTH_UNSP_PROCS,ISSUANCE_COST,WORK_CAP_EXP) values ('XIAAJ','Frat 1&2',0,null,0,null,null,null,0,null);
Insert into "award_test" (AWARD_NUMBER,AWARD_NAME,TOTAL_PROCEEDS,EARNING_PROCS,TOT_PROCS_EARNINGS,GROSS_PROCS,PROC_REF_DEF_ESCR,OTH_UNSP_PROCS,ISSUANCE_COST,WORK_CAP_EXP) values ('XIAAW','CEFA S',180727500,null,180727500,null,null,null,-472500,null);
Insert into "award_test" (AWARD_NUMBER,AWARD_NAME,TOTAL_PROCEEDS,EARNING_PROCS,TOT_PROCS_EARNINGS,GROSS_PROCS,PROC_REF_DEF_ESCR,OTH_UNSP_PROCS,ISSUANCE_COST,WORK_CAP_EXP) values ('XIAAQ','Escondido Village #1',0,null,0,null,null,null,0,null);
Insert into "award_test" (AWARD_NUMBER,AWARD_NAME,TOTAL_PROCEEDS,EARNING_PROCS,TOT_PROCS_EARNINGS,GROSS_PROCS,PROC_REF_DEF_ESCR,OTH_UNSP_PROCS,ISSUANCE_COST,WORK_CAP_EXP) values ('XIACW','CEFA U',0,null,0,null,null,null,0,null);
Insert into "award_test" (AWARD_NUMBER,AWARD_NAME,TOTAL_PROCEEDS,EARNING_PROCS,TOT_PROCS_EARNINGS,GROSS_PROCS,PROC_REF_DEF_ESCR,OTH_UNSP_PROCS,ISSUANCE_COST,WORK_CAP_EXP) values ('XIACF','CEFA E',19753227.34,null,19753227.34,null,null,null,-106772.66,null);
Insert into "award_test" (AWARD_NUMBER,AWARD_NAME,TOTAL_PROCEEDS,EARNING_PROCS,TOT_PROCS_EARNINGS,GROSS_PROCS,PROC_REF_DEF_ESCR,OTH_UNSP_PROCS,ISSUANCE_COST,WORK_CAP_EXP) values ('XIACN','CEFA T3',27562758.96,null,27562758.96,null,null,null,-47941.04,null);
Insert into "award_test" (AWARD_NUMBER,AWARD_NAME,TOTAL_PROCEEDS,EARNING_PROCS,TOT_PROCS_EARNINGS,GROSS_PROCS,PROC_REF_DEF_ESCR,OTH_UNSP_PROCS,ISSUANCE_COST,WORK_CAP_EXP) values ('XIAAG','Medium Term Notes - Tranche 1',50000000,null,50000000,null,null,null,0,null);
Insert into "award_test" (AWARD_NUMBER,AWARD_NAME,TOTAL_PROCEEDS,EARNING_PROCS,TOT_PROCS_EARNINGS,GROSS_PROCS,PROC_REF_DEF_ESCR,OTH_UNSP_PROCS,ISSUANCE_COST,WORK_CAP_EXP) values ('PPE_T_CAPITAL','PPE_T_CAPITAL',0,null,0,null,null,null,0,null);
Insert into "award_test" (AWARD_NUMBER,AWARD_NAME,TOTAL_PROCEEDS,EARNING_PROCS,TOT_PROCS_EARNINGS,GROSS_PROCS,PROC_REF_DEF_ESCR,OTH_UNSP_PROCS,ISSUANCE_COST,WORK_CAP_EXP) values ('XIAAK','Frat 3',0,null,0,null,null,null,0,null);
Insert into "award_test" (AWARD_NUMBER,AWARD_NAME,TOTAL_PROCEEDS,EARNING_PROCS,TOT_PROCS_EARNINGS,GROSS_PROCS,PROC_REF_DEF_ESCR,OTH_UNSP_PROCS,ISSUANCE_COST,WORK_CAP_EXP) values ('XIAAF','Tresidder',0,null,0,null,null,null,0,null);
Insert into "award_test" (AWARD_NUMBER,AWARD_NAME,TOTAL_PROCEEDS,EARNING_PROCS,TOT_PROCS_EARNINGS,GROSS_PROCS,PROC_REF_DEF_ESCR,OTH_UNSP_PROCS,ISSUANCE_COST,WORK_CAP_EXP) values ('XIABH','CEFA L',5055000,null,5055000,null,null,null,0,null);
Insert into "award_test" (AWARD_NUMBER,AWARD_NAME,TOTAL_PROCEEDS,EARNING_PROCS,TOT_PROCS_EARNINGS,GROSS_PROCS,PROC_REF_DEF_ESCR,OTH_UNSP_PROCS,ISSUANCE_COST,WORK_CAP_EXP) values ('XIAAU','CEFA A/K',6605655,null,6605655,null,null,null,-74345,null);
Insert into "award_test" (AWARD_NUMBER,AWARD_NAME,TOTAL_PROCEEDS,EARNING_PROCS,TOT_PROCS_EARNINGS,GROSS_PROCS,PROC_REF_DEF_ESCR,OTH_UNSP_PROCS,ISSUANCE_COST,WORK_CAP_EXP) values ('XIAAD','SU 2024 Bonds',150000000,null,150000000,null,null,null,0,null);Expected output rows to columns (i took first two insert statements)
'XIAAE','CEFA CP',300000000,200,300000200,300,500,600,0,700
'XIABG','CEFA K',0,null,0,null,null,null,0,null
I need to have awardnumber and corresponding details below
'XIAAE' 'XIABG'
'CEFA CP' 'CEFA K'
300000000 0
200 null
300000200 0
This way i need to get all the information vertically with awardnumber. I have written following code but it is not working.
create or replace
Function Pivot_award return sys_refcursor
IS
v_dept VARCHAR2 (20000);
v_query VARCHAR2 (1000);
op_rs sys_refcursor;
BEGIN
SELECT LISTAGG('''' || award_number || '''',',') WITHIN GROUP (ORDER BY award_number)
INTO V_DEPT FROM award_test ;
v_query :=
'SELECT * from award_test
UNPIVOT(VAL for operator in(
AWARD_NAME,
TOTAL_PROCEEDS,
EARNING_PROCS ,
TOT_PROCS_EARNINGS ,
GROSS_PROCS ,
PROC_REF_DEF_ESCR ,
OTH_UNSP_PROCS ,
ISSUANCE_COST ,
WORK_CAP_EXP ))
PIVOT(max(VAL) for award_number in ('||v_dept||'))';
OPEN op_rs FOR v_query;
return op_rs;
END;throwing an error ORA-06502: PL/SQL: numeric or value error: character string buffer too small
ORA-06512: at "APPS.PIVOT_AWARD", line 11
ORA-06512: at line 5
also when i run simple query
SELECT * from award_test
UNPIVOT(VAL for operator in(
AWARD_NAME,
TOTAL_PROCEEDS,
EARNING_PROCS ,
TOT_PROCS_EARNINGS ,
GROSS_PROCS ,
PROC_REF_DEF_ESCR ,
OTH_UNSP_PROCS ,
ISSUANCE_COST ,
WORK_CAP_EXP ))
PIVOT(max(VAL) for award_number in ('PPE_T_CAPITAL','XIBNG','XIABP')) Throwing an error
ora-01790 Expression must have same datatype as correspoding expression
Edited by: 893185 on Nov 10, 2011 2:00 PM -
How to Unpivot, Crosstab, or Pivot using Oracle 9i with PL/SQL?
How to Unpivot, Crosstab, or Pivot using Oracle 9i with PL/SQL?
Here is a fictional sample layout of the data I have from My_Source_Query:
Customer | VIN | Year | Make | Odometer | ... followed by 350 more columns/fields
123 | 321XYZ | 2012 | Honda | 1900 |
123 | 432ABC | 2012 | Toyota | 2300 |
456 | 999PDQ | 2000 | Ford | 45586 |
876 | 888QWE | 2010 | Mercedes | 38332 |
... followed by up to 25 more rows of data from this query.
The exact number of records returned by My_Source_Query is unknown ahead of time, but should be less than 25 even under extreme situations.
Here is how I would like the data to be:
Column1 |Column2 |Column3 |Column4 |Column5 |
Customer | 123 | 123 | 456 | 876 |
VIN | 321XYZ | 432ABC | 999PDQ | 888QWE |
Year | 2012 | 2012 | 2000 | 2010 |
Make | Honda | Toyota | Ford | Mercedes|
Odometer | 1900 | 2300 | 45586 | 38332 |
... followed by 350 more rows with the names of the columns/fields from the My_Source_Query.
From reading and trying many, many, many of the posting on this topic I understand that the unknown number or rows in My_Source_Query can be a problem and have considered working with one row at a time until each row has been converted to a column.
If possible I'd like to find a way of doing this conversion from rows to columns using a query instead of scripts if that is possible. I am a novice at this so any help is welcome.
This is a repost. I originally posted this question to the wrong forum. Sorry about that.The permission level that I have in the Oracle environment is 'read only'. This is also be the permission level of the users of the query I am trying to build.
As requested, here is the 'create' SQL to build a simple table that has the type of data I am working with.
My real select query will have more than 350 columns and the rows returned will be 25 rows of less, but for now I am prototyping with just seven columns that have the different data types noted in my sample data.
NOTE: This SQL has been written and tested in MS Access since I do not have permission to create and populate a table in the Oracle environment and ODBC connections are not allowed.
CREATE TABLE tbl_MyDataSource
(Customer char(50),
VIN char(50),
Year char(50),
Make char(50),
Odometer long,
InvDate date,
Amount currency)
Here is the 'insert into' to populate the tbl_MyDataSource table with four sample records.
INSERT INTO tbl_MyDataSource ( Customer, VIN, [Year], Make, Odometer, InvDate, Amount )
SELECT "123", "321XYZ", "2012", "Honda", "1900", "2/15/2012", "987";
INSERT INTO tbl_MyDataSource ( Customer, VIN, [Year], Make, Odometer, InvDate, Amount )
VALUES ("123", "432ABC", "2012", "Toyota", "2300", "1/10/2012", "6546");
INSERT INTO tbl_MyDataSource ( Customer, VIN, [Year], Make, Odometer, InvDate, Amount )
VALUES ("456", "999PDQ", "2000", "Ford", "45586", "4/25/2002", "456");
INSERT INTO tbl_MyDataSource ( Customer, VIN, [Year], Make, Odometer, InvDate, Amount )
VALUES ("876", "888QWE", "2010", "Mercedes", "38332", "10/13/2010", "15973");
Which should produce a table containing these columns with these values:
tbl_MyDataSource:
Customer VIN Year Make Odometer InvDate Amount
123 | 321XYZ | 2012 | Honda | 1900 | 2/15/2012 | 987.00
123 | 432ABC | 2012 | Toyota | 2300 | 1/10/2012 | 6,546.00
456 | 999PDQ | 2000 | Ford | 45586 | 4/25/2002 | 456.00
876 | 888QWE | 2010 | Mercedes | 38332 | 10/13/2010 | 15,973.00
The desired result is to use Oracle 9i to convert the columns into rows using sql without using any scripts if possible.
qsel_MyResults:
Column1 Column2 Column3 Column4 Column5
Customer | 123 | 123 | 456 | 876
VIN | 321XYZ | 432ABC | 999PDQ | 888QWE
Year | 2012 | 2012 | 2000 | 2010
Make | Honda | Toyota | Ford | Mercedes
Odometer | 1900 | 2300 | 45586 | 38332
InvDate | 2/15/2012 | 1/10/2012 | 4/25/2002 | 10/13/2010
Amount | 987.00 | 6,546.00 | 456.00 | 15,973.00
The syntax in SQL is something I am not yet sure of.
You said:
>
"Don't use the same name or alias for two different things. if you have a table called t, then don't use t as an alais for an in-line view. Pick a different name, like ordered_t, instead.">
but I'm not clear on which part of the SQL you are suggesting I change. The code I posted is something I pieced together from some of the other postings and is not something I full understand the syntax of.
Here is my latest (failed) attempt at this.
select *
from (select * from tbl_MyDataSource) t;
with data as
(select rownum rnum, t.* from (select * from t order by c1) ordered_t), -- changed 't' to 'ordered_t'
rows_to_have as
(select level rr from dual connect by level <= 7 -- number of columns in T
select rnum,
max(decode(rr, 1, c1)),
max(decode(rr, 2, c2)),
max(decode(rr, 3, c3)),
max(decode(rr, 4, c3)),
max(decode(rr, 5, c3)),
max(decode(rr, 6, c3)),
max(decode(rr, 7, c3)),
from data, rows_to_have
group by rnumIn the above code the "select * from tbl_MyDataSource" is a place holder for my select query which runs without error and has these exact number of fields and data types as order shown in the tbl_MyDataSource above.
This code produces the error 'ORA-00936: missing expression'. The error appears to be starting with the 'with data as' line if I am reading my PL/Sql window correctly. Everything above that row runs without error.
Thank you for your great patients and for sharing your considerable depth of knowledge. Any help is gratefully welcomed. -
View API for Pivot/Unpivot - information is missing
Hi,
I'm trying to prepare a report in order to show the column mappings over multiple mapping operators usin the view API. Each operator has its particular problems when crossing from out to in, but most of them can be solved using the view API except of the following:
The Pivot/Unpivot expressions seem not to be documented. ALL_IV_XFORM_MAP_PARAMETERS.transformation_expression is empty for the out parameters (probably due to the 1:n cardinality with the row locator). But where else to look up? I'm unable to cross the bridge from outgroup to ingroup.
With Join operator there is the problem that the transformation expression doesn't contain the ingroup qualification, so, the true source cannot be identified uniquely when the attribute name is repeated in several ingroups.
Both informations must be buried somewhere in the repository. I would be satisfied the receive an "undocumented" solution if it is a real deficit in the view API. Maybe some privileged person could post an enhencement request.
best regards
ThomasNot got a single answer , surprise whether the question is too dumb to ask or JAXB doesn't support feature or my requirement is weird :-) ... in any case let me know please
regards -
I have a column called Qty and AMt
QTY AMT
1 2
1 1
2 2
I need to pivot/unpivot this in such a way that I need a collumn called Measurecode and Measure
MeasureCode Measure
QTY 1
AMT 2
QTY 1
AMT 1
QTY 2
AMT 2
How do I do this?
SPPandeyYou must have a column that uniquely identifies each row in the original table. Otherwise, after unpivot shuffle rows may occur. For example, if you need sorting by Measure column.
-- code #1
;with QA as (
SELECT ID, 1 as Seq, QTY, AMT
from table
union all
SELECT ID, 2, QTY, AMT
from table
SELECT ID,
MeasureCode= case when Seq=1 then 'QTY' else 'AMT' end,
Measure= case when Seq=1 then QTY else AMT end
from QA
order by ID, MeasureCode desc;
or
-- code #2
SELECT ID, MeasureCode, Measure
from table
unpivot (Measure for MeasureCode in (QTY, AMT)) as U;
If there is no column that identifies the rows, you can create temporary column with sequence before the unpivot:
-- code #2 v2
SELECT Seq, MeasureCode, Measure
from (SELECT Seq= row_number() over (order by (SELECT 0)), QTY, AMT from table) as S
unpivot (Measure for MeasureCode in (QTY, AMT)) as U;
José Diz Belo Horizonte, MG - Brasil -
Hello,
I have table T1 which has three columns col1, col2 col3 with values sachin, ramesh, tendulkar resp. I want output same as shown in the screenshot. I want to achieve this using pivot/unpivot operators. Please help.Hi Naomi,
The query you suggested will work of the scenarios where the number of columns are not fixed? I have just given an example but In practicle there can be many more columns to my table like col1,col2,col3.....coln. Please let me know the dynamic query.
Thanks!
Sachin
Naomi's Solution is perfect. If your table does not have fixed columns then there is a design problem. And BTW, In this case how can you have variable columns.
I mean, there is FirstName, MiddelName and LastName. What is left? All the attributes of a name are considered in these three attributes. Apart from these you have Title, Sex and others but those are not part of a Name.
Chaos isn’t a pit. Chaos is a ladder. Many who try to climb it fail and never get to try again. The fall breaks them. And some are given a chance to climb, but they refuse. They cling to the realm, or the gods, or love. Illusions. Only the ladder is real.
The climb is all there is. -
Are Pivot and Unpivot supported in SSRS
We're upgrading from SSRS 2005 to 2008 R2. I'm rewriting some of my reports and using Unpivot. In SQL it works fine when I run it against the database. But in SSRS (BIDS 2008) when I paste the working SQL statement into the Query Builder, I get
an error that "The UNPIVOT SQL construct or statement is not supported". If i click OK and preview my report, it does run. So, I'm not sure why I'm getting this error. Has anyone had experience with this?
Milissa HartwellThats a known issue with query designer in SSRS. You dont need to worry much on it as report still works fine. If you're so concerned on the pop up make query into a stored procedure and call stored procedure from SSRS dataset query. Then you wont get
any of these issue.
Please Mark This As Answer if it helps to solve the issue Visakh ---------------------------- http://visakhm.blogspot.com/ https://www.facebook.com/VmBlogs
Maybe you are looking for
-
My laptop no longer senses my network
When I set up my network about a month ago everything worked fine. Tonight I tried to connect my work laptop to my netwrok, but it doesn't even sense the network. I see all of my neighbor's network's but not my own. I am SO frustrated. Any help w
-
Callable statement with oracle stored procedure error
i'm calling a stored procedure in java with the following code. However i constantly recieve this error so what is going on please HELP Parameter Type Conflict: sqlType=2006 my call statement would be this: call Statement = {call getUserByLogin(?,?,?
-
Object Slection in Reporting tool of MSS
Hi, We have ECC 6.0 Ehp4 and we are using HIS Reporting tool to display the HR data reports in portal in the form of lists. In Portal the path to these reports is My Staff Manager--> Report(in second level Navigation) then it will display reporting i
-
Item Category--Revenue Recognition
Dear All, system is not allowing me to assign revenue recognition field in VOV7 as it is grey field. As well when I tried through IMG/S&D/Basic Function/Account assignement/Revenue Recognition Item Category-Change mode system is showing grey field fo
-
Reader 9.2 works not on Restricted users?
Hello. In a user/pc if i logon with administrator account the Reader 9.2 works without any problem. If i logon with a user account the reader comes with the error "Internal error". This situation was the same with reader 8.1 , 9.0 and now with 9.2. O