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
2 1871
Here's how you can do a pivot query in Oracle: - chris@XE> drop table t;
-
-
Table dropped.
-
-
Elapsed: 00:00:00.04
-
chris@XE>
-
chris@XE> create table t (
-
2 LOGTYPE varchar2(20),
-
3 CLLI varchar2(20),
-
4 SWREL varchar2(20),
-
5 RPTDATE date,
-
6 CNAME varchar2(20),
-
7 CVALUE varchar2(20)
-
8 );
-
-
Table created.
-
-
Elapsed: 00:00:00.01
-
chris@XE>
-
chris@XE> insert into t values('avl-stplan', 'tahlt01', 'EAGLE537', to_date('08/11/2007','dd/mm/yyyy'), 'TCPRSTSENT' ,'12');
-
-
1 row created.
-
-
Elapsed: 00:00:00.01
-
chris@XE> insert into t values('avl-stplan', 'tahlt01', 'EAGLE537', to_date('08/11/2007','dd/mm/yyyy'), 'IPHDRERR' ,'88');
-
-
1 row created.
-
-
Elapsed: 00:00:00.01
-
chris@XE> insert into t values('avl-stplan', 'tahlt01', 'EAGLE537', to_date('08/11/2007','dd/mm/yyyy'), 'IPADDRERR' ,'65');
-
-
1 row created.
-
-
Elapsed: 00:00:00.00
-
chris@XE> insert into t values('avl-stplan', 'tahlt01', 'EAGLE537', to_date('08/11/2007','dd/mm/yyyy'), 'IPPROTERR' ,'34');
-
-
1 row created.
-
-
Elapsed: 00:00:00.00
-
chris@XE> insert into t values('avl-stplan', 'tahlt01', 'EAGLE537', to_date('08/11/2007','dd/mm/yyyy'), 'STATUS' ,'N');
-
-
1 row created.
-
-
Elapsed: 00:00:00.01
-
chris@XE> insert into t values('avl-stplan', 'tahlt01', 'EAGLE537', to_date('08/11/2007','dd/mm/yyyy'), 'TYPE' ,'LIM');
-
-
1 row created.
-
-
Elapsed: 00:00:00.00
-
chris@XE> insert into t values('avl-stplan', 'tahlt01', 'EAGLE537', to_date('08/11/2007','dd/mm/yyyy'), 'LOC' ,'1216');
-
-
1 row created.
-
-
Elapsed: 00:00:00.00
-
chris@XE> insert into t values('avl-stplan', 'tahlt02', 'EAGLE537', to_date('08/11/2007','dd/mm/yyyy'), 'TCPRSTSENT' ,'40');
-
-
1 row created.
-
-
Elapsed: 00:00:00.00
-
chris@XE> insert into t values('avl-stplan', 'tahlt02', 'EAGLE537', to_date('08/11/2007','dd/mm/yyyy'), 'IPHDRERR' ,'22');
-
-
1 row created.
-
-
Elapsed: 00:00:00.01
-
chris@XE> insert into t values('avl-stplan', 'tahlt02', 'EAGLE537', to_date('08/11/2007','dd/mm/yyyy'), 'IPADDRERR' ,'60');
-
-
1 row created.
-
-
Elapsed: 00:00:00.01
-
chris@XE> insert into t values('avl-stplan', 'tahlt02', 'EAGLE537', to_date('08/11/2007','dd/mm/yyyy'), 'IPPROTERR' ,'28');
-
-
1 row created.
-
-
Elapsed: 00:00:00.01
-
chris@XE> insert into t values('avl-stplan', 'tahlt02', 'EAGLE537', to_date('08/11/2007','dd/mm/yyyy'), 'STATUS' ,'K');
-
-
1 row created.
-
-
Elapsed: 00:00:00.01
-
chris@XE> insert into t values('avl-stplan', 'tahlt02', 'EAGLE537', to_date('08/11/2007','dd/mm/yyyy'), 'TYPE' ,'LIM');
-
-
1 row created.
-
-
Elapsed: 00:00:00.01
-
chris@XE> insert into t values('avl-stplan', 'tahlt02', 'EAGLE537', to_date('08/11/2007','dd/mm/yyyy'), 'LOC' ,'1209');
-
-
1 row created.
-
-
Elapsed: 00:00:00.01
-
chris@XE>
-
chris@XE> commit;
-
-
Commit complete.
-
-
Elapsed: 00:00:00.01
-
chris@XE>
-
chris@XE> select * from t
-
2
-
chris@XE> SELECT logtype,
-
2 clli,
-
3 swrel,
-
4 rptdate,
-
5 MAX (DECODE (cname,'TCPRSTSENT', cvalue)) tcprstsent,
-
6 MAX (DECODE (cname,'IPHDRERR', cvalue)) IPHDRERR,
-
7 MAX (DECODE (cname,'IPADDRERR', cvalue)) IPADDRERR,
-
8 MAX (DECODE (cname,'IPPROTERR', cvalue)) IPPROTERR,
-
9 MAX (DECODE (cname,'STATUS', cvalue)) STATUS,
-
10 MAX (DECODE (cname,'TYPE', cvalue)) TYPE,
-
11 MAX (DECODE (cname,'LOC', cvalue)) LOC
-
12 FROM t
-
13 GROUP BY logtype,
-
14 clli,
-
15 swrel,
-
16 rptdate;
-
-
LOGTYPE CLLI SWREL RPTDATE TCPRSTSENT IPHDRERR IPADDRERR IPPROTERR STATUS TYPE LOC
-
----------- -------- --------- ------------ ----------- --------- ---------- ---------- ------- ----- -----
-
avl-stplan tahlt02 EAGLE537 08-NOV-2007 40 22 60 28 K LIM 1209
-
avl-stplan tahlt01 EAGLE537 08-NOV-2007 12 88 65 34 N LIM 1216
-
-
Elapsed: 00:00:00.03
-
chris@XE>
-
Please find a related discussion here and few others in the How To section of Oracle.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Marek Mänd |
last post by:
Please explain to a experienced fool like me, how to hide table rows
correctly at this case....
|
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,...
|
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...
|
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...
|
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...
|
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
|
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...
|
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...
|
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...
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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....
|
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...
|
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...
| |