MVSFORUMS.com Forum Index MVSFORUMS.com
A Community of and for MVS Professionals
 
 FAQFAQ   SearchSearch   Quick Manuals   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

DB2 query for displaying rows into columns

 
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database
View previous topic :: View next topic  
Author Message
yadav2005
Intermediate


Joined: 10 Jan 2005
Posts: 348
Topics: 144

PostPosted: Tue Mar 06, 2007 5:49 am    Post subject: DB2 query for displaying rows into columns Reply with quote

Hai,

I have data in table in the format:
Code:

SEQNO      MARKS
-----      -----
1               10
1               20
1               15
1               40
1               33

I want the output in the format as:
Code:

SEQNO      MARKS
-----      -----
1               10
                15
                20
                40
                33
Back to top
View user's profile Send private message
Nic Clouston
Advanced


Joined: 01 Feb 2007
Posts: 1075
Topics: 7
Location: At Home

PostPosted: Tue Mar 06, 2007 6:05 am    Post subject: Reply with quote

Is that your actual data? What is the reasoning for the sequence in the output (it goes 10,20,15,40,33 on the table but 10,15,20,40,33 on output i.e. 15 and 20 have swapped)
_________________
Utility and Program control cards are NOT, repeat NOT, JCL.
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12375
Topics: 75
Location: San Jose

PostPosted: Tue Mar 06, 2007 6:16 am    Post subject: Reply with quote

yadav2005,

Look Up the QMF Reporting features.

Kolusu
_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
yadav2005
Intermediate


Joined: 10 Jan 2005
Posts: 348
Topics: 144

PostPosted: Thu Mar 08, 2007 4:34 am    Post subject: Reply with quote

Thanks Kolusu for your reply.

Can i have the query which will give the output in the form:
Code:

1   10   20   15   40   33
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12375
Topics: 75
Location: San Jose

PostPosted: Thu Mar 08, 2007 9:14 am    Post subject: Reply with quote

yadav2005,

Please search before posting. check this link

http://www.mvsforums.com/helpboards/viewtopic.php?t=5585&highlight=rows

Hope this helps...

Cheers

Kolusu
_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
yadav2005
Intermediate


Joined: 10 Jan 2005
Posts: 348
Topics: 144

PostPosted: Mon Mar 12, 2007 3:35 am    Post subject: Reply with quote

Kolusu,

I am unable to get the desired results.
Code:

CREATE TABLE TEMP                                                   
(                                                                       
SEQNO                  SMALLINT,                                       
MARKS                  SMALLINT                                       
);
INSERT INTO TEMP VALUES (1,1);                                       
INSERT INTO TEMP VALUES (1,2);                                       
INSERT INTO TEMP VALUES (1,3);                                       
INSERT INTO TEMP VALUES (1,4);                                       
SELECT SEQNO                                                           
      ,SUM(CASE WHEN MARKS = 1 THEN MARKS ELSE 0 END) AS M1           
      ,SUM(CASE WHEN MARKS = 2 THEN MARKS ELSE 0 END) AS M2           
      ,SUM(CASE WHEN MARKS = 3 THEN MARKS ELSE 0 END) AS M3           
      ,SUM(CASE WHEN MARKS = 4 THEN MARKS ELSE 0 END) AS M4           
  FROM TEMP                                                         
  GROUP BY SEQNO;                                                     

I am getting the error as below and how to solve this to get the desired output.
Code:

---------+---------+---------+---------+---------+---------+---------+---------+
DSNT408I SQLCODE = -401, ERROR:  THE OPERANDS OF AN ARITHMETIC OR COMPARISON   
         OPERATION ARE NOT COMPARABLE                                           
DSNT418I SQLSTATE   = 42818 SQLSTATE RETURN CODE                               
DSNT415I SQLERRP    = DSNXOBFC SQL PROCEDURE DETECTING ERROR                   
DSNT416I SQLERRD    = 930  0  0  -1  0  0 SQL DIAGNOSTIC INFORMATION           
DSNT416I SQLERRD    = X'000003A2'  X'00000000'  X'00000000'  X'FFFFFFFF'       
         X'00000000'  X'00000000' SQL DIAGNOSTIC INFORMATION                   
---------+---------+---------+---------+---------+---------+---------+---------+
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12375
Topics: 75
Location: San Jose

PostPosted: Mon Mar 12, 2007 6:57 am    Post subject: Reply with quote

yadav2005,

what is your version of DB2?

Any way try this

Code:

SELECT    SEQNO                                                     
      ,SUM(CASE WHEN MARKS = 1 THEN MARKS ELSE INT(0) END) AS M1 
      ,SUM(CASE WHEN MARKS = 2 THEN MARKS ELSE INT(0) END) AS M2 
      ,SUM(CASE WHEN MARKS = 3 THEN MARKS ELSE INT(0) END) AS M3 
      ,SUM(CASE WHEN MARKS = 4 THEN MARKS ELSE INT(0) END) AS M4 
  FROM TEMP                                                       
  GROUP BY SEQNO;                                                 


Kolusu
_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
yadav2005
Intermediate


Joined: 10 Jan 2005
Posts: 348
Topics: 144

PostPosted: Mon Mar 12, 2007 8:10 am    Post subject: Reply with quote

Kolusu,

Still i get the same error and i use DB2 version 7. Please let me know how to get the desired results.
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12375
Topics: 75
Location: San Jose

PostPosted: Mon Mar 12, 2007 9:56 am    Post subject: Reply with quote

yadav2005 wrote:
Kolusu,

Still i get the same error and i use DB2 version 7. Please let me know how to get the desired results.


There is no way you should get the error. The query works fine. Check and see if there is another table named TEMP where marks is defined as character data type.

Kolusu
_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
yadav2005
Intermediate


Joined: 10 Jan 2005
Posts: 348
Topics: 144

PostPosted: Wed Mar 14, 2007 5:27 am    Post subject: Reply with quote

Kolusu,

You are really great and you are correct and i am sorry to say that i was refering to a wrong table that is why i was not able to get the dessired results.

Thanks for your answer.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


MVSFORUMS
Powered by phpBB © 2001, 2005 phpBB Group