Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Syntax problem joining to select statements


Syntax problem joining to select statements

Author
Message
paul.chancey
paul.chancey
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 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
Luis Cazares
Luis Cazares
SSCrazy Eights
SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)

Group: General Forum Members
Points: 8474 Visits: 18081
Put parenthesis aroud the subqueries that will form your datasets.


Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
paul.chancey
paul.chancey
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search