472,353 Members | 1,806 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

Converting Rows into Columns without affecting the data in the table.

1
Hi,
I need a query which would convert Rows into Columns without causing any damages to the original data. I am not supposed to solve this by creating temporary tables and later dropping it.

I have a table which is in the below format:

LOGTYPE CLLI SWREL RPTDATE CNAME CVALUE
avl-stplan tahlt01 EAGLE537 08/11/2007 TCPRSTSENT 12
avl-stplan tahlt01 EAGLE537 08/11/2007 IPHDRERR 88
avl-stplan tahlt01 EAGLE537 08/11/2007 IPADDRERR 65
avl-stplan tahlt01 EAGLE537 08/11/2007 IPPROTERR 34
avl-stplan tahlt01 EAGLE537 08/11/2007 STATUS N
avl-stplan tahlt01 EAGLE537 08/11/2007 TYPE LIM
avl-stplan tahlt01 EAGLE537 08/11/2007 LOC 1216
avl-stplan tahlt02 EAGLE537 08/11/2007 TCPRSTSENT 40
avl-stplan tahlt02 EAGLE537 08/11/2007 IPHDRERR 22
avl-stplan tahlt02 EAGLE537 08/11/2007 IPADDRERR 60
avl-stplan tahlt02 EAGLE537 08/11/2007 IPPROTERR 28
avl-stplan tahlt02 EAGLE537 08/11/2007 STATUS K
avl-stplan tahlt02 EAGLE537 08/11/2007 TYPE LIM
avl-stplan tahlt02 EAGLE537 08/11/2007 LOC 1209



I need a Output which has the following format:

LOGTYPE CLLI SWREL RPTDATE TCPRSTSENT IPHDRERR IPADDRERR IPPROTERR STATUS TYPE LOC
avl-stplan tahlt01 EAGLE537 08/11/2007 12 88 65 34 N LIM 1216
avl-stplan tahlt02 EAGLE537 08/11/2007 40 22 60 28 K LIM 1209

NOTE: I just gave above data as a sample. Actually the number of CNAME rows is more than 30 per (LOGTYPE and CLLI and RPTDATE). For this reason, (if available) I do not want write every CNAME in solution sql.

Please help with some solution.

Thanks,
Recep
Dec 24 '07 #1
2 1871
Here's how you can do a pivot query in Oracle:

