Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Syntax problem joining to select statements Expand / Collapse
Author
Message
Posted Thursday, September 19, 2013 2:04 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, October 08, 2013 2:56 PM
Points: 3, Visits: 6
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 ,
distinctRecs.dateEntered ,
allRecs.preparedByFirst ,
allRecs.approvedByFirst ,
allRecs.preparedByLast ,
allRecs.approvedBylast
FROM

SELECT max([dateEntered]) ,
[branchNumber]
FROM [VaultRecap].[dbo].[cashMovement] distinctRecs
GROUP BY [branchNumber]

LEFT JOIN

SELECT [preparedByFirst] ,
[approvedByFirst] ,
[preparedByLast] ,
[approvedByLast] ,
[branchNumber] ,
[dateEntered]
FROM [VaultRecap].[dbo].[cashMovement] allRecs

ON allRecs.dateEntered = distinctRecs.dateEntered
AND allRecs.branchNumber = distinctRecs.branchNumber

ORDER BY distinctRecs.branchNumber
Post #1496576
Posted Thursday, September 19, 2013 2:16 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 6:14 PM
Points: 2,763, Visits: 5,908
Put parenthesis aroud the subqueries that will form your datasets.


Luis C.
I am a great believer in luck, and I find the harder I work the more I have of it. Stephen Leacock

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1496583
Posted Thursday, September 19, 2013 2:24 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, October 08, 2013 2:56 PM
Points: 3, Visits: 6
Thank you for the input. I got it working now with this code.

SELECT distinctRecs.branchNumber ,
distinctRecs.dateEntered ,
allRecs.preparedByFirst ,
allRecs.approvedByFirst ,
allRecs.preparedByLast ,
allRecs.approvedBylast
FROM
(SELECT max([dateEntered]) as dateEntered ,
[branchNumber]
FROM [VaultRecap].[dbo].[cashMovement]
GROUP BY [branchNumber]) distinctRecs
LEFT JOIN
(SELECT [preparedByFirst] ,
[approvedByFirst] ,
[preparedByLast] ,
[approvedByLast] ,
[branchNumber] ,
[dateEntered]
FROM [VaultRecap].[dbo].[cashMovement]) allRecs
ON allRecs.dateEntered = distinctRecs.dateEntered
AND allRecs.branchNumber = distinctRecs.branchNumber
ORDER BY distinctRecs.branchNumber
Post #1496587
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse