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 

Sel rows in same table based where condition equal 1st Sel

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


Joined: 08 Oct 2004
Posts: 274
Topics: 52
Location: California

PostPosted: Fri Oct 08, 2004 1:05 pm    Post subject: Sel rows in same table based where condition equal 1st Sel Reply with quote

Hi, Ive been browsing this board for a while, decided to join.
I have a query(that works) I run in SPUFI. Is there a more efficent way to get my results? The query checks for a condition in the current period, if found then searches previous period for another conditon, then returns cols. for both(or more) hits. This is all in the same table. I also tried searching without any luck.
Thanks!
Code:

 SELECT DISTINCT                                                 
A.EMPRNO, A.SSNO, A.FIRST99, A.OVER99, A.LNAME, A.FNAME, A.ACCTNO,
A.PSTPERIOD,                                                     
C.EMPRNO, C.SSNO, C.FIRST99, C.OVER99, C.LNAME, C.FNAME, C.ACCTNO,
C.PSTPERIOD                                                       
 FROM HOURS.HRSDTL A                                             
           LEFT JOIN (SELECT *                                   
                 FROM HOURS.HRSDTL C                             
             WHERE C.EMPRNO = 10200  AND                         
    C.PSTPERIOD BETWEEN DATE('10/01/2003') AND                   
                            ('02/01/2004')) C ON A.SSNO = C.SSNO 
   WHERE (A.PSTPERIOD = '2003-10-01' AND                         
          A.EMPRNO IN (29300, 1100)) AND                         
     A.SSNO   IN (SELECT B.SSNO                                   
                  FROM HOURS.HRSDTL B                             
                   WHERE A.SSNO   = B.SSNO AND                   
                         B.EMPRNO = 10200  AND                   
    B.PSTPERIOD BETWEEN DATE('10/01/2003') AND ('02/01/2004'))   
 ORDER BY C.ACCTNO, C.SSNO;
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: Sat Oct 09, 2004 6:11 am    Post subject: Reply with quote

Nascar9,

welcome aboard. I think a case statement can be used to select the data instead of a left join. However can you put down your requirements with an example data?

You are also using harcoded values for the dates? Do you want to automate that part also ?

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


Joined: 08 Oct 2004
Posts: 274
Topics: 52
Location: California

PostPosted: Mon Oct 11, 2004 11:59 am    Post subject: Reply with quote

