Transpose or Pivot
I am a new user to Oracle Reports and I am trying to convert many reports from our current environment in SAS.
We have many reports in our current SAS environment in which we basically transpose (pivot) our data before we produce the report. There is an easy way to accomplish this in SAS - "Proc Transpose". I have looked into completing this in the SQL first but can't get it exactly how I need it and then in some cases it would need to run dynamic which I know is possible in PL/SQL but not simple.
I was thinking the reports builder would do it for me.
Here is an example of the data before transposing:
Channel ReportingMonth napp dapp
a September-03 1000 1000000
a October-03 2000 2000000
a November-03 1500 1500000
b September-03 1000 1000000
b October-03 2000 2000000
b November-03 1500 1500000
Here is an example of the data after transposing and really what the report needs to look like:
Channel Measurement September-03 October-03 November-03
a NAPP 1000 2000 1500
a DAPP 1000000 2000000 1500000
b NAPP 1000 2000 1500
b DAPP 1000000 2000000 1500000
My thought is somehow in a Matrix report, but I don't want the Matrix Cell Fields to show under the Matrix Column Fields.
Any help would be grealy appreciated.
stacy,
you will need to generate the matrix report using the wizard and then manually manipulate the cells and the headings so they follow your required positioning.
regards,
philipp
Similar Messages
-
Sql query transpose or pivot info
we have table with hundred thousand plus users.for each user we have two rows,meaning two test areas,in this case 67 and 75 and each testarea has a flag. not sure this is relevant here but some testers have test areas with same flag and some times different
SQL> desc testers
Name Type
tester_ID NUMBER
test_area_id NUMBER
flag VARCHAR2
SQL > select * from testers;
tester_ID test_area_ID flag
439788 67 Y
439788 75 N
434529 67 P
434529 75 P
434576 67 P
434529 75 X i am looking to split the testarea flag so i can have one row for each tester rather two.
tester_ID test_area_ID_67_flag test_area_ID_75_flag
439788 Y N
434529 P P
434576 P X i tried few with partation row over but not quite getting there.any help/ideas are greatly appreciated
thanksHi,
Starting in Oracle 11, you can also use the SELECT ... PIVOT feature.
See the following thread for an example:
Re: rows to column in 11g as single query -
Hi Gurus,
I would like to get the pivot result for following sql
SELECT TO_CHAR (A.AUDITTIMESTAMP, 'HH24') HOUR,
TO_CHAR (A.AUDITTIMESTAMP, 'DD-MM-YYYY') "DATE",
COUNT (*)
FROM PROCESS A
WHERE TO_CHAR (A.AUDITTIMESTAMP, 'MM%') IN (select TO_CHAR (sysdate, 'MM%') from dual)
GROUP BY TO_CHAR (A.AUDITTIMESTAMP, 'HH24'),
TO_CHAR (A.AUDITTIMESTAMP, 'DD-MM-YYYY') order by 1,2;Result
HOUR DATE COUNT
00 06-03-2012 2037
00 07-03-2012 1436
00 08-03-2012 1180
00 09-03-2012 1438
00 10-03-2012 1024
00 11-03-2012 2212
00 12-03-2012 1438
00 13-03-2012 1261Expected output in the following format
DATE 00 01 02 03 04.....
01-03-2012 1241 3858 7167 2443 7836
02-03-2012 859 3400 8379 2273 9445
03-03-2012 434 1787 6588 1617 1323
04-03-2012 236 1072 8955 13247 1630
05-03-2012 1354 3295 6685 2264 2442
06-03-2012 2037 3550 9280 2958 2816
07-03-2012 1436 4003 7397 2624 2239
and so on..Pivot function can be sum of count
Thanks
RajCreating a view of your query named hourly_audits, using DATUM as Alias for the DATE column and Q as Alias for the COUNT column:
SELECT a.datum,
a.Q "00",
b.Q "01",
c.Q "02"
FROM hourly_audits a, hourly_audits b, hourly_audits c
WHERE a.hour = '00' AND
b.hour = '01' AND
c.hour = '02' AND
a.datum = b.datum AND
a.datum = c.datum;etc. -
Pivot type query without aggregate function. Transposing
Hi experts,
Oracle 11g.
I have a table (see code example to reproduce), that has a date, a grouping, and the count of that grouping (determined in another query). I need a pivot type query, but, without the aggregate functions. This is just for a report display. I can not seem to figure this one out. Thanks for your help.
CREATE TABLE temp_task
AS
SELECT TO_DATE ('15-NOV-2012') validation_date,
'GROUP 1' AS group_number,
42 AS monthly_count
FROM DUAL
UNION ALL
SELECT TO_DATE ('14-DEC-2012') validation_date,
'GROUP 1' AS group_number,
33 AS monthly_count
FROM DUAL
UNION ALL
SELECT TO_DATE ('15-NOV-2012') validation_date,
'GROUP 2' AS group_number,
10 AS monthly_count
FROM DUAL
UNION ALL
SELECT TO_DATE ('14-DEC-2012') validation_date,
'GROUP 2' AS group_number,
32 AS monthly_count
FROM DUAL
UNION ALL
SELECT TO_DATE ('15-NOV-2012') validation_date,
'GROUP 3' AS group_number,
7 AS monthly_count
FROM DUAL
UNION ALL
SELECT TO_DATE ('14-DEC-2012') validation_date,
'GROUP 3' AS group_number,
9 AS monthly_count
FROM DUAL;Using only SQL I need to return the following:
VALIDATION_DATE | GROUP 1 | GROUP 2 | GROUP 3
11/15/2012 | 42 | 10 | 7
12/14/2012 | 33 | 32 | 9Hi
You always need to use an aggregate function while pivoting.
Even if you don't really need any aggregation, that is, when what you see in the table is what you'll get in the result set, you still have to use an aggregate function. If there will only be one value contrinuting to each cell, then you can use MIN or MAX. It won't matter which; since there's only 1 value, that value will be the highest of the 1, and it will also be the lowest. For NUMBER columns, you could also use SUM or AVG.
SELECT *
FROM temp_task
PIVOT ( MIN (monthly_count)
FOR group_number IN ( 'GROUP 1'
, 'GROUP 2'
, 'GROUP 3'
ORDER BY validation_date
; Output:
VALIDATION_ 'GROUP 1' 'GROUP 2' 'GROUP 3'
15-Nov-2012 42 10 7
14-Dec-2012 33 32 9It sounds like you're doing real aggregation someplace, to get monthly_count. Maybe it would be simpler and more efficient to do the pivoting at that point. What is the big picture here? Post some sample data as it is before you compute monthly_count, and the results you want from that data (if different from what you've already posted), and then let's see if we can't aggregte it and pivot it at the same time. -
I'm almost embarrassed about asking this but my sql skills just haven't kept up since 8i !!
I know i could write a procedure and put the data into a temp table , but i really should get with the program and learn some new functions !!
using select wo_num, g_start,g_end from batch
I have a row returned from a table like so
WO_NUM G_START G_END
1000000 100 105
Using a select statement I would like to turn it into
WO_NUM G
1000000 100
1000000 101
1000000 102
1000000 103
1000000 104
1000000 105Hi,
You're asking about a counter table, that is, a result set (that you can use like a table) that has all possible values in a range: in this case, all the integers between g_start and g_end.
In Oracle 9 (and up) you can generate such a counter table using CONNECT BY.
The query below does what you want for all rows in table_x:
WITH cntr AS
SELECT LEVEL - 1 AS n
FROM dual
CONNECT BY LEVEL <= 1 + (
SELECT MAX (g_end - g_start)
FROM table_x
SELECT x.wo_num
, g_start + c.n AS g
FROM table_x x
JOIN cntr c ON x.g_end >= x.g_start + c.n
ORDER BY x.wo_num
, g;But you don't want to do this for every row in table_x: you want to do it for every row in the result set of your original query. (It doesn't matter if there's only one row or not.)
So add your original query as another sub-query, and use that instead of table_x:
WITH original_results AS
SELECT wo_num
, g_start
, g_end
FROM table_x
WHERE column_1 < column_2
, cntr AS
SELECT LEVEL - 1 AS n
FROM dual
CONNECT BY LEVEL <= 1 + (
SELECT MAX (g_end - g_start)
FROM original_results
SELECT x.wo_num
, g_start + c.n AS g
FROM original_results x
JOIN cntr c ON x.g_end >= x.g_start + c.n
ORDER BY x.wo_num
, g;The original query in this example is pretty simple, but you can use any query, involving joins and sub-queries. -
How to transpose rows into multiple columns using pivot table
I have 1 row containing 12 columns with value "JAN", "FEB", "MAR", "J-1","F-1","M-1","J-2","F-2","M-2","J-3","F-3","M-3"
I want to display as
JAN J-1 F-1 M-1
FEB J-2 F-2 M-2
MAR J-3 F-3 M-3
How do I achieve the above?Today you have only 3 months JAN, FEB, MAR. Is it always the same number of columns. What if there are more months added to this row?
Is your data really coming from relational source or some sort of text file?
There is a better way to do this in narrative view using HTML if your requirement is just to show them in multiple rows and do some calculations.
Go to Narrative View;
In prefix, use <html> <table>
In 'Narrative' text box add something like this
<tr> <td> @1 </td> <td> @4 </td> <td> @7 </td> </tr>
<tr> <td> @2 </td> <td> @5 </td> <td> @8 </td> </tr>
<tr> <td> @3 </td> <td> @6 </td> <td> @9 </td> </tr>
In Suffix, use </table> </html>
You can also add simple calculations like sum etc at the very bottom of these rows as grand totals.
kris -
I am having situation in my project in which I need to transpose the data, means need to display all rows to column.
Please asap help me on this.
Thanks in advance
Anup888679 wrote:
Thanks for your response, but Pivot present in 11+ & I am using 10gWell, the 2nd link in the FAQ indicated by Karthick_Arp is :
[url http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:766825833740]Tom Kyte - Pivoting pre-11g
Pre-11g meaning 10g or less.... -
I am having the data like this
1
1
b1
1
2
b2
1
3
b3
1
4
b4
2
1
a1
2
2
a2
2
3
a3
2
4
a4
3
1
c1
3
2
c2
3
3
c3
3
4
c4
I want to display like this
b1
b2
b3
b4
1
1
1
1
1
2
3
4
a1
a2
a3
a4
1
1
1
1
1
2
3
4
c1
c2
c3
c4
1
1
1
1
1
2
3
4
please help me hereI'm not sure that the way you want it be done is a classical transposing, which is literally rotation counterclockwise. The classical transposing can be done via PIVOT query but the result will be
b1 b2 b3 b4 a1 a2 a3 a4 c1 c2 c3 c4
1 1 1 1 2 2 2 2 3 3 3 3
1 2 3 4 1 2 3 4 1 2 3 4
So, there is no elegant way of doing your task. Especially, without VBA, I see the only way is a UNION ALL query.
E.g. you have a table
t1
f1
f2
f3
1
1
b1
1
2
b2
1
3
b3
1
4
b4
2
1
a1
2
2
a2
2
3
a3
2
4
a4
3
1
c1
3
2
c2
3
3
c3
3
4
c4
<tfoot></tfoot>
You have 3 queries:
f_1
f1
1
2
3
4
1
1
1
1
1
2
2
2
2
2
3
3
3
3
3
<tfoot></tfoot>
TRANSFORM Max(t1.f1) AS [Max-f1]
SELECT t1.f1
FROM t1
GROUP BY t1.f1
PIVOT t1.f2;
f_2
f1
1
2
3
4
1
1
2
3
4
2
1
2
3
4
3
1
2
3
4
<tfoot></tfoot>
TRANSFORM Max(t1.f2) AS [Max-f2]
SELECT t1.f1
FROM t1
GROUP BY t1.f1
PIVOT t1.f2;
f_3
f1
1
2
3
4
1
b1
b2
b3
b4
2
a1
a2
a3
a4
3
c1
c2
c3
c4
<tfoot></tfoot>
TRANSFORM Max(t1.f3) AS [Max-f3]
SELECT t1.f1
FROM t1
GROUP BY t1.f1
PIVOT t1.f2;
And finally, the UNION ALL query:
piv
f1
1
2
3
4
1
b1
b2
b3
b4
1
1
2
3
4
1
1
1
1
1
2
a1
a2
a3
a4
2
1
2
3
4
2
2
2
2
2
3
c1
c2
c3
c4
3
1
2
3
4
3
3
3
3
3
<tfoot></tfoot>
select [f1], [1], [2], [3], [4] from f_1
UNION ALL
select [f1], [1], [2], [3], [4] from f_2
UNION ALL select [f1], [1], [2], [3], [4] from f_3
ORDER BY [f1], [4] DESC;
It's rather simple for your example with 3 letters and 4 numbers. As soon as you have, at least, 10 variations, it will be pure delirium.
Andrey V Artemyev | Saint-Petersburg, Russia -
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/ -
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. -
HI All,
I want to transpose columns to rows in my reports. I tried through Pivot Table and i was able to do that, but columns name are not appearing in the left.
Plz help me out how to do that with column name/
Can we do the same with measure also???
ThanksAll, thanks for the information.
I have a similar requirement as being discussed here. However, I do not have any measures in my report. It is a plain request which fetches data in three columns A, B and C (and variable number of rows depending on the data available).
Can I still use pivot table to transpose this data such that the resultant display would have three rows always and variable number of columns, with the first column containing the header A, B and C ? If yes, how do I go about it? Else, is there any other approach?
Thanks again
I
Edited by: user635102 on Sep 18, 2008 12:31 PM -
How to do pivoting in ODI? Please anyone guide me the steps of pivoting.
How to convert rows to columns or viceversaThere are no KMs to do pivoting in ODI.
You have to customize it or use TMPs to transform your rows to cols and vice-versa. There is a sample of a KM with Pivot Functionality in the forum.
Check this url to more information: Re: How to transpose columns to rows in ODI
The url to the specified KM Pivot Table is: http://s3.amazonaws.com/Ora/KM_IKM_Pivot.zip
If you aren't familiarized with KM Customization, I recommend you to create TMPs converting the rows to cols end use the TMP as your final source.
Regards.
Luiz Araujo -
Transposing group from rows to column
Hi Team,
I am having groups of rows of a fixed kind that need to be transposed to column. eg
Attrib
Value
Attribute1
a
Attribute2
b
Attribute3
c
Attribute4
d
Attribute1
e
Attribute2
f
Attribute3
g
Attribute4
h
Attribute2
i
Attribute3
j
Attribute4
k
This need to be transposed as ;
Attribute1
Attribute2
Attribute3
Attribute4
a
b
c
d
e
f
g
h
NULL
i
j
k
Any help would be great...
I tried using PIVOT like
pivot (min (AttributeValue) for rn in ([1], [2], [3], [4],[5], [6])) pvt
where i was having rank in rn... but the places where Attribute1 is not present, that also nneed to become NULL. This is something I am not able to do.Hi
If
I may, there is very small fix to
Rishabh's query in order to make it work.
* as it is it will work only if the order of the insert fit the result, and not in any case! the SQL Server might bring the data in different order if there is no use of ORDER BY
Option 1:
;with cte as
SELECT * , ROW_NUMBER () OVER (partition by Attrib ORDER BY (SELECT NULL)) as rnum
FROM T
--select * from cte
select [Attribute1],[Attribute2],[Attribute3],[Attribute4]
FROM
SELECT Attrib,Value , rnum
FROM cte
)p
PIVOT
(MAX(value) FOR Attrib IN ([Attribute1],[Attribute2],[Attribute3],[Attribute4]))pvt
Option 2: Another option for solution might be
;with MyCTE as (
select Attrib, Value, ROW_NUMBER() OVER (partition by Attrib order by (SELECT NULL)) RN
from T
select
MAX(CASE when Attrib = 'Attribute1' then Value END) as Attribute1
,MAX(CASE when Attrib = 'Attribute2' then Value END) as Attribute2
,MAX(CASE when Attrib = 'Attribute3' then Value END) as Attribute3
,MAX(CASE when Attrib = 'Attribute4' then Value END) as Attribute4
--, RN
from MyCTE
group by RN
And here is all together (check your solution which is the first, it will not bring the correct result since I changed the order of inserted)
create table T (Attrib NVARCHAR(100), Value NVARCHAR(10))
insert T values
('Attribute1', 'a'),
('Attribute2', 'b'),
('Attribute3', 'j'),
('Attribute3', 'c'),
('Attribute4', 'd'),
('Attribute1', 'e'),
('Attribute2', 'f'),
('Attribute3', 'g'),
('Attribute4', 'h'),
('Attribute2', 'i'),
('Attribute4', 'k')
GO
;with cte as
SELECT * , (ROW_NUMBER () OVER (ORDER BY (SELECT NULL)) -1 )/ 4 as rnum
FROM T
--select * from cte
select [Attribute1],[Attribute2],[Attribute3],[Attribute4]
FROM
SELECT Attrib,Value , rnum
FROM cte
)p
PIVOT
(MAX(value) FOR Attrib IN ([Attribute1],[Attribute2],[Attribute3],[Attribute4]))pvt
GO -- Not OK
;with MyCTE as (
select Attrib, Value, ROW_NUMBER() OVER (partition by Attrib order by (SELECT NULL)) RN
from T
select
MAX(CASE when Attrib = 'Attribute1' then Value END) as Attribute1
,MAX(CASE when Attrib = 'Attribute2' then Value END) as Attribute2
,MAX(CASE when Attrib = 'Attribute3' then Value END) as Attribute3
,MAX(CASE when Attrib = 'Attribute4' then Value END) as Attribute4
--, RN
from MyCTE
group by RN
GO -- OK
;with cte as
SELECT * , ROW_NUMBER () OVER (partition by Attrib ORDER BY (SELECT NULL)) as rnum
FROM T
--select * from cte
select [Attribute1],[Attribute2],[Attribute3],[Attribute4]
FROM
SELECT Attrib,Value , rnum
FROM cte
)p
PIVOT
(MAX(value) FOR Attrib IN ([Attribute1],[Attribute2],[Attribute3],[Attribute4]))pvt
GO --OK
drop table T
Ronen Ariely
[Personal Site] [Blog] [Facebook] -
Transposing column names to row for a multi row table
Hi
I have a table some thing like this that needs to be transposed.
FIELD_POPULATED First_NM**Last_NM**Mi_NM*
A_NULL 0 0 0
A_NOT_NULL 120 120 120
B_NULL 0 0 0
B_NOT_NULL 0 0 0
The above table has to be transposed as
column_name*A_NULL**A_NOT_NULL**B_NULL* B_NOT_NULL
FIRST_NM 0 120 0 0
Last_NM 0 120 0 0
Mi_NM 0 120 0 0
I am working oracle on 11g. Any help is greatly appreciatedHi,
See this thread:
Re: Help with PIVOT query (or advice on best way to do this) -
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.
Maybe you are looking for
-
USB 2.0 stopped being recognized by USB 3.0 port
Hey all, Two days ago, I was syncing my iPod Classic (USB 2.0) with iTunes and I also had my USB 2.0 Time Machine drive plugged in (I do not think it was backing up at the time), both of these devices were plugged into the USB 3.0 ports on my 15" Mac
-
Mini DisplayPort no longer working after update
Mid-2010 MacBook Pro here. Ever since I updated to Yosemite, my Mini DisplayPort is no longer working. With Mavericks it was working just fine. I have restarted and done everything I know how to do (PRAM, SMC, etc) - does anyone have any ideas? Th
-
Hi guys, Here is the issue, the accounting clerk creates a note on a line item of a customer open item using DMC (deduction management component) with action codes and a partial payment has been paid by the customer on the same invoice and since a ne
-
How can I add a course to the iTunes U?
I have a problem. When I want to subscribe via iPhone 4 a course nothing happens. When I downloading it through my computer and then to my iPhone it doesn't appears in the iTunes U. Can you help me?
-
I know its a little unorthodox - but due to some security and search-spider constraints for a project I'm on, we need to post PDF documentation to our website rather than HTML Help or WebHelp. My question is this - is there a way when generating a PD