Click here to Skip to main content
15,895,256 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I write one query in Ms access as follow

SQL
SELECT * from EmployeePaymentDetails where EmpId =@EmpID and PayID= ( select MAX(PayId) from  EmployeePaymentDetails where PayDate = ( select Max(PayDate) from EmployeePaymentDetails where EmpID=@EmpID) ) and PayDate = ( select Max(PayDate) from EmployeePaymentDetails where EmpID=@EmpID)


my table has 4 records as follows

EmployeePaymentDetails
PayID PayDate	      EmpID	AmountPaid	RemainingAmt
1   	19/05/2010	1	0	         5000
1   	19/06/2010	1	0	         10000
2   	19/05/2010	2	0	         5000
2   	19/06/2010	2	0	         10000



if I pass EmpId = 2 it give me correct anwser as RemainingAmt = 10000 but if I pass EmpId = 1 dr doesn't contains any thing why?

it should give me answer as RemainingAmt= 10000
Posted
Updated 18-Apr-10 22:05pm
v2

1 solution

This statement is returning 2 as the value.

(select MAX(PayId) from  EmployeePaymentDetails where PayDate = ( select Max(PayDate) from EmployeePaymentDetails where EmpID=@EmpID) )


That's evaluating as SELECT MAX(PayId) WHERE PayDate = '19/06/2010'. So your statement overall is evaluating as

SELECT * from EmployeePaymentDetails where EmpId = 1 and PayID = 2


...which of course returns no records.

Since PayID isn't unique, this logic doesn't really offer you anything. How about the below..?

SQL
SELECT 
    *
FROM 
    EmployeePaymentDetails
WHERE 
    (((EmployeePaymentDetails.EmpID)=[@EmpID]) 
AND 
    ((EmployeePaymentDetails.PayDate) =
(
    select Max(PayDate) from EmployeePaymentDetails where EmpID=@EmpID))
);
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900