473,382 Members | 1,368 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 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 1927
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. http://marekmand.kuubik.ee/iebug_canthide_table_rows_properly.htm Click on the header "label" and see...
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, but I am not quite sure how to do that. Is that...
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 project has me kinda stumped and after a couple...
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 values in the Excel sheet. i.e. for text columns...
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 relevant piece of code. Compiler vc++ 7.0 (.Net...
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 selected rows from the data table (i.e. no longer...
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 creating temporary tables and later dropping it. I...
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 same Products in 4 different Stores. I'm only...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.