Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Ms sql left join and where problem

Posted on 2010-09-05
1
Medium Priority
?
312 Views
Last Modified: 2012-05-10
I have thos sql query, works fine:

select * from (SELECT hosp,req_n,clas,marca,descr,req.fecha as fechar,titulo,(select count(*) from resp where req=req_n and ok=-1) as quan, ROW_NUMBER() OVER (ORDER BY req_n desc) AS ROWID   from req) as req left join resp as resp on resp.req=req.req_n left join usuarios on resp.prov=usuarios.id_u where rowid between 1 and 10

 need to add a where ok=-1:

on resp.req=req.req_n where ok=-1 left join

I get an error: Incorrect syntax near the keyword 'left'. How can I handle this condition.

Withput the last left join it works

select * from (SELECT hosp,req_n,clas,marca,descr,req.fecha as fechar,titulo,(select count(*) from resp where req=req_n and ok=-1) as quan, ROW_NUMBER() OVER (ORDER BY req_n desc) AS ROWID   from req) as req left join resp as resp on resp.req=req.req_n where ok=-1


0
Comment
Question by:robrodp
1 Comment
 
LVL 43

Accepted Solution

by:
pcelba earned 2000 total points
ID: 33607912
Two possibilities exist:

select * from
  (SELECT hosp,req_n,clas,marca,descr,req.fecha as fechar,titulo,
     (select count(*) from resp where req=req_n and ok=-1) as quan,
     ROW_NUMBER() OVER (ORDER BY req_n desc) AS ROWID   from req) as req
 left join resp as resp on resp.req=req.req_n AND resp.ok = -1
 left join usuarios on resp.prov=usuarios.id_u
where rowid between 1 and 10

select * from
  (SELECT hosp,req_n,clas,marca,descr,req.fecha as fechar,titulo,
     (select count(*) from resp where req=req_n and ok=-1) as quan,
     ROW_NUMBER() OVER (ORDER BY req_n desc) AS ROWID   from req) as req
 left join resp as resp on resp.req=req.req_n
 left join usuarios on resp.prov=usuarios.id_u
where rowid between 1 and 10  AND resp.ok = -1

They are a little bit different, so you have to check results
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Screencast - Getting to Know the Pipeline

886 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question