I have a DB table that contains a branchNumber and a dateEntered field among others.
I want to retrieve a unique record for each branchNumber with the latest dateEntered date. I have the sql below.
The first selection chooses fields from the joined selections.
The second selection gets the unique branchNumbers with the latest dateEntered from the table. (tested good)
which is left joined with
the third selection which gets all the records from the table. (tested good)
On allRecs.branchNumber and allRecs.dateEntered = distinctRecs.branchNumber and distinctRecs.dateEntered
My query below is giving me these syntax errors. Does anyone have some advise?
Msg 156, Level 15, State 1, Line 8
Incorrect syntax near the keyword 'SELECT'.
Msg 156, Level 15, State 1, Line 12
Incorrect syntax near the keyword 'LEFT'.
Msg 156, Level 15, State 1, Line 19
Incorrect syntax near the keyword 'ON'.
SELECT distinctRecs.branchNumber ,
SELECT max([dateEntered]) ,
FROM [VaultRecap].[dbo].[cashMovement] distinctRecs
GROUP BY [branchNumber]
SELECT [preparedByFirst] ,
FROM [VaultRecap].[dbo].[cashMovement] allRecs
ON allRecs.dateEntered = distinctRecs.dateEntered
AND allRecs.branchNumber = distinctRecs.branchNumber
ORDER BY distinctRecs.branchNumber