join with subquery

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


 select  
      c.BranchID,
      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,
                                 cs.namesid
                       from contrstatuscodes cs
                       where cs.statuscode = '500') r
                        on r.branchid = n.branchid))  --Incorrect syntax near keyword 'on' here


            where
                  payoutdate is not null
                  and payoutdate > '9/1/13'
            and stat500 is null
Delta7428Asked:
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

0

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
PortletPaulfreelancerCommented:
No points please.

2 resources you may find useful:

http://www.sql-format.com/
this is very precise in locating TSQL syntax erorrs (once you click 'format')

http://poorsql.com/
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.
0
Delta7428Author Commented:
Awesome!  Great resources PortletPaul.

Points coming jimhorn
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Thanks for the grade.  Good luck with your project.  -Jim
0
Delta7428Author Commented:
Thanks Jim
0
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.