Expand|Select|Wrap|Line Numbers
  1. chris@XE> drop table t;
  2.  
  3. Table dropped.
  4.  
  5. Elapsed: 00:00:00.04
  6. chris@XE>
  7. chris@XE> create table t (
  8.   2  LOGTYPE  varchar2(20),
  9.   3  CLLI     varchar2(20),
  10.   4  SWREL    varchar2(20),
  11.   5  RPTDATE  date,
  12.   6  CNAME    varchar2(20),
  13.   7  CVALUE   varchar2(20)
  14.   8  );
  15.  
  16. Table created.
  17.  
  18. Elapsed: 00:00:00.01
  19. chris@XE>
  20. chris@XE> insert into t values('avl-stplan', 'tahlt01', 'EAGLE537', to_date('08/11/2007','dd/mm/yyyy'), 'TCPRSTSENT' ,'12');
  21.  
  22. 1 row created.
  23.  
  24. Elapsed: 00:00:00.01
  25. chris@XE> insert into t values('avl-stplan', 'tahlt01', 'EAGLE537', to_date('08/11/2007','dd/mm/yyyy'), 'IPHDRERR'   ,'88');
  26.  
  27. 1 row created.
  28.  
  29. Elapsed: 00:00:00.01
  30. chris@XE> insert into t values('avl-stplan', 'tahlt01', 'EAGLE537', to_date('08/11/2007','dd/mm/yyyy'), 'IPADDRERR'  ,'65');
  31.  
  32. 1 row created.
  33.  
  34. Elapsed: 00:00:00.00
  35. chris@XE> insert into t values('avl-stplan', 'tahlt01', 'EAGLE537', to_date('08/11/2007','dd/mm/yyyy'), 'IPPROTERR'  ,'34');
  36.  
  37. 1 row created.
  38.  
  39. Elapsed: 00:00:00.00
  40. chris@XE> insert into t values('avl-stplan', 'tahlt01', 'EAGLE537', to_date('08/11/2007','dd/mm/yyyy'), 'STATUS'     ,'N');
  41.  
  42. 1 row created.
  43.  
  44. Elapsed: 00:00:00.01
  45. chris@XE> insert into t values('avl-stplan', 'tahlt01', 'EAGLE537', to_date('08/11/2007','dd/mm/yyyy'), 'TYPE'       ,'LIM');
  46.  
  47. 1 row created.
  48.  
  49. Elapsed: 00:00:00.00
  50. chris@XE> insert into t values('avl-stplan', 'tahlt01', 'EAGLE537', to_date('08/11/2007','dd/mm/yyyy'), 'LOC'        ,'1216');
  51.  
  52. 1 row created.
  53.  
  54. Elapsed: 00:00:00.00
  55. chris@XE> insert into t values('avl-stplan', 'tahlt02', 'EAGLE537', to_date('08/11/2007','dd/mm/yyyy'), 'TCPRSTSENT' ,'40');
  56.  
  57. 1 row created.
  58.  
  59. Elapsed: 00:00:00.00
  60. chris@XE> insert into t values('avl-stplan', 'tahlt02', 'EAGLE537', to_date('08/11/2007','dd/mm/yyyy'), 'IPHDRERR'   ,'22');
  61.  
  62. 1 row created.
  63.  
  64. Elapsed: 00:00:00.01
  65. chris@XE> insert into t values('avl-stplan', 'tahlt02', 'EAGLE537', to_date('08/11/2007','dd/mm/yyyy'), 'IPADDRERR'  ,'60');
  66.  
  67. 1 row created.
  68.  
  69. Elapsed: 00:00:00.01
  70. chris@XE> insert into t values('avl-stplan', 'tahlt02', 'EAGLE537', to_date('08/11/2007','dd/mm/yyyy'), 'IPPROTERR'  ,'28');
  71.  
  72. 1 row created.
  73.  
  74. Elapsed: 00:00:00.01
  75. chris@XE> insert into t values('avl-stplan', 'tahlt02', 'EAGLE537', to_date('08/11/2007','dd/mm/yyyy'), 'STATUS'     ,'K');
  76.  
  77. 1 row created.
  78.  
  79. Elapsed: 00:00:00.01
  80. chris@XE> insert into t values('avl-stplan', 'tahlt02', 'EAGLE537', to_date('08/11/2007','dd/mm/yyyy'), 'TYPE'       ,'LIM');
  81.  
  82. 1 row created.
  83.  
  84. Elapsed: 00:00:00.01
  85. chris@XE> insert into t values('avl-stplan', 'tahlt02', 'EAGLE537', to_date('08/11/2007','dd/mm/yyyy'), 'LOC'        ,'1209');
  86.  
  87. 1 row created.
  88.  
  89. Elapsed: 00:00:00.01
  90. chris@XE>
  91. chris@XE> commit;
  92.  
  93. Commit complete.
  94.  
  95. Elapsed: 00:00:00.01
  96. chris@XE>
  97. chris@XE> select * from t
  98.   2
  99. chris@XE> SELECT   logtype,
  100.   2           clli,
  101.   3           swrel,
  102.   4           rptdate,
  103.   5           MAX (DECODE (cname,'TCPRSTSENT', cvalue)) tcprstsent,
  104.   6           MAX (DECODE (cname,'IPHDRERR', cvalue)) IPHDRERR,
  105.   7           MAX (DECODE (cname,'IPADDRERR', cvalue)) IPADDRERR,
  106.   8           MAX (DECODE (cname,'IPPROTERR', cvalue)) IPPROTERR,
  107.   9           MAX (DECODE (cname,'STATUS', cvalue)) STATUS,
  108.  10           MAX (DECODE (cname,'TYPE', cvalue)) TYPE,
  109.  11           MAX (DECODE (cname,'LOC', cvalue)) LOC
  110.  12  FROM     t
  111.  13  GROUP BY logtype,
  112.  14           clli,
  113.  15           swrel,
  114.  16           rptdate;
  115.  
  116. LOGTYPE     CLLI     SWREL     RPTDATE      TCPRSTSENT  IPHDRERR  IPADDRERR  IPPROTERR  STATUS  TYPE  LOC
  117. ----------- -------- --------- ------------ ----------- --------- ---------- ---------- ------- ----- -----
  118. avl-stplan  tahlt02  EAGLE537  08-NOV-2007  40          22        60         28         K       LIM   1209
  119. avl-stplan  tahlt01  EAGLE537  08-NOV-2007  12          88        65         34         N       LIM   1216
  120.  
  121. Elapsed: 00:00:00.03
  122. chris@XE>
  123.  
Dec 25 '07 #2
debasisdas
8,127 Expert 4TB
Please find a related discussion here and few others in the How To section of Oracle.
Dec 26 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Marek Mänd | last post by:
Please explain to a experienced fool like me, how to hide table rows correctly at this case....
3
by: Jim Heavey | last post by:
Trying to figure out the technique which should be used to add rows to a datagrid. I am thinking that I would want an "Add" button on the footer,...
1
by: Andrew | last post by:
Hey all, I am very new to ASP.Net (and .Net in general), but that isn't stopping the boss from wanting to begin new projects in it. This latest...
1
by: Ramakrishnan Nagarajan | last post by:
Hi, I am converting Excel data into a Dataset in C#. There are around 24 columns in the Excel Sheet. First I tried to insert one row with correct...
13
by: ppateel | last post by:
Hi, I am new to c++ and I am converting a c program to c++. I changed malloc call to new and I am getting an exception violation. Here is the...
8
by: JoeM | last post by:
I have a list of data in a database(access). How can I get the total rows in the database? Thanks
7
by: Susan Mackay | last post by:
I have a data table that is connected to a database table with a data adapter in the 'standard' manner. However I want to be able to remove...
6
Vasuki Masilamani
by: Vasuki Masilamani | last post by:
Hi, I need a query which would convert Columns into Rows without causing any damages to the original data. I am not supposed to solve this by...
0
by: Orbie | last post by:
Hi Guys, I need some help with pivoting or converting some rows on a Table into columns using SQL Server 2008! I have a Table which contains the...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.