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 

SQL to form Columns from Rows

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


Joined: 24 Feb 2005
Posts: 33
Topics: 17

PostPosted: Mon May 08, 2006 4:36 pm    Post subject: SQL to form Columns from Rows Reply with quote

Hello, I have the following situation.

If I select the Pay information of a person for a period (10/1 -10/31) from one of the tables the results are like this
Code:

SSN      PY_END_DT     Gross-to-net     Amt
----     -----------   -----------      -----
999      10/31/2005     SWT              50.00
999      10/31/2005     FWT            1200.00
999      10/31/2005     GPY             700.00

But I want it display as follows
Code:

SSN     PY_END_DT         SWT       FWT       GPY   
----    --------------   -----      ----     ----
999     10/31/2005       50.00    1200.00   700.00 

How do I convert the output rows and display as columns in SPUFI?
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


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

PostPosted: Mon May 08, 2006 7:01 pm    Post subject: Reply with quote

lacoe1,

Assuming that the AMT column is defined as Decimal, the following sql will give you the desired results.

Code:

SELECT SSN                     
      ,PY_END_DT               
      ,SUM(CASE GROSS_TO_NET   
            WHEN 'SWT' THEN AMT
            ELSE DECIMAL(0)     
       END) AS SWT             
      ,SUM(CASE GROSS_TO_NET   
            WHEN 'FWT' THEN AMT
            ELSE DECIMAL(0)     
       END) AS FWT             
      ,SUM(CASE GROSS_TO_NET   
            WHEN 'GPY' THEN AMT
            ELSE DECIMAL(0)     
       END) AS GPY             
  FROM Table                     
 GROUP BY SSN                   
         ,PY_END_DT             
         ;                     


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
lacoe1
Beginner


Joined: 24 Feb 2005
Posts: 33
Topics: 17

PostPosted: Mon May 08, 2006 8:24 pm    Post subject: Reply with quote

Thanks Kolusu
Back to top
View user's profile Send private message
astro
Beginner


Joined: 05 Oct 2005
Posts: 31
Topics: 7

PostPosted: Fri May 12, 2006 4:44 am    Post subject: Reply with quote

Hi Kolusu,
Just a doubt.
Do we really need SUM function here ?
I think without SUM function also we can get the desired result of lacoe1.
Pl. explain
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


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

PostPosted: Fri May 12, 2006 7:32 am    Post subject: Reply with quote

Quote:

I think without SUM function also we can get the desired result of lacoe1.


Astro,

The objective here is to get one 1 row from 3 rows. If I did not use the sum then you will have 3 rows as shown below. The case statement is used to pad zero.

Code:

-+---------+---------+---------+---------+---------+---------+-----
SSN  PY_END_DT               SWT              FWT              GPY
-+---------+---------+---------+---------+---------+---------+-----
999  2005-10-31            50.00              .00              .00
999  2005-10-31              .00              .00           700.00
999  2005-10-31              .00          1200.00              .00


Once you use the sum grouping on the ssn and py_end_dt you will have the results as follows

Code:

-+---------+---------+---------+---------+---------+---------+---------+
SSN  PY_END_DT                 SWT                FWT                GPY
-+---------+---------+---------+---------+---------+---------+---------+
999  2005-10-31              50.00            1200.00             700.00


Hope this clears your doubts

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


Joined: 05 Oct 2005
Posts: 31
Topics: 7

PostPosted: Fri May 12, 2006 7:50 am    Post subject: Reply with quote

Hi Kolusu,
Thanks for your nice explanation
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