Transform columns in rows
I have a requirement in which I need to write a query for transforming the columns to rows.
Input table (DB - 11g)
========
ORDER_ID HOME_NUM WORK_NUM MOBILE_NUM FAX_NUM
100 999456901 <null> 334054483 <null>
101 <null> <null> <null> 787372163
102 587372163 943528985 642344352 677676666
103 53522890 <null> <null> <null>
104 <null> 83366656 956656655 <null>
decode the contact numbers like this, HOME_NUM = 1, WORK_NUM = 2, MOBILE_NUM = 3 and FAX_NUM = 4 and insert as phone_type_cd with the corresponding number. Output should be
ORDER_ID phone_type_cd contact_num
100 1 999456901
100 3 334054483
101 4 787372163
102 1 587372163
102 2 943528985
102 3 642344352
102 4 677676666
103 1 53522890
104 2 83366656
104 3 956656655
Edited by: sarvan on Jul 12, 2011 6:09 AM
Hi,
Another way is to cross-join your table to a table (or a result set, as I ded below) that contains one row fro every possible phone_type:
WITH cntr AS
SELECT LEVEL AS phone_type
FROM dual
CONNECT BY LEVEL <= 4
SELECT i.order_id
, c.phone_type
, CASE c.phone_type
WHEN 1 THEN i.home_num
WHEN 2 THEN i.work_num
WHEN 3 THEN i.mobile_num
WHEN 4 THEN i.fax_num
END AS contact_num
FROM cntr c
CROSS JOIN input_table i
WHERE CASE c.phone_type
WHEN 1 THEN i.home_num
WHEN 2 THEN i.work_num
WHEN 3 THEN i.mobile_num
WHEN 4 THEN i.fax_num
END IS NOT NULL
ORDER BY i.order_id
, c.phone_type
;The query above will work in Oracle 9 (and up), but starting in Oracle 11 there's a better way: SELECT ... UNPIVOT .
If input_table consists of only the columns you posted:
SELECT *
FROM input_table
UNPIVOT ( contact_num
FOR phone_type IN ( home_num AS 1
, work_num AS 2
, mobile_num AS 3
, fax_num AS 4
ORDER BY order_id
, phone_type
;Edited by: Frank Kulash on Jul 12, 2011 9:31 AM
Similar Messages
-
Hi, I would like transform columns to rows.
i.e. source table:
colA colB colC colD colE colX
aa bb cc dd ee 1
ff gg hh ii jj 2
..and output table-solution:
colText colValue colX
colA aa 1
colB bb 1
colC cc 1
colD dd 1
colE ee 1
colA ff 2
colB gg 2
colC hh 2
colD ii 2
colE jj 2
It's possible do it only with select - it would be the best solution.
Thanks zdenek T.Here is another way.
SQL> DESC MYTABLE
Name Null? Type
COLA VARCHAR2(20)
COLB VARCHAR2(20)
COLC VARCHAR2(20)
COLD VARCHAR2(20)
COLE VARCHAR2(20)
COLX VARCHAR2(20)
SQL> SELECT * FROM MYTABLE;
COLA COLB COLC
COLD COLE COLX
aa bb cc
dd ee 1
ff gg hh
ii jj 2
1 SELECT COLUMN_NAME,DECODE(COLUMN_ID,1,COLA,2,COLB,3,COLC,4,COLD,5,COLE),COLX
2 FROM MYTABLE M,USER_TAB_COLUMNS U
3 WHERE TABLE_NAME = 'MYTABLE'
4 AND COLUMN_NAME <> 'COLX'
5* ORDER BY 2,3
SQL> /
COLUMN_NAME DECODE(COLUMN_ID,1,C COLX
COLA aa 1
COLB bb 1
COLC cc 1
COLD dd 1
COLE ee 1
COLA ff 2
COLB gg 2
COLC hh 2
COLD ii 2
COLE jj 2
10 rows selected. -
Transform columns in row for a table in Oracle 10g
Hi,
I am using Oracle 10g.
There is a table (example) EMPLOYEE, with following structure
IDUSER YEAR1 YEAR2 YEAR3
12345 value1 value2 value3
99999 value7 value6 value5
and I need display :
IDUSER YEAR
12345 value1
12345 value2
12345 value3
99999 value7
99999 value6
99999 value5
How can I do this with without unpivot?
ThanksHi,
Welcome to the forum!
One way to unpivot in any version of Orace is to cross-join your table with another table (or resut set, in ths case) that has as many rows as you need toroduce from each row of your original table.
Here's an example:
WITH cntr AS
SELECT LEVEL AS n
FROM dual
CONNECT BY LEVEL <= 3
SELECT e.iduser
, CASE c.n
WHEN 1 THEN year1
WHEN 2 THEN year2
WHEN 3 THEN year3
END AS uear
FROM cntr c
CROSS JOIN employee e
I hope this answers your question.
If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only), and the results you want from that data.
Explain, using specific examples, how you get those results from that data.
Always say what version of Oracle you're using (e.g. 11.2.0.2.0).
See the forum FAQ {message:id=9360002}
Edited by: Frank Kulash on Apr 11, 2013 6:47 AM -
Map CdC op transform SEQUENCE and ROW oper column properties
I am using Map_cdc_op transform, for sequence column and row operation column properties, what fields should i choose?
My source table has the following columns:
RMID - primary key int
RMName - varchar
created_date datetime
Last_updated datetime
target table also has above 4 columns plus one rmLogID column with int data type. which is key column in target.
does map_cdc_op transform also take care of the surrogate key column id in target? which is "rmlogid".
thank you very much for the helpful info.Suneer I am using SQL serverdatabase, this table is dragged to workspace as source:
RMID - primary key int
RMName - varchar
created_date datetime
Last_updated datetime
My task is to capture data changes from this table to target table, for that i a using Map_op_CDC transform, now under the properties SEQUENCE and ROW oper column what fields should i pic.I never used this transform before.
RMid is the primary key in source, rest teh of teh fields are changeable. i track created_dt and last_updated dates for that row.
Thanks a lot for the helpful info. -
Transpose of columns to rows (unpivoting) and not rows to columns
Hi,
I am right now using oracle 10g. I HAVE the following specification. Here I specified only 5 columns.
We have upto 200 columns.
TRANS_ID PORTFILIO_NUM TICKER PRICE NUM_SHARES ........................................
2 100 MO 25.00 100 ........................................
3 100 MCD 31.50 100 ........................................
I want the above to be transformed into the following output .
TRANS_ID TYPE VALUE
2 PORTFILIO_NUM 100
2 TICKER MO
2 PRICE 25.00
2 NUM_SHARES 100.
I don't want to use case/decode function by hard coding the 200 columns.
Can anyone provide me a good way (mostly dynamic way) of doing this?
I searched the whole forum and also other forums. Everywhere I could find
rows to columns / columns to rows where the column names have been hardcoded.
I want a dynamic way of doing it. Let me know if u need any other inputs.
DDL :
CREATE TABLE PORT_TRANS
TRANS_ID VARCHAR2(100 BYTE),
PORTFILIO_NUM VARCHAR2(100 BYTE),
TICKER VARCHAR2(100 BYTE),
PRICE VARCHAR2(100 BYTE),
NUM_SHARES VARCHAR2(100 BYTE)
INSERT INTO PORT_TRANS (TRANS_ID,PORTFILIO_NUM,TICKER,PRICE,NUM_SHARES)
VALUES('2','100','MO','25.00','100');
INSERT INTO PORT_TRANS (TRANS_ID,PORTFILIO_NUM,TICKER,PRICE,NUM_SHARES)
VALUES('3,'100','MCD','31.50','100');
COMMIT;
Thanks,
Priya.Hi,
What you're trying to write is something like this:
WITH cntr AS
SELECT LEVEL AS n
FROM dual
CONNECT BY LEVEL <= 4
SELECT p.trans_id
, CASE
WHEN c.n <= 2
THEN
CASE c.n
WHEN 1 THEN 'PORTFILIO_NUM'
WHEN 2 THEN 'TICKER'
END
ELSE
CASE c.n
WHEN 3 THEN 'PRICE'
WHEN 4 THEN 'NUM_SHARES'
END
END AS type
, CASE
WHEN c.n <= 2
THEN
CASE c.n
WHEN 1 THEN p.PORTFILIO_NUM
WHEN 2 THEN p.TICKER
END
ELSE
CASE c.n
WHEN 3 THEN p.PRICE
WHEN 4 THEN p.NUM_SHARES
END
END AS value
FROM port_trans p
CROSS JOIN cntr c
ORDER BY p.trans_id
, c.n
;I wrote this as if CASE could only handle 2 choices, rather than 128, just to show how to nest CASE expressions.
What you have to do is write the CASE expressions, based on the contents of all_tab_columns.
In your sample data, all of the columns are VARCHAR2 (another design flaw). If you have any columns of other types, use TO_CHAR to convert them to VARCHAR2; that is, the final code to be run will have something like:
... WHEN 4 THEN TO_CHAR (p.NUM_SHARES)If I had to do this, I might run several queries on all_tab_columns, each producing one script, containing just a fragment of the query.
To run the whole thing, I would hard-code a main query like this
WITH cntr AS
SELECT LEVEL AS n
FROM dual
CONNECT BY LEVEL <=
@num_columns.sql
SELECT p.trans_id
, CASE
@type.sql
END AS type
, CASE
@value.sql
END AS value
FROM port_trans p
CROSS JOIN cntr c
ORDER BY p.trans_id
, c.n
;As with any coidng, start small and take baby steps. Maybe the first step would just be to write num_columns.sql, which just contains the number 4. When you can do that, hard-code the CONNECT BY query, calling num_columns.sql.
Good luck! -
Hi there,
Context:
I have to run a job weekly which would load (update/insert) DestTable with data from SourceTable, before i load the data into DestTable i have to transform the data which includes transpose Columns to rows based on a composite key and calculate to derive few columns. I have written this job in .net exe but it is taking huge amount of time to finish the job, like it took 5hours to process 1.9 million records.
SourceTable has a similar structure
ColName
Item_id
sales
Item_desc
profit
state_code
DestTable has a similar structure
ColName
Item_id
Sales_for_state1
sales_for_state2
sales_for_state3
sales_for_state4
profit
profit_margin --this is calculated based on the above columns
the above is just a glimpse of the tables' structure that i have to process.
Resolution:
I would like to rewrite the .net exe code into a PL/SQL. can you help me write a query that would transform the columns to rows and calculate and then insert them in to the DestTable.
ThanksAs far as I understand your structure, it's not a pivot-like thing. So, probably, you can make a transformation of the following kind. And then directly upload to the target_table:
select item_id,
sum(decode(state_code,'CODEOFSTATE1',sales, null)) s4state1,
sum(decode(state_code,'CODEOFSTATE2',sales, null)) s4state2,
sum(decode(state_code,'CODEOFSTATE3',sales, null)) s4state3,
sum(decode(state_code,'CODEOFSTATE4',sales, null)) s4state4,
sum(profit)
from source_table
group by item_id -
Interactive Report - Icon View - Dynamic Columns per Rows ?
Hi all,
We use the icon view functionnality in Interactive Report.
Is there a way to display the 'columns per row' attribute as an application item and set it dynamical via PL/SQL ?
Any suggestions?
Thanks in advance for advices,
Regards,
GrégoryHi,
Apex 4.0 interactive reports and images (Scott's thread)
Have some useful information and pointers to the solution you are looking for.
I hope this help.
Thank you,
Ranish -
SWAP COLUMNS AND ROW IN AN INTERNAL TABLE to display in ALV
Hi ,
I want to swap all the rows in an internal table with the column of the internal table to display it horizontally in ALV grid.
e.g
1 2 3 (field names)
A P X
B Q Y
C R Z
should look like :
D A B C
E P Q R
F X Y Z
Where D , E, F in first column is already apended in new table.
Or else is there a way to rotate the ALV grid so that it can display rows as columns & columns as rows.
regardshi,
i have an internal table which is like
f1 f2 f3 f4 (column header)
A 1 2 3
B 4 5 6
C 7 8 9
the values in o/p table should be
A B C ( column header)
1 4 7
2 5 8
3 6 9
Please help!! -
How to enter a data into the specified column and row in a created table
Hi,
I want to enter some data to specified column and row in a already created table. Please let me know how to do this.
Regards
Shivakumar SinghA table is just a 2D array of strings. Keep it in a shift register and use "replace array element" to modify the desired entry programmatically.
If you want to modify it manually and directly from the front panel, make it into a control and type directly into the desired element. (In this case your program would need to write to it using a local variable).
Atttached is a simple example in LabVIEW 7.0 that shows both possibilities.
LabVIEW Champion . Do more with less code and in less time .
Attachments:
ChangeTableEntries.vi 41 KB -
Problem in displaying the data of columns into rows in sap script
hi,
i am working on a sap script and i have to display the dat which is displayed in column into rows but it is not displaying it properly.
eg, C
12.1
Si
5.5
it is displaying the data right now like this but i want to display the data like this:-
eg, C Si
12.1 5.5
plzzprovide me guidelines how to solve this problem.hi,
i am using this code to display the data:-
plzz provide me guidelines where i am getting wrong?
TOPparCOMPONENT DESP,,,,,, INS. LOT #, , , , , , MIC,,,,,,,,,, MIC VALUEparENDTOPparFINAL
PROTECT
IF &I_FINAL-PRUEFLOS& NE '000000000000'
&I_FINAL-MAKTX(23)&&i_final-prueflos(12Z)&
&I_FINAL-kurztext(25)&
&I_FINAL-original_input(8)&
ELSE
&I_FINAL-MAKTX(23)&
&I_FINAL-kurztext(25)&
&I_FINAL-original_input(8)&
ENDIF
ENDPROTECT
ITEMHEAD
POSITION WINDOW
SIZE WIDTH +0 . 4 CH HEIGHT +1 LN
BOX FRAME 10 TW
BOX HEIGHT '1.35' LN INTENSITY 20
IF &PAGE& = '1'
BOX XPOS '0' CH YPOS '0' CM WIDTH '0' CM HEIGHT '43' LN FRAME '10' TW
For horizontal line at top
BOX XPOS '0' CH YPOS '0' CM WIDTH '75' CH HEIGHT '0' LN FRAME '10' TW
COLUMN LINES...
END OF COLUMN LINES...
BOX XPOS '0' CH YPOS '43' LN WIDTH '75' CH HEIGHT '0' LN FRAME '10'TW
BOX XPOS '75' CH YPOS '0' LN WIDTH '0' CH HEIGHT '43' LN FRAME '10'TW
ELSE
COLUMN LINES...
END OF COLUMN LINES...
BOX XPOS '0' CH YPOS '0' CM WIDTH '0' CM HEIGHT '47' LN FRAME '10' TW
BOX XPOS '0' CH YPOS '0' CM WIDTH '75' CH HEIGHT '0' LN FRAME '10' TW
BOX XPOS '0' CH YPOS '0' CM WIDTH '45' CM HEIGHT '0' LN FRAME '10' TW
BOX XPOS '20' CH YPOS '0' CM WIDTH '0' CM HEIGHT '47' LN FRAME '10' TW
BOX XPOS '0' CH YPOS '47' LN WIDTH '75' CH HEIGHT '0' LN FRAME '10'TW
BOX XPOS '75' CH YPOS '0' LN WIDTH '0' CH HEIGHT '47' LN FRAME '10'TW
ENDIF
LINEFEED
NEWPAGE
NEW-PAGE
provide me guidelines to solve this problem.
Edited by: ricx .s on Mar 13, 2009 5:58 AM -
Filling in values based on the column and row headers
I have a question that seems simple, but I can't figure out how to do it and I've searched all over the forum to no avail.
I have one column that is width increasing by 3 in. increments (column A)
I have one row that is height increasing by 3 in. increments (row B)
I have one cell that is price (cell A1)
I have a formula to calculate price per sq. ft.: =CEILING(A3B2/144,1)A1. This gives me the whole sq. ft. number multiplied by the price per sq. ft. to give me a total price at that dimension (A3 is the width, B2 is the height, and A1 is the cell that contains the price per sq. ft.).
*Here is the problem*: When I try to cut and paste or fill the formula to the other cells, it doesn't calculate the formula as =CEILING(column A * Row B/144,1)*A1. Instead, it moves all of the values relative to the new cell that the formula is being pasted or filled to.
Is there a way to autofill the cells so that the formula continues to refer to the width column, the height row, and the price cell?Aha! Apparently I didn't do enough research before I posted and I have since answered my own question. Here's how you do it. Look for "Distinguishing Absolute and Relative Cell References" and you should be able to find what you need. In my case, here is the formula I needed to have to make the copy and paste function do what I needed it to do: =CEILING($A3*B$2/144,1)*$A$. Hope that helps anyone who may have had the same problem I did.
-
How to get the current selected column and row
Hi,
A difficult one, how do i know which column (and row would also be nice) of a JTable is selected?
e.g.
I have a JButton which is called "Edit" when i select a cell in the JTable and click the button "Edit" a new window must be visible as a form where the user can edit the a part of a row.
Then the column which was selected in the JTable must be given (so i need to know current column) and then i want the TextField (the one needed to be edited) be active with requestFocus(). So it would be
pricetextfield.requestFocus();
Problem now is that i have to click every time in the window the JTextField which was selected in the JTable. I have chosen for this way of editing because my application is multi-user and it would be too difficult for me when everybody did editing directly (catch signals, reload data, etc.).
My question is how do I know the current column and the current row in a JTable?I'm not sure what your mean by the "current" row or column, but the following utility methods return
which row and column have focus within the JTable.
public static int getFocusRow(JTable table) {
return table.getSelectionModel().getLeadSelectionIndex();
public static int getFocusColumn(JTable table) {
return table.getColumnModel().getSelectionModel().getLeadSelectionIndex();
} -
Please help to generate the table from column to rows
Hello -
I have one table with more than 100 columns there are 70 column start with HC1.....HC70 (they are not in sequence) and has some value 0 or 1
Table structure:
HICN_ID HC1 HC2 HC4 HC5 HC6.................................HC70
1234A 0 1 1 0 1 1
3456D 1 0 0 1 0 0
Now What i want is like this..
HICN_ID HC
1234A 2
1234A 4
1234A 6
3456D 1
3456D 5
Can you please help me on this
thanks
nickPlease look at the same scenario from the below link.
You can accomplish this by a "pivot" query. Please look at the small testcase that I prepared below:
SQL> desc t1
Name Null? Type
NAME VARCHAR2(10)
YEAR NUMBER(4)
VALUE NUMBER(4)
SQL>
SQL> select * from t1;
NAME YEAR VALUE
john 1991 1000
john 1992 2000
john 1993 3000
jack 1991 1500
jack 1992 1200
jack 1993 1340
mary 1991 1250
mary 1992 2323
mary 1993 8700
9 rows selected.
SQL> -- now, try out the pivot query
SQL> select year,
2 max( decode( name, 'john', value, null ) ) "JOHN",
3 max( decode( name, 'jack', value, null ) ) "JACK",
4 max( decode( name, 'mary', value, null ) ) "MARY"
5 from
6 (
7 select name, year, value
8 from t1
9 )
10 group by year ;
YEAR JOHN JACK MARY
1991 1000 1500 1250
1992 2000 1200 2323
1993 3000 1340 8700
SQL>
Hope that helps.
Source : http://p2p.wrox.com/oracle/11931-sql-query-convert-columns-into-rows.html
Thanks,
Balaji K. -
[CS2/CS3 JS] Inserting columns or rows in tables
Anybody here know how to insert or add column(s)/row(s) in tables in Indesign Javascript? Please help... Thanks...
Hi Joaquin,
you might have to take use of the add()-command:
Add a column before the first column:
myTable.columns.add( LocationOptions.BEFORE, myTable.columns[0] );
Add a row after the last row:
myTable.rows.add( LocationOptions.AFTER, myTable.rows[-1] );
Martin -
Hello everyone,
I am developing an application (ALV OO) to select more than 1 column by row ( one ). This is an a holiday application so the idea is:
-One column will be the day, and the row will be the user.
So I am trying to select more than one day by user (that would be the row).
I am using the method set_table_for_first_display but when it shows the alv, doesn't let me to select more than one column with a click of the mouse.
Does anybody know if I can do this (select more than one column, by row) in somehow?
Please let me know if you need more clarification about this.
Thanks in advance
DiegoHi Diego,
it's all in the documentation.
set different selection modes through the value of the field u201CSEL_MODEu201D in the layout structure.
SPACE
same as 'B'
see 'B'
Default setting
'A'
Column and row selection
Multiple columns
Multiple rows
The user selects the rows through pushbuttons at the left border of the grid control.
'B'
Simple selection, list box
Multiple columns
Multiple rows
'C'
Multiple selection, list box
Multiple columns
Multiple rows
'D'
Cell selection
Multiple columns
Multiple rows
Any cells
The user selects the rows through pushbuttons at the left border of the grid control
Beyond setting this option, you can set u201CNO_ROWMARKu201D option to hide the mark column which is normally visible when the selection mode allows multiple row selection.
One point to notice here is that if you set your ALV Grid as to be editable, it may override your selection mode regardless of your layout settings.
This is from SDN Community Contribution "An Easy Reference for ALV Grid Control" By: Serdar ŞİMŞEKLER
Sorry, no link,. it's on my disk.
Regards,
Clemens
Maybe you are looking for
-
Something went wrong while downloading your template
We have Office 2013 installed on Windows 7 Enterprise 64 Bit. When trying to download templates from Word, we get the message Something went wrong while downloading your template. We have confirmed it's not the firewall blocking the download. We have
-
Header and Item consolidation delta
Hi all, If I do a lookup on header DSO while loading into a consolidated DSO from a item level DSO, how do we make sure that deltas are managed if only changes occur in the header DSO? In this case since the driver is the item level DSO, I would imag
-
Variant/Sales Order Costing: Plz suggest
Hi Gurus, My scenario is that the company is making machine tools - A machine is assembled from about 60 sub-assemblies (60 sub-assemblies in BOM). Which can be produced in-house or purchased. Each of these sub assemblies have further child sub-assem
-
Appearance.properties file
Hi there, I am trying to open the appearance.properties file for my project but it says "Windows can't open this file" - what program do I need to download to be able to open and edit this file? Thanks!
-
Creative suite updates take too long if they update at all
why does the creative suite (in my case) take days upon days to update? it seems to get stuck and won't update any further. the blue sliding dohinky that shows how far the update has gone seems to get stuck and won't go any further. I've tried to kee