Here is my select statement,
SELECT TOP 1
EmpId, RemainingAdvance
FROM SalaryDetails
WHERE EmpId IN (SELECT Emp_Id
FROM Employee
WHERE Desig_Id='27')
ORDER BY CreatedDate DESC
When i executed SELECT Emp_Id FROM Employee WHERE Desig_Id='27' the results were
Emp_Id
16
17
But when i execute my first statement it gives me result for only 16 but no output 17…
I have records for both EmpId’s in SalaryDetails Table…..
EDIT:
Removing TOP 1 from my query i got this,
SELECT EmpId, RemainingAdvance FROM SalaryDetails
where EmpId in (select Emp_Id from Employee where Desig_Id='27')
ORDER BY CreatedDate DESC
gave me
I want results for EmpId 16,17 ORDER BY CreatedDate DESC… Because my now my Desig_Id='27' and i will change it with a variable @CategoryId … So there may be ‘n’ number of employees based on @CategoryId
EmpId RemainingAdvance
16 354.00
17 0.00

Note: The query is written without trying it out.
I suppose, this is what you are looking for.
This variant also works (in at least one DBMS – namely IBM Informix Dynamic Server 11.50):
The compound join in the first ON clause might improve the performance of the query – but it is quite possible that the optimizer would hoist the ‘AND SD.CreatedDate = SD2.MaxDate’ condition anyway, meaning you wouldn’t spot any difference even if you examined the two query plans. I’m not quite sure of the best way to indent the table expressions after the main FROM clause.
Edited by Jonathan Leffler – as requested by Shahkalpesh.