Thanks Koluso!
All data is in same table.
1. Find all rows with:
PSTPERIOD = '2003-10-01' and EMPRNO = 29300 or 1100
2. Use the rows from 1 and search for rows:
SSNO is equal and EMPRNO = 10200 and
PSTPERIOD BETWEEN DATE('10/01/2003') AND ('02/01/2004'
3. Return EMPRNO, SSNO, FIRST99, OVER99 LNAME, FNAME, ACCTNO, PSTPERIOD FOR BOTH 1 AND 2.
4. DISTINCT is not required, nice to have.
5. Dates ranges are fixed at this point, maybe a variable in future.
Here is a couple of rows from the query.

EMPRNO SSNO FIRST99 OVER99 LNAME FNAME ACCTNO PSTPERIOD EMPRNO SSNO FIRST99 OVER99 LNAME FNAME ACCTNO PSTPERIOD
1100 123456789 72.80 .00 GONZALEZ BILL 1058 2003-10-01 10200 123456789 99.00 61.00 GONZALEZ BILL 834 2004-02-01

1100 123456789 72.80 .00 GONZALEZ BILL 1058 2003-10-01 10200 123456789 97.20 .00 GONZALEZ BILL 834 2004-01-01
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 Oct 11, 2004 12:19 pm    Post subject: Reply with quote

Nascar9,

Try the following sql. I used a UNION clause to get the desired results.

Code:

SELECT EMPRNO                                         
      ,SSNO                                           
      ,FIRST99                                         
      ,OVER99                                         
      ,LNAME                                           
      ,FNAME                                           
      ,ACCTNO                                         
      ,PSTPERIOD                                       
  FROM TABLE                                           
 WHERE EMPRNO IN ('29300', '1100')                     
   AND PSTPERIOD = '2003-10-01'                       
UNION                                                 
SELECT EMPRNO                                         
      ,SSNO                                           
      ,FIRST99                                         
      ,OVER99                                         
      ,LNAME                                           
      ,FNAME                                           
      ,ACCTNO                                         
      ,PSTPERIOD                                       
  FROM TABLE                                           
 WHERE SSNO IN (SELECT SSNO                           
                  FROM TABLE                           
                 WHERE EMPRNO IN ('29300', '1100')     
                   AND PSTPERIOD = '2003-10-01')       
   AND PSTPERIOD BETWEEN '2003-10-01' AND '2004-02-01'
   ; 


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
NASCAR9
Intermediate


Joined: 08 Oct 2004
Posts: 274
Topics: 52
Location: California

PostPosted: Mon Oct 11, 2004 12:56 pm    Post subject: Reply with quote

Kolusu, I ran your SQL tweeked like this:
Code:

 SELECT EMPRNO                                         
      ,SSNO                                           
      ,FIRST99                                         
      ,OVER99                                         
      ,LNAME                                           
      ,FNAME                                           
      ,ACCTNO                                         
      ,PSTPERIOD                                       
  FROM HOURS.HRSDTL                                   
  WHERE EMPRNO IN (29300, 1100)                         
   AND PSTPERIOD = '2003-10-01'                       
UNION                                                 
SELECT EMPRNO                                         
      ,SSNO                                           
      ,FIRST99                                         
      ,OVER99                                         
      ,LNAME                                           
      ,FNAME                                           
      ,ACCTNO                                         
      ,PSTPERIOD                                       
  FROM HOURS.HRSDTL                                   
  WHERE SSNO IN (SELECT SSNO                             
                  FROM HOURS.HRSDTL                   
                 WHERE EMPRNO IN (29300, 1100)         
                   AND PSTPERIOD = '2003-10-01')       
   AND PSTPERIOD BETWEEN '2003-10-01' AND '2004-02-01'
   AND EMPRNO = 10200                               
   ;                                 

The results were every row in the first select, and the rows that matched in union condition.

The requirerments are if first condiotion TRUE, then check for second condition. If both are true then output information.

I do like how the output format is display, stacked. Just need to eliminate the un-needed rows.

Thanks
Back to top
View user's profile Send private message
NASCAR9
Intermediate


Joined: 08 Oct 2004
Posts: 274
Topics: 52
Location: California

PostPosted: Mon Oct 11, 2004 1:22 pm    Post subject: Reply with quote

Researching the 'UNION' I'm not sure it will do what is necessary.
Correct me if I'm wrong.
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 Oct 11, 2004 2:50 pm    Post subject: Reply with quote

Nascar9,

Quote:

The results were every row in the first select, and the rows that matched in union condition.


Not exactly true. The UNION clause eliminates the duplicates from the query. so the result set may also contain some records from the 2nd query also.

Quote:

The requirerments are if first condiotion TRUE, then check for second condition. If both are true then output information.

The query above is indeed checking for both the conditions to be true. The query in the second select statement makes sure that 1st condition is true.

Quote:

Researching the 'UNION' I'm not sure it will do what is necessary.


UNION is used eliminate duplicates. For a better understanding of the query , run each query individually and then compare the results to the query using UNION. You will be able to find out the duplicates eliminated.

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
NASCAR9
Intermediate


Joined: 08 Oct 2004
Posts: 274
Topics: 52
Location: California

PostPosted: Mon Oct 11, 2004 3:12 pm    Post subject: Reply with quote

Here are that stats from both queries:
UNION:
DSNE610I NUMBER OF ROWS DISPLAYED IS 42275
DSNE616I STATEMENT EXECUTION WAS SUCCESSFUL, SQLCODE IS 100
---------+---------+---------+---------+---------+---------+--
---------+---------+---------+---------+---------+---------+--
DSNE617I COMMIT PERFORMED, SQLCODE IS 0
DSNE616I STATEMENT EXECUTION WAS SUCCESSFUL, SQLCODE IS 0
---------+---------+---------+---------+---------+---------+--
DSNE601I SQL STATEMENTS ASSUMED TO BE BETWEEN COLUMNS 1 AND 72
DSNE620I NUMBER OF SQL STATEMENTS PROCESSED IS 1
DSNE621I NUMBER OF INPUT RECORDS READ IS 49
DSNE622I NUMBER OF OUTPUT RECORDS WRITTEN IS 43797


Left JOIN:
DSNE610I NUMBER OF ROWS DISPLAYED IS 38
DSNE616I STATEMENT EXECUTION WAS SUCCESSFUL, SQLCODE IS 100
---------+---------+---------+---------+---------+---------+-----
---------+---------+---------+---------+---------+---------+-----
DSNE617I COMMIT PERFORMED, SQLCODE IS 0
DSNE616I STATEMENT EXECUTION WAS SUCCESSFUL, SQLCODE IS 0
---------+---------+---------+---------+---------+---------+-----
DSNE601I SQL STATEMENTS ASSUMED TO BE BETWEEN COLUMNS 1 AND 72
DSNE620I NUMBER OF SQL STATEMENTS PROCESSED IS 1
DSNE621I NUMBER OF INPUT RECORDS READ IS 19
DSNE622I NUMBER OF OUTPUT RECORDS WRITTEN IS 74


What appear is UNION wrote out every matching condition. Maybe I'm missing something, but the doc I'm looking at seems to back this up.
Question Question Question
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 Oct 11, 2004 3:20 pm    Post subject: Reply with quote

Nascar9,

Add a DISTINCT verb in the subquery of the second sql.

Code:

WHERE SSNO IN (SELECT DISTINCT SSNO                             
                  FROM HOURS.HRSDTL                   
                 WHERE EMPRNO IN (29300, 1100)         
                   AND PSTPERIOD = '2003-10-01')       
   AND PSTPERIOD BETWEEN '2003-10-01' AND '2004-02-01'
   AND EMPRNO = 10200                               


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
NASCAR9
Intermediate


Joined: 08 Oct 2004
Posts: 274
Topics: 52
Location: California

PostPosted: Mon Oct 11, 2004 3:41 pm    Post subject: Reply with quote

Kolusu,
Same results 42,275. I've tried DISTINCT on every Select no change.
I really appreciate your efforts.
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