I have read only access of this database and would like to change this query around so that I can compare the tblBranchTransfers.BranchId in the sub-query to a number, say '1' and if it doesn't exist, check the tblItems.BranchId for 1.I can't seem to find a way to do a sub-query or join or case statement to make this work.SELECT Findex, BranchID, DivisionID, CategoryId, SerialNumber, (SELECT TOP 1 BranchID FROM tblBranchTransfers WHERE Findex = tblItems.Findex ORDER BY DateApplied DESC,AID DESC ) as current_BranchID FROM tblItems
I thought a join on a virtual table may be the way to go but can't think how to get only the 'top' result (so only the most recent result for Findex of 5040 for the Findex of this query:SELECT * FROM tblBranchTransfers WHERE Quantity = 1 ORDER BY DateApplied DESC, AID DESCAID BranchId Findex SerialNo Quantity DateApplied Version----------- -------- ------ -------------------------------- -------- ----------------------- -----------24986 8 5040 1 2012-06-28 13:15:00.000 823024984 8 2618 1 2012-06-28 13:15:00.000 823024982 8 5034 1 2012-06-28 13:15:00.000 823024980 8 1969 1 2012-06-28 13:15:00.000 823024978 8 6904 1 2012-06-28 13:15:00.000 823024970 1 2635 1 2012-06-28 08:45:00.000 822824976 1 5040 1 2012-06-27 17:00:00.000 822924974 1 5034 1 2012-06-27 17:00:00.000 822924972 1 6904 1 2012-06-27 17:00:00.000 8229
Any idea's or suggestions?