UNPIVOT
Can anyone please tell me what's wrong with the following query? I'm using TOAD
SELECT *
FROM bin_data
UNPIVOT (
direction
FOR bin_data
IN (bin_1_data, bin_2_data, bin_3_data)
);I'm getting the following error:
ORA-00933: SQL command not properly ended
Thanks
PS: Its oracle 10g database
Edited by: thinkingeye on Aug 27, 2010 10:43 AM
Hi,
You can unpivot in any version of Oracle by cross-joining with a table (in practice, usually a result set) that has as many rows as there are columns to unpivot.
For example:
WITH cntr AS
SELECT LEVEL AS n
FROM dual
CONNECT BY LEVEL <= 3
SELECT CASE c.n
WHEN 1 THEN b.bin_1_data
WHEN 2 THEN b.bin_2_data
WHEN 3 THEN b.bin_3_data
END AS bin_data
, ... -- whatever other columns you need
FROM bin_data b
CROSS JOIN cntr c
;Remember to convert the unpivoted columns to the same data type, if they aren't already the same type.
Similar Messages
-
Unpivot task is generating rows for null inputs
So I have a C# application (VS 2012 with .NET4.5) that builds SSIS (SQL2012) packages programatically. the packages can be opened in the designer and they run fine. However, there is one case that is giving me a problem. I have an
OleDb source connected to a table in SQL server. I am using the unpivot task to convert columns in a sparse matrix to an Entity Attribute Value model. So basically, the primary key value of the source table is a pass-through value in the unpivot
task, each column is mapped to the destination column, and the attribute id is hard coded as the pivot key. Like i said this works great EXCEPT i came across one column and a table that was null for all the rows in the table. when I run the package,
it fails with:
OnError,SERVERNAME,DOMAIN\user,{94E83A3B-5386-4712-BEDC-11E35341675F},{94E83A3B-5386-4712-BEDC-11E35341675F},{3187347C-8D44-4D51-8FDB-B5C4159A58B0},9/14/2012 9:48:02 AM,9/14/2012 9:48:02 AM,-1071607780,0x,There was an error with OLE DB Destination.Inputs[OLE
DB Destination Input].Columns[AttributeId] on OLE DB Destination.Inputs[OLE DB Destination Input]. The column status returned was: "The value violated the integrity constraints for the column."
So I set up a data viewer on the data flow and found that the unpivot component was generating rows for every null value. not only that, but the values for the key column and the attribute id (which was hard coded) were also null for all the rows
sent from the unpivot to the ole db destination. I manually created a package with an unpivot for just the column in question and got the same result. then I inserted a value for every row in the table and the same package runs fine.
can someone offer any help or advice on what might be causing this?its just two columns of data that are concerned. ten character numeric strings in the one and null in the other. the pivot key is hard coded in the unpivot component configuration screen. see the output of the data viewer below. How do
I get Microsoft involved with this?
2013399057 NULL
2013399488 NULL
2013399770 NULL
2013402244 NULL
2013402440 NULL
2013404066 NULL
2013404070 NULL
2013404203 NULL
2013404206 NULL
2013404401 NULL
2013404589 NULL
2013404705 NULL
2013404738 NULL
2013404768 NULL
2013404784 NULL
2013404813 NULL -
Unpivot multiple products fields data dynamically
Hi,
I am having data as below;
ID
CMC
EMS
KBP
Week1
501378
320967
822.54
Week2
13500
6000
3000
Week3
34534
63563
9868
Week4
32523
32532
54223
Week5
235235
53453
34534
Week6
34534
534534
34534
I want to show the above data like below;
Product
Week1
Week2
Week3
Week4
Week5
Week6
CMC
501378
13500
34534
32523
235235
34534
EMS
320967
6000
63563
32532
53453
534534
KBP
822.54
3000
9868
54223
34534
34534
I tried the following query:
declare @ListWeekData varchar(max)
declare @query nvarchar(max)
SET @ListWeekData = STUFF((SELECT distinct ',' + QUOTENAME(convert(varchar,WeekOfMonth))
FROM #WeeklyBreakupData
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
print @ListWeekData
SELECT @query = 'SELECT Product
FROM
#WeeklyBreakupData
UNPIVOT
Producta FOR Product IN (' + STUFF(@ListWeekData, 1, 1, '') + ')
) AS up;';
PRINT @query;
EXEC (@query);
drop table #WeeklyBreakupData
but unable to get the desired result. Please help me out ASAP.
Thanks,
SriniHere you go
CREATE TABLE tmp (Name CHAR(1),Forecast INT,Stock INT)
INSERT tmp SELECT 'a',100,300
INSERT tmp SELECT 'b',300,400
INSERT tmp SELECT 'c',200,250
INSERT tmp SELECT 'd',200,300
-- dynamic pivot (SQL Server 2005/2008)
DECLARE @pivot_cols NVARCHAR(1000);
SELECT @pivot_cols =
STUFF((SELECT DISTINCT '],[' + Name
FROM tmp
ORDER BY '],[' + Name
FOR XML PATH('')
), 1, 2, '') + ']';
DECLARE @pivot_query NVARCHAR(2000);
SET @pivot_query =
N'SELECT * FROM (
SELECT * FROM (
SELECT Name,SUM(Forecast) Forecast,SUM(Stock) Stock FROM tmp
GROUP BY Name ) tmp UNPIVOT
Col for [GROUP] IN (Forecast,Stock)
) as UnPvt ) pvt
PIVOT (MAX(Col) FOR Name IN (' + @pivot_cols + ')) j'
EXEC(@pivot_query);
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 -
Mother of all unpivots - columns to rows?
I have multiple tables as follows:
eventdate event1 event2 event3 event4 ... eventn
1/1/07 8 5 3 2 ... 7
2/1/07 1 14 6 9 ... 22
n/n
The tables have different column headers (apart from date), different numbers of columns and different numbers of rows.
I'd like to copy the data from these tables to one table as follows:
tablename eventname eventdate count
table1 event1 1/1/07 8
table1 event2 1/1/07 5
etc
Can anyone advise a solution? Something that handled one source table and could be set up to cycle through multiple tables via script would be great.
I have hit google and forums hard and seen a lot of solutions that either hard code column names or parse the contents of a delimited data column. This is not what I'm after.
Thanks in advance
user592258Prior to 11g, unpivot goes like this:
SELECT id, column_value
FROM your_table t1
, TABLE(your_collection_type(col1,col2,col3,col4)) t2;which transforms
id, col1, col2, col3, col4into
id column_value
id column_value
id column_value
id column_valueIt gets a little more complicated if you have to generate a second column to show which source column the value was taken from. You would need an object type with two attributes and a collection of that type, and then use something like
SELECT id, t2.label, t2.val
FROM your_table t1
, TABLE(your_collection_type
( your_object_type(1,col1)
, your_object_type(2,col2)
, your_object_type(3,col3)
, your_object_type(4,col4))
) t2; -
How to unpivot in SQL Server 2008 R2
Hi,
I have data like below:
AvgPetrolPrice
AvgKerosenePrice
AvgGasOilPrice
GrowthRatePetrol
GrowthRateKerosene
GrowthRateGasOil
685
689
688
0.21
0.21
0.21
I need output like below:
Product
Avg for the Year
Growth Rate for the Year
Petrol
685
0.21
Kerosene
689
0.21
Gas Oil
688
0.21
Please help ASAP.
Regards,
SriniSee example
DECLARE @Table TABLE
UserId INT,
Day1 INT NULL,
Day2 INT NULL,
Day3 INT NULL,
Day4 INt NULL,
DayMax Int NULL
INSERT INTO @Table(UserId, Day1, Day2, Day3, Day4, DayMax)
VALUES(1,10,null,20,3,null);
INSERT INTO @Table(UserId, Day1, Day2, Day3, Day4, DayMax)
VALUES(2,50,25,15,5,null);
SELECT UserId, DayQuantity AS DayMax FROM @Table
UNPIVOT (DayQuantity FOR DayNumber IN (Day1,Day2,Day3,Day4)) AS c
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 -
Hello,
I have a single row of data showing school term start and end dates in 2011/12. I need to convert this single row of data (containing 16 columns) into 5 rows comprised of just 4 columns (year, term, term_start, term_end).
Is it possible to use just Oracle 11g's unpivot function_ to convert the following 16 columns of data:
select * from
(select 2012 as terms_year,
1 as T1,'05-SEP-2011' as T1_SD, '21-OCT-2011' as T1_ED,
2 as T2,'31-OCT-2011' as T2_SD, '16-DEC-2011' as T2_ED,
3 as T3,'03-JAN-2012' as T3_SD, '10-FEB-2012' as T3_ED,
4 as T4,'20-FEB-2012' as T4_SD, '30-APR-2012' as T4_ED,
5 as T5,'16-APR-2012' as T5_SD, '01-JUN-2012' as T5_ED
from dual) mytable
aka
TERMS_YEAR T1 T1_SD T1_ED T2 T2_SD T2_ED T3 T3_SD T3_ED T4 T4_SD T4_ED T5 T5_SD T5_ED
2012 1 05-SEP-2011 21-OCT-2011 2 31-OCT-2011 16-DEC-2011 3 03-JAN-2012 10-FEB-2012 4 20-FEB-2012 30-APR-2012 5 16-APR-2012 01-JUN-2012 into the following 4 columns of data (year, term, term_start, term_end):
select terms_year, term, t1_sd as term_start, t1_ed as term_end from
(select 2012 as terms_year, 1 as term, '05-SEP-2011' as T1_SD, '21-OCT-2011' as T1_ED from dual union all
select 2012 as terms_year, 2 as term, '31-OCT-2011' as T2_SD, '16-DEC-2011' as T2_ED from dual union all
select 2012 as terms_year, 3 as term, '03-JAN-2012' as T3_SD, '10-FEB-2012' as T3_ED from dual union all
select 2012 as terms_year, 4 as term, '20-FEB-2012' as T4_SD, '30-APR-2012' as T4_ED from dual union all
select 2012 as terms_year, 5 as term, '16-APR-2012' as T5_SD, '01-JUN-2012' as T5_ED from dual) mytable
aka
TERMS_YEAR TERM TERM_START TERM_END
2012 1 05-SEP-2011 21-OCT-2011
2012 2 31-OCT-2011 16-DEC-2011
2012 3 03-JAN-2012 10-FEB-2012
2012 4 20-FEB-2012 30-APR-2012
2012 5 16-APR-2012 01-JUN-2012 Much obliged if anyone can teach me how - I can't get my head around the pivot/unpivot syntax! E.g.
Select *
From mytable
UNPIVOT (
unpivot_clause
unpivot_for_clause
unpivot_in_clause)
Thanks,
TP.Hi,
Using the SELECT ... PIVOT feature:
SELECT terms_year
, term
, term_start
, term_end
FROM mytable
UNPIVOT ( ( term
, term_start
, term_end
) FOR num_col
IN ( (t1, t1_sd, t1_ed) AS 1
, (t2, t2_sd, t2_ed) AS 2
, (t3, t3_sd, t3_ed) AS 3
, (t4, t4_sd, t4_ed) AS 4
, (t5, t5_sd, t5_ed) AS 5
ORDER BY terms_year -- If needed
, num_col
;The basic syntax for UNPIVOTing to 1 column (plus a label column) is
UNPIVOT ( outcol
FOR label IN ( incol_1 AS label_val_1
, incol_2 AS label_val_2
, incol_n AS label_val_n
)where
outcol is the pivoted output column, containing values from the original table,
label is a label column, which will contain values hard-coded in the query,
incol_1, incol_2, ..., incol_n are the columns from the origianl table to be unpivoted
label_val_1, label_val_2, ..., label_val_n are the hard-coded values that will go in the label column.
The syntax for pivoting to m columns (m > 1) is like that for pivoting 1 column, but
instead of outcol, you give a comma-delimited list of m outcols, enclosed in parentheses, and
instead of incol_1, incol_2, ... incol_n, you give a comma-delimited list of m columns, enclosed in parentheses.
By the way, storing dates in VARCHAR2 columns is not a very good idea. Use DATE columns instead.
Edited by: Frank Kulash on Jul 16, 2012 6:11 PM -
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 -
UNPIVOT with two set of records
Hi Gurus,
I am using Oracle 11g Release 11.2.0.1.0
OS: windows
Table Structure
EMPL0YEE_ROLE
employee_id number
ROLE_1 VARCHAR2(10)
ROLE_2 VARCHAR2(10)
ROLE_3 VARCHAR2(10)
ROLE_4 VARCHAR2(10)
ROLE_5 VARCHAR2(10)
MANAGER_1 VARCHAR2(10)
MANAGER_2 VARCHAR2(10)
MANAGER_3 VARCHAR2(10)
MANAGER_4 VARCHAR2(10)
MANAGER_5 VARCHAR2(10)
DATA
EMPLOYEE_ID MANAGER_1 MANAGER_2 MANAGER_3 MANAGER_4 MANAGER_5 ROLE_1 ROLE_2 ROLE_3 ROLE_4 ROLE_5
1345 John Mike Ram Kumar DBA Sql Dev PLSQL Admin
1 rows selected
I want a output like this
EMPLOYEE_ID MANAGER ROLE1
1345 John DBA
1345 Mike SQL DEV
1345 Ram PLSQL
1345 Kumar Admin
I have tried with UNPIVOT
I am able to get manager and role in two different queries
SELECT
EMPLOYEE_ID
,MANAGER
FROM
EMPLOYEE_ROLE
UNPIVOT( MANAGER
FOR col1
in (MANAGER_1,
MANAGER_2,
MANAGER_3,
MANAGER_4,
MANAGER_5
EMPLOYEE_ID MANAGER
1345 John
1345 Mike
1345 Ram
1345 Kumar
4 rows selected
SELECT
EMPLOYEE_ID
,ROLE1
FROM
EMPLOYEE_ROLE
UNPIVOT( ROLE1
FOR col1
in (ROLE_1,
ROLE_2,
ROLE_3,
ROLE_4,
ROLE_5
EMPLOYEE_ID ROLE1
1345 DBA
1345 Sql Dev
1345 PLSQL
1345 Admin
4 rows selected
WHEN I tried adding 2 UNPIVOT clauses i got
SELECT
EMPLOYEE_ID
,ROLE1
,MANAGER
FROM
EMPLOYEE_ROLE
UNPIVOT( ROLE1
FOR col1
in (ROLE_1,
ROLE_2,
ROLE_3,
ROLE_4,
ROLE_5
UNPIVOT( MANAGER
FOR col2
in (MANAGER_1,
MANAGER_2,
MANAGER_3,
MANAGER_4,
MANAGER_5
it is giving me cartesian product as given below
EMPLOYEE_ID ROLE1 MANAGER
1345 DBA John
1345 DBA Mike
1345 DBA Ram
1345 DBA Kumar
1345 Sql Dev John
1345 Sql Dev Mike
1345 Sql Dev Ram
1345 Sql Dev Kumar
1345 PLSQL John
1345 PLSQL Mike
1345 PLSQL Ram
1345 PLSQL Kumar
1345 Admin John
1345 Admin Mike
1345 Admin Ram
1345 Admin Kumar
16 rows selectedIs it possible to add two unpivots in a single UNPIVOT query.
Any help is appreciated.
Thanks!
Regards,
Gatha
Edited by: Gatha on Sep 27, 2011 1:50 PM
Edited by: Gatha on Sep 27, 2011 1:51 PM
Edited by: Gatha on Sep 27, 2011 1:53 PM
Edited by: Gatha on Sep 27, 2011 2:40 PMYou can use an object so that the couple (MANAGER, ROLE) is considered as a single column to unpivot :
SQL> create or replace type role_manager_obj as object (manager varchar2(10), role varchar2(10))
2 /
Type created
SQL> WITH emp AS (
2 SELECT employee_id
3 , case when manager_1 is not null then role_manager_obj(manager_1, role_1) end as rm1
4 , case when manager_2 is not null then role_manager_obj(manager_2, role_2) end as rm2
5 , case when manager_3 is not null then role_manager_obj(manager_3, role_3) end as rm3
6 , case when manager_4 is not null then role_manager_obj(manager_4, role_4) end as rm4
7 , case when manager_5 is not null then role_manager_obj(manager_5, role_5) end as rm5
8 FROM employee_role
9 )
10 SELECT employee_id
11 , treat(rm_obj as role_manager_obj).manager as manager
12 , treat(rm_obj as role_manager_obj).role as role
13 FROM emp
14 UNPIVOT(rm_obj FOR rm IN (rm1, rm2, rm3, rm4, rm5))
15 ;
EMPLOYEE_ID MANAGER ROLE
1345 John DBA
1345 Mike SQL DEV
1345 Ram PLSQL
1345 Kumar Admin
Or, doing it the old way, with a cross join :
SQL> WITH five_rows AS ( SELECT level i FROM dual CONNECT BY level <= 5 )
2 SELECT employee_id
3 , manager
4 , role
5 FROM (
6 SELECT e.employee_id
7 , case t.i when 1 then manager_1
8 when 2 then manager_2
9 when 3 then manager_3
10 when 4 then manager_4
11 when 5 then manager_5
12 end as manager
13 , case t.i when 1 then role_1
14 when 2 then role_2
15 when 3 then role_3
16 when 4 then role_4
17 when 5 then role_5
18 end as role
19 FROM employee_role e
20 CROSS JOIN five_rows t
21 )
22 WHERE manager IS NOT NULL
23 ;
EMPLOYEE_ID MANAGER ROLE
1345 John DBA
1345 Mike SQL DEV
1345 Ram PLSQL
1345 Kumar Admin
Edited by: odie_63 on 27 sept. 2011 13:04 -
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 -
Help with unpivot in 10.2
Even after looking at a lot of the examples (and seeing several different approaches) I'm having trouble getting my head around how to unpivot data in 10.2.
I have this horribly designed table, like this:
create table emp_awds (
empid varchar2(5),
award01 varchar2(5),
award01_date date,
award02 varchar2(5),
award02_date date,
award20 varchar2(5),
award20_date date)So, each row has 20 'buckets' for individual awards.
A particular award name can appear in any of the buckets.
There can be gaps in the buckets - an employee may have awards in buckts 1 through 10, nothing in 11-15, and more awards in 16-20, etc.
What I need to get is a count of each named award since a given date:
BUSDRIVER 6
BOTTLEWASHER 15
COOK 4Maybe something like this?
SELECT awards
, COUNT(*)
FROM ( SELECT DECODE
( rn
, 1, award01
, 2, award02
, 3, award03
) AS awards
, DECODE
( rn
, 1, award01_date
, 2, award02_date
, 3, award03_date
) AS awards_date
FROM emp_awds
CROSS JOIN ( SELECT rownum AS rn
FROM dual
CONNECT BY LEVEL <= 3 -- Number of award columns
WHERE awards IS NOT NULL
AND awards_date >= :somedate
GROUP BY awards
;So what you need to do is generate an additional row for each award column for each employee. The CROSS JOIN accomplishes that. Once that's complete the award information will be spread among all the columns still. We can then use DECODE to condense that information into a single column for both the award name and date. Once that's complete you can aggregate and filter as needed.
Edited by: Centinul on Mar 8, 2012 11:42 AM -
2 tables:
CREATE TABLE Requirement
reqID int IDENTITY(1,1) PRIMARY KEY,
req_result_id numeric(10,0) NOT NULL,
description varchar(200),
heading varchar(100),
GO
INSERT INTO Requirement VALUES (5296,'Cold pack','HH19_5');
INSERT INTO Requirement VALUES (5296,'Band-Aids','HH19_6');
INSERT INTO Requirement VALUES (5296,'First aid cream','HH19_7');
INSERT INTO Requirement VALUES (5296,'Tape','HH19_8');
INSERT INTO Requirement VALUES (5296,'Gloves','HH19_9);
INSERT INTO Requirement VALUES (5296,'Bandages','HH19_10');
INSERT INTO Requirement VALUES (5296,'Hand sanitizer','HH19_11');
INSERT INTO Requirement VALUES (5296,'Scissors','HH19_12');
INSERT INTO Requirement VALUES (5296,'Poison control information','HH19_13');
INSERT INTO Requirement VALUES (5296,'Name','HH02');
GO
CREATE TABLE Response
respID int IDENTITY(1,1) PRIMARY KEY,
req_result_id numeric(10,0) NOT NULL,
HH02 varchar(200),
HH19_5 varchar(20),
HH19_6 varchar(20),
HH19_7 varchar(20),
HH19_8 varchar(20),
HH19_9 varchar(20),
HH19_10 varchar(20),
HH19_11 varchar(20),
HH19_12 varchar(20),
HH19_13 varchar(20),
GO
INSERT INTO Response VALUES (33,'Mary',7,8,9,10,11,12,13,14,15);
INSERT INTO Response VALUES (35,'Barbara',7,8,9,10,11,12,13,14,15);
INSERT INTO Response VALUES (144,'Melissa',7,8,9,10,11,12,13,14,15);
INSERT INTO Response VALUES (146,'Missy',7,8,9,10,11,12,13,14,15);
INSERT INTO Response VALUES (5296,'Pamela',7,8,9,10,11,12,13,14,15);
GO
Result:
description
heading
response
Name
HH02
Pamela
Bandages
HH19_10
12
Hand sanitizer
HH19_11
13
Scissors
HH19_12
14
Poison control information
HH19_13
15
Cold pack
HH19_5
7
Band-Aids
HH19_6
8
First aid cream
HH19_7
9
Tape
HH19_8
10
Gloves
HH19_9
11
For now, I'm using where req_result_id = 5296. Only one row from the Response table is selected.
I'm playing with unpivot and cross apply. I might use two CTE's but am not yet sure how to join them. I would just like to see what others here suggest.
I only need help with T-SQL - not table design.
Let me know if you need more info or if I need to reformat the expected output to make it legible.
Thanks for any suggestions.CREATE TABLE Requirement
reqID int IDENTITY(1,1) PRIMARY KEY,
req_result_id numeric(10,0) NOT NULL,
description varchar(200),
heading varchar(100),
GO
INSERT INTO Requirement VALUES (5296,'Cold pack','HH19_5');
INSERT INTO Requirement VALUES (5296,'Band-Aids','HH19_6');
INSERT INTO Requirement VALUES (5296,'First aid cream','HH19_7');
INSERT INTO Requirement VALUES (5296,'Tape','HH19_8');
INSERT INTO Requirement VALUES (5296,'Gloves','HH19_9');
INSERT INTO Requirement VALUES (5296,'Bandages','HH19_10');
INSERT INTO Requirement VALUES (5296,'Hand sanitizer','HH19_11');
INSERT INTO Requirement VALUES (5296,'Scissors','HH19_12');
INSERT INTO Requirement VALUES (5296,'Poison control information','HH19_13');
INSERT INTO Requirement VALUES (5296,'Name','HH02');
GO
CREATE TABLE Response
respID int IDENTITY(1,1) PRIMARY KEY,
req_result_id numeric(10,0) NOT NULL,
HH02 varchar(200),
HH19_5 varchar(20),
HH19_6 varchar(20),
HH19_7 varchar(20),
HH19_8 varchar(20),
HH19_9 varchar(20),
HH19_10 varchar(20),
HH19_11 varchar(20),
HH19_12 varchar(20),
HH19_13 varchar(20),
GO
INSERT INTO Response VALUES (33,'Mary',7,8,9,10,11,12,13,14,15);
INSERT INTO Response VALUES (35,'Barbara',7,8,9,10,11,12,13,14,15);
INSERT INTO Response VALUES (144,'Melissa',7,8,9,10,11,12,13,14,15);
INSERT INTO Response VALUES (146,'Missy',7,8,9,10,11,12,13,14,15);
INSERT INTO Response VALUES (5296,'Pamela',7,8,9,10,11,12,13,14,15);
GO
;with mycte as (
select respID,req_result_id,response,heading from Response
cross apply (values
([HH19_5],'HH19_5')
,([HH19_6],'HH19_6')
,([HH19_7],'HH19_7')
,([HH19_8],'HH19_8')
,([HH19_9],'HH19_9')
,([HH19_10],'HH19_10')
,([HH19_11],'HH19_11')
,([HH19_12],'HH19_12')
,([HH19_13],'HH19_13')
,([HH02],'HH02') ) d(response,heading)
WHERE req_result_id=5296 )
select description,r.heading,response from Requirement r
inner join mycte m on r.req_result_id=m.req_result_id and r.heading=m.heading
Order by Case When len(r.heading)=4 then 1 Else 2 END, Len(Replace(r.heading,'HH19_','')) desc
drop table Requirement,Response -
Hi ALL
I have the following sample data:
AMENDMENT_DATES_ID AMENDMENT_ID ENTRY_DATE AMENDMENT_DATE_CODE
29710 1 12/20/2007 15
31852 2 1/17/2006 29
31860 3 2/9/2006 29
31868 4 9/13/2007 29
31876 6 3/28/2006 29
29994 88 11/21/2007 6
31890 9 8/23/2006 29
I tried to create seperate fields based on the "AMENDMENT_DATE_CODE" using CASE AND DECODE but was not successful. I need the values of the field "AMENDMENT_DATE_CODE" as seperate fields, like :
AMENDMENT_DATES_ID AMENDMENT_ID ENTRY_DATE 15 29 6
Please need help.
ThanksHi,
Unpivot means display multiple columns from one row as if they were one column on multiple rows.
It looks like you want to do the opposite: display the same column from 3 rows as 3 columns on 1 row. That's called Pivot .
This thread shows what you need:
Help for a query to add columns
That example used the aggregate COUNT; you'll probably want MIN or MAX instead. If the combination (AMENDMENT_DATES_ID, AMENDMENT_ID, ENTRY_DATE, AMENDMENT_DATE_CODE) is unique, it won't matter whethere you use MIN or MAX; when there's no more than 1 item, the largest one is the same as the smallest.
As you can see from that thread, there is a different (better) way of doing pivots starting in Oracle 11. What version are you using?
If you'd like help, post a little sample data (CREATE TABLE and INSERT statements), the results you want from that data, and your best attempt at a query. -
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. -
Hello everyone , can somebody please explain the difference in the VALUE column when DESCRIPTION = 'dept' when the query is run with and without the order by clause.
SELECT * FROM
SELECT * FROM emp
--ORDER BY job
)UNPIVOT (VALUE FOR DESCRIPTION IN (sal AS 'salary', deptno AS 'dept'));Bug? 9900850.8
A workaround could be
with emp_1 as
select ename,sal,deptno,deptno dep_dummy,empno --"or whatever column wanted..
from emp
SELECT ename,DESCRIPTION,VALUE
from
select *
from emp_1
UNPIVOT
VALUE FOR DESCRIPTION IN
(sal AS 'salary', deptno AS 'dept')
order by dep_dummy,empno,description; --"and other columns..
ENAME DESCRI VALUE
CLARK dept 10
CLARK salary 2450
KING dept 10
KING salary 5000
MILLER dept 10
MILLER salary 1300
SMITH dept 20
SMITH salary 800
JONES dept 20
JONES salary 2975
SCOTT dept 20
SCOTT salary 3000
ADAMS dept 20
ADAMS salary 1100
FORD dept 20
FORD salary 3000
ALLEN dept 30
ALLEN salary 1600
WARD dept 30
WARD salary 1250
MARTIN dept 30
MARTIN salary 1250
BLAKE dept 30
BLAKE salary 2850
TURNER dept 30
TURNER salary 1500
JAMES dept 30
JAMES salary 950
28 rows selected.
Edited by: jeneesh on May 16, 2013 11:00 AM -
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
Maybe you are looking for
-
Hi Experts, (I will post this question in ABAP forums also) I am a new SAP FI consultant and am not sure of how to overcome the situation I encountered, while trying to print the IDOcs generated during the payment run. Without specifying any print v
-
Technical problem in ms office 2013
When ever I open the files, the corresponding one launches but the file does not open. Some times, even if the file opens (which is very rare), the file stops responding in a minute or when ever I click enable editing. Error is Word is not respondin
-
I have an existing table and corresponding form that needed to have a couple of columns added. I started by altering the table to add the column: alter table my_tab add new_col_a number;and then I added a text box to the form and titled it new_col_a.
-
I'm using the Adobe Illustrator 2014 version and having a problem about fonts.
Hello. I have a text file of Hindi, Arabic, Panjibi, Farsi and Uldo, and need to put a languages to the Illistration, but even if I have a font on ,my computer, when I copied a word from the MS word and Paste to the Illustrator, it pasted to weird la
-
LabVIEW 8.2 Image Display bug
Hi all, Description : Trying to create a constant by right clicking on an image display (controls palette > vision > image display) causes labview to crash. LV version : 8.20 only, didn't exist in 8.0.1 and earlier ! Same bugg happens: with image dis