Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

query not delivering unique records 1

Status
Not open for further replies.

LeaIbbs

Technical User
Joined
Mar 17, 2008
Messages
2
Location
GB
I am trying to set up a query to select PaymentDate, CustSurname, CustForename, AgentName showing only the last record for each customer if the last payment is over 7 days and also include customers with no payment.

the table structure is:

tblLoans
LoanID (PK)
CustID (FK)
AgentID(FK)

tblPayments
PaymentID (PK)
PaymentDate
LoanID (FK)

tblAgents
AgentID (PK)
AgentName

tblCustomers
CustID (PK)
CustSurname
Custforename


I have tried the code as below: but it now gives me every payment for the defaulters as opposed to just the last defaulted payment. Can you shed any light on what I have done wrong.

Code:
select b.PaymentDate, a.CustSurname, a.Custforename, c.AgentName, d.LoanID
from tblCustomers a, tblPayments b, tblAgents c, tblLoans d
where d.LoanID = b.LoanID and d.CustID = a.CustID and d.AgentID = c.AgentID
and (DATE()-(SELECT max(PaymentDate) from tblPayments f where f.LoanID=d.LoanID ))>7
UNION select '' as PaymentDate, a.CustSurname, a.Custforename, c.AgentName, d.LoanID
from 
tblCustomers a, tblLoans d, tblAgents c
where a.CustID = d.LoanID
and c.AgentID = d.AgentID
and (select count(*) from tblPayments where tblPayments.LoanID = d.LoanID) = 0;
 



Hi,

Use DISTINCT and ZERO for the date in the second query as DateTime values are NUMBERS...
Code:
select DISTINCT 
  b.PaymentDate
, a.CustSurname
, a.Custforename
, c.AgentName
, d.LoanID

from
  tblCustomers a
, tblPayments b
, tblAgents c
, tblLoans d

where d.LoanID = b.LoanID
  and d.CustID = a.CustID
  and d.AgentID = c.AgentID
  and (DATE()-(SELECT max(PaymentDate) from tblPayments f where f.LoanID=d.LoanID ))>7

UNION 

select DISTINCT
  0 
, a.CustSurname
, a.Custforename
, c.AgentName
, d.LoanID

from 
  tblCustomers a 
, tblLoans d
, tblAgents c

where a.CustID = d.LoanID
  and c.AgentID = d.AgentID
  and (select count(*) from tblPayments where tblPayments.LoanID = d.LoanID) = 0;


Skip,

[glasses]Have you heard that the roundest knight at King Arthur's round table was...
Sir Cumference![tongue]
 
Thanks Skip, I have tried this but still get the same result


it still gives me every payment for the defaulters as opposed to just the last defaulted payment per customer.
 




Your criteria does not define the MAX date...
Code:
select DISTINCT 
  MAX(b.PaymentDate)
, a.CustSurname
, a.Custforename
, c.AgentName
, d.LoanID

from
  tblCustomers a
, tblPayments b
, tblAgents c
, tblLoans d

where d.LoanID = b.LoanID
  and d.CustID = a.CustID
  and d.AgentID = c.AgentID
  and (DATE()-(SELECT max(PaymentDate) from tblPayments f where f.LoanID=d.LoanID ))>7

Group By 
  a.CustSurname
, a.Custforename
, c.AgentName
, d.LoanID
....


Skip,

[glasses]Have you heard that the roundest knight at King Arthur's round table was...
Sir Cumference![tongue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top