If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Problems with a Simple Query

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-10-09, 08:57
rockdave35 rockdave35 is offline
Registered User
 
Join Date: Jan 2009
Posts: 43
Problems with a Simple Query

Hey guys,

I have the following query:


Code:

SELECT   distinct PR0.RECIP_SSN_NBR,
         PR01.AGTY_ID_NM, 
         PR0.RECIP_RETIR_DT,
         PR0.AGTY_ID_CD,
         PR02.ANTY_PYMT_TOT_AMT
         
          
FROM     DSNP.PR01_T_RECIP_SYS PR0, 
         DSNP.PR01_T_EMPR PR01, 
         DSNP.PR01_T_ANTY_PYMT PR02 
WHERE    PR0.AGTY_ID_CD=PR01.AGTY_ID_CD 
AND      PR0.RECIP_SSN_NBR=PR02.RECIP_SSN_NBR 
AND      PR0.AGTY_SYS_CD = 'TAPERS' 
AND      PR02.ANTY_PYMT_DT BETWEEN '2007-07-01' AND '2008-06-30'
They want to see the employee's most recent PR02.ANTY_PYMT_TOT_AMT. The problem is the results are displaying numerous records for the same SSN if they have different TOT_AMT's within my date range.

How can I modify the query to only pull the most recent payment amount going by the PR02.ANTY_PYMT_DT field?

Ex. SSN DATE PAYMENT
3334455 08/12/07 234.44
3334455 09/12/07 123.44

I want the query to display:

3334455 09/12/07 123.44


Thanks!
Reply With Quote
  #2 (permalink)  
Old 07-10-09, 09:10
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Usage of ROW_NUMBER() in this thread may help you.
Last transaction by customer and account
Reply With Quote
  #3 (permalink)  
Old 07-10-09, 09:28
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Or

Code:
SELECT   distinct PR0.RECIP_SSN_NBR,
         PR01.AGTY_ID_NM, 
         PR0.RECIP_RETIR_DT,
         PR0.AGTY_ID_CD,
         PR02.ANTY_PYMT_TOT_AMT
         
          
FROM     DSNP.PR01_T_RECIP_SYS PR0, 
         DSNP.PR01_T_EMPR PR01, 
         LATERAL
         (SELECT *
          FROM   DSNP.PR01_T_ANTY_PYMT PR02
          WHERE  PR0.RECIP_SSN_NBR=PR02.RECIP_SSN_NBR 
          AND    PR02.ANTY_PYMT_DT BETWEEN '2007-07-01' AND '2008-06-30'
          ORDER BY PR02.ANTY_PYMT_DT DESC
          FETCH FIRST 1 ROW ONLY) PR02
WHERE    PR0.AGTY_ID_CD=PR01.AGTY_ID_CD 
-- AND      PR0.RECIP_SSN_NBR=PR02.RECIP_SSN_NBR 
AND      PR0.AGTY_SYS_CD = 'TAPERS' 
-- AND      PR02.ANTY_PYMT_DT BETWEEN '2007-07-01' AND '2008-06-30'
;
LATERAL must be neccesary.
Please see Complex select (divide & return multiple rows with a single select)
for description.
Reply With Quote
  #4 (permalink)  
Old 07-10-09, 09:38
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
My question.
Is distinct necessary?
If so, why?(in other word, from where duplicate rows come?)
Reply With Quote
  #5 (permalink)  
Old 07-10-09, 09:46
rockdave35 rockdave35 is offline
Registered User
 
Join Date: Jan 2009
Posts: 43
Quote:
Originally Posted by tonkuma
My question.
Is distinct necessary?
If so, why?(in other word, from where duplicate rows come?)
You're right. The distinct doesn't do anything. Forgot to take that out. lol I'll give the Lateral a shot. Thanks!
Reply With Quote
  #6 (permalink)  
Old 07-10-09, 10:02
dav1mo dav1mo is offline
Registered User
 
Join Date: Dec 2007
Location: Richmond, VA
Posts: 782
Or since you want the most recent go back to just about the easiest way and ask for the most recent

SELECT PR0.RECIP_SSN_NBR,
PR01.AGTY_ID_NM,
PR0.RECIP_RETIR_DT,
PR0.AGTY_ID_CD,
PR02.ANTY_PYMT_TOT_AMT


FROM DSNP.PR01_T_RECIP_SYS PR0,
DSNP.PR01_T_EMPR PR01,
DSNP.PR01_T_ANTY_PYMT PR02
WHERE PR0.AGTY_ID_CD=PR01.AGTY_ID_CD
AND PR0.RECIP_SSN_NBR=PR02.RECIP_SSN_NBR
AND PR0.AGTY_SYS_CD = 'TAPERS'
AND PR02.ANTY_PYMT_DT = (select max(PR03.ANTY_PYMT_DT)
from DSNP.PR01_T_ANTY_PYMT PR03
where PR03.ANTY_PYMT_DT BETWEEN '2007-07-01' AND '2008-06-30' and PR02.RECIP_SSN_NBR = PR03.RECIP_SSN_NBR)

Dave
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On