join with subquery

I'm getting "incorrect syntax near keyword on" on the join to the contrstatuscodes table

      c.namesid as NamesId,
      c.contrid as ContrId,
    r.branchid as stat500

      from contracts c
      inner join names n
      on c.branchid = n.branchid
      and c.namesid = n.namesid

      left join ((select cs.branchid,
                       from contrstatuscodes cs
                       where cs.statuscode = '500') r
                        on r.branchid = n.branchid))  --Incorrect syntax near keyword 'on' here

                  payoutdate is not null
                  and payoutdate > '9/1/13'
            and stat500 is null
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
For starters, the parentheses are not correct, and you can lose the alias cs as the subquery only has one table  
left join (select branchid, namesid from contrstatuscodes where statuscode = '500') r
   on r.branchid = n.branchid  --Incorrect syntax near keyword 'on' here

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
No points please.

2 resources you may find useful:
this is very precise in locating TSQL syntax erorrs (once you click 'format')
this will find fewer TSQL specific syntax errors, and is a bit more 'forgiving'
for example click this - for "small" queries it provides a url.

Using this I think you can spot the parentheses problem that Jim has sorted out for you.

hope you like'em. Cheers.
Delta7428Author Commented:
Awesome!  Great resources PortletPaul.

Points coming jimhorn
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Thanks for the grade.  Good luck with your project.  -Jim
Delta7428Author Commented:
Thanks Jim
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.