Home » SQL & PL/SQL » SQL & PL/SQL » Need a Query to transpose rows to columns dynamically
Need a Query to transpose rows to columns dynamically [message #641283] Wed, 12 August 2015 23:55 Go to next message
yashaswini.gopalan
Messages: 7
Registered: August 2015
Location: bangalore
Junior Member
Table 1
Factor
Complexity
Severity
Impact


Table2
Test_ID Stage_ID Factor Factor_Comment Factor_value
Test1 Stage 1 Complexity Medium 3
Test1 Stage1 Severity L1 2
Test1 Stage2 Impact More 5
Test1 Stage2 Complexity Low 2


I have two such tables. One having the factors another having the comments and values for each factors. Factors in table 1 can grow dynamically. I need to generate the below report

TestID StageID Complexity Complexity_Factor_Value Severity Severity_Factor_Value Impact Impact_Factor_Value
Test1 Stage1 Medium 3 L1 2 NULL NULL
Test1 Stage2 L1 2 NULL NULL More 5

There will be more TestID and Stage ID. I need to bring in the test and stage combination into a single row with all factor's value and comment like specified above. The factors should be fetched from table 1.Thanks in advance.
Re: Need a Query to transpose rows to columns dynamically [message #641284 is a reply to message #641283] Thu, 13 August 2015 00:04 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
Hi,

Welcome to the forum!

Please read and follow the OraFAQ Forum Guide and How to use [code] tags.
Re: Need a Query to transpose rows to columns dynamically [message #641285 is a reply to message #641283] Thu, 13 August 2015 00:06 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
Welcome to this forum

Please read and follow the forum guidelines, to enable us to help you:
OraFAQ Forum Guide
How to use {code} tags and make your code easier to read


Please read and follow the forum guidelines, to enable us to help you:
OraFAQ Forum Guide
How to use {code} tags and make your code easier to read

Re: Need a Query to transpose rows to columns dynamically [message #641289 is a reply to message #641283] Thu, 13 August 2015 00:43 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

With any SQL or PL/SQL question, please, Post a working Test case: create table (including all constraints) and insert statements along with the result you want with these data then we will be able work with your table and data. Explain with words and sentences the rules that lead to this result.

Note that PIVOT is a FAQ and solutions depend on your Oracle version you should post with every question.

[Updated on: Thu, 13 August 2015 00:45]

Report message to a moderator

Re: Need a Query to transpose rows to columns dynamically [message #641292 is a reply to message #641289] Thu, 13 August 2015 01:25 Go to previous messageGo to next message
yashaswini.gopalan
Messages: 7
Registered: August 2015
Location: bangalore
Junior Member
The version is oracle 10g
Re: Need a Query to transpose rows to columns dynamically [message #641296 is a reply to message #641292] Thu, 13 August 2015 02:21 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

Good, now the test case.

Re: Need a Query to transpose rows to columns dynamically [message #641320 is a reply to message #641296] Thu, 13 August 2015 05:25 Go to previous messageGo to next message
yashaswini.gopalan
Messages: 7
Registered: August 2015
Location: bangalore
Junior Member
Hi,

Please find the queries to create the table


create table table1 (factor varchar2(30));
create table table2 (table_id varchar2(10),stage_id varchar2(10),factor varchar2(30),factor_comment varchar2(30),factor_value varchar2(30));

insert into table1 values ('Complexity');
insert into table1 values ('Severity');
insert into table1 values ('Impact');
insert into table2 values('Test1','Stage1','Complexity','Medium',3);
insert into table2 values('Test1', 'Stage1' ,'Severity', 'L1', 2);
insert into table2 values('Test1' ,'Stage2' ,'Impact', 'More', 5);
insert into table2 values('Test1', 'Stage2', 'Complexity' ,'Low',2 );

Thanks
Re: Need a Query to transpose rows to columns dynamically [message #641338 is a reply to message #641320] Thu, 13 August 2015 07:57 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

From what I see "table1" of no use as all information are in "table2".
SQL> select table_id, stage_id,
  2         max(decode(factor,'Complexity',factor_comment)) Complexity,
  3         max(decode(factor,'Complexity',factor_value)) Complexity_value,
  4         max(decode(factor,'Severity',factor_comment)) Severity,
  5         max(decode(factor,'Severity',factor_value)) Severity_value,
  6         max(decode(factor,'Impact',factor_comment)) Impact,
  7         max(decode(factor,'Impact',factor_value)) Impact_value
  8  from table2
  9  group by table_id, stage_id
 10  order by table_id, stage_id
 11  /
TABLE_ID   STAGE_ID   COMPLEXITY COMPLEXITY SEVERITY   SEVERITY_V IMPACT     IMPACT_VAL
---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
Test1      Stage1     Medium     3          L1         2
Test1      Stage2     Low        2                                More       5

Re: Need a Query to transpose rows to columns dynamically [message #641346 is a reply to message #641338] Thu, 13 August 2015 08:55 Go to previous messageGo to next message
yashaswini.gopalan
Messages: 7
Registered: August 2015
Location: bangalore
Junior Member
Thanks for the help. But the table 1 values has to be picked up dynamically. We cannot hardcode
Re: Need a Query to transpose rows to columns dynamically [message #641355 is a reply to message #641346] Thu, 13 August 2015 09:33 Go to previous messageGo to next message
cookiemonster
Messages: 13975
Registered: September 2008
Location: Rainy Manchester
Senior Member
Are saying that you need a pair of columns for every distinct value in tabl2.factor that corresponds to an entry in table1?

[Updated on: Thu, 13 August 2015 09:33]

Report message to a moderator

Re: Need a Query to transpose rows to columns dynamically [message #641358 is a reply to message #641346] Thu, 13 August 2015 09:53 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
yashaswini.gopalan wrote on Thu, 13 August 2015 15:55
Thanks for the help. But the table 1 values has to be picked up dynamically. We cannot hardcode


So post a test case with its result representing your problem otherwise we cannot give you an answer to problem if you post a test case which has nothing to do with the problem.
It is like: "Hello Doctor, here I am, my wife is sick, what she have to do?".

Re: Need a Query to transpose rows to columns dynamically [message #641378 is a reply to message #641358] Thu, 13 August 2015 22:42 Go to previous messageGo to next message
yashaswini.gopalan
Messages: 7
Registered: August 2015
Location: bangalore
Junior Member
All entries in Table1 has to come as the heading of my report. Its corresponding values and comments from table2 for each test id and stage id combination has to be shown in a single row
Re: Need a Query to transpose rows to columns dynamically [message #641380 is a reply to message #641378] Fri, 14 August 2015 00:10 Go to previous messageGo to next message
Littlefoot
Messages: 21826
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
yashaswini.gopalan wrote on Fri, 14 August 2015 05:42
All entries in Table1 has to come as the heading of my report.


Which tool do you use to create that report? Is it really SQL*Plus? Or is it (for example) Oracle Reports (or some other tool)?
Re: Need a Query to transpose rows to columns dynamically [message #641388 is a reply to message #641380] Fri, 14 August 2015 01:18 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

... and do you mean that if a value is in table2 and not in table1 then it is not displayed?

Re: Need a Query to transpose rows to columns dynamically [message #641416 is a reply to message #641388] Fri, 14 August 2015 07:22 Go to previous messageGo to next message
yashaswini.gopalan
Messages: 7
Registered: August 2015
Location: bangalore
Junior Member
Table 1 will be like a configuration table which has all the factor names. For each test id and stage id combination the comments and rating for all factors stored in table 1 has to be displayed in the report.
Re: Need a Query to transpose rows to columns dynamically [message #641424 is a reply to message #641416] Fri, 14 August 2015 10:25 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

You didn't answer to Littlefoot's question.
Let me state me clearer.
There is no way in pure SQL.
There are ways depending on your client tool.

Re: Need a Query to transpose rows to columns dynamically [message #641439 is a reply to message #641424] Fri, 14 August 2015 19:29 Go to previous messageGo to next message
Kevin Meade
Messages: 2103
Registered: December 1999
Location: Connecticut USA
Senior Member
I once saw a really brilliant piece of code that did exactly what you want. It used the ANYDATA data type. Sadly I have not been able to find it. I'll keep looking.
Re: Need a Query to transpose rows to columns dynamically [message #641441 is a reply to message #641439] Fri, 14 August 2015 20:24 Go to previous messageGo to next message
Kevin Meade
Messages: 2103
Registered: December 1999
Location: Connecticut USA
Senior Member
Think I found it.

This is an article that builds upon another article (isn't I.T. great... we openly steal from each other and we like it). Of course we give credit for what we have stolen.

The article is about some guy named Anton who created a pipelined function that can do a dynamic pivot. After you read the article you will likely think two things:

1. what did I just read, cause I am not sure I understand it all
2. but that is not what I asked for, I am not trying to pivot any data

#1 I get. I don't understand it all either. #2 though means you have missed the point. The point is, the query (select * from <pivot crap>) is generating a dynamic number of output columns. That is a head bender, but as I interpret your request, it is what you wanted right?. So read up and figure out how to translate it into something else you can use. Then remember to post your working solution so we can steal it from you. Indeed, given the repeating nature of your test case, you may not need to do too much alteration. Understand it and then warp it to your own twisted ends?

select * from table( pivot('select deptno,job,avg(sal) sal_avg from emp group by deptno,job'));


  DEPTNO    ANALYST      CLERK    MANAGER  PRESIDENT   SALESMAN     WIZARD
-------- ---------- ---------- ---------- ---------- ---------- ----------
      10                  1300       2450       5000                  1991
      20       3000        950       2975
      30                   950       2850                  1400
      40                                                   1100


Neat.

In the above example, DEPTNO is the key of the output row, JOB becomes all the columns, and sal_avg is the data in the matrix. If there were 50 job titles, there would be 51 columns. Thus you have a query that can dynamically output any number of columns. Now you just have to wade through the code this provides, in order to figure out the one special moment where the magic happens, and then exploit it.

Good luck. Kevin

Wish I could have met this Anton dude.

[Updated on: Fri, 14 August 2015 20:45]

Report message to a moderator

Re: Need a Query to transpose rows to columns dynamically [message #641448 is a reply to message #641439] Sat, 15 August 2015 02:00 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

Kevin Meade wrote on Sat, 15 August 2015 02:29
I once saw a really brilliant piece of code that did exactly what you want. It used the ANYDATA data type. Sadly I have not been able to find it. I'll keep looking.


I posted several times such "solutions".
As I said there is no solution in pure SQL.
The best way depends on the client tool.


[Updated on: Sat, 15 August 2015 02:03]

Report message to a moderator

Re: Need a Query to transpose rows to columns dynamically [message #641488 is a reply to message #641448] Mon, 17 August 2015 00:37 Go to previous messageGo to next message
yashaswini.gopalan
Messages: 7
Registered: August 2015
Location: bangalore
Junior Member
@michel Cadot.

If i get PL/SQL code snippet for this, even that would do.
Re: Need a Query to transpose rows to columns dynamically [message #641494 is a reply to message #641488] Mon, 17 August 2015 01:26 Go to previous message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

Answer our question.
It is simple, what is your problem with it?

[Updated on: Mon, 17 August 2015 01:27]

Report message to a moderator

Previous Topic: Need a Help on SQL query
Next Topic: Need a Stored Procedure
Goto Forum:
  


Current Time: Wed Jul 29 07:59:01 CDT 2026