Solved

SQL Query help required

Posted on 2011-09-08
6
245 Views
Last Modified: 2012-05-12
Hi

I'm trying to return data from a SQL Server databae using the following query:

SELECT id,thing1,ISNULL(Monitor, 0) AS Monitor
FROM Things as t  
LEFT OUTER JOIN ThingRelationships as t  
ON t.ID=t.ChildDeviceID  
WHERE t.AccountID =1
AND r.ParentDeviceID= (27)

Where I want all the things with the account Id of 1 to be returned whether there is a record in the ThingRelationships table or not, with a ParentDeviceID=27.

I'd expect the following, for example.

id    thing1            Monitor
12   something     0
23   something     0
34   something     0
56   something     0
2     something     0
4     something     0
56   something     0
43   something     1

as 43 is currently the only one with a relationship.

Thanks in advance!


0
Comment
Question by:Spike_66
6 Comments
 
LVL 5

Accepted Solution

by:
zvytas earned 500 total points
ID: 36501909
Try the following:

SELECT id,thing1,ISNULL(Monitor, 0) AS Monitor
FROM Things as t  left JOIN ThingRelationships as r ON t.ID=r.ChildDeviceID AND r.ParentDeviceID= (27)
WHERE t.AccountID =1
0
 
LVL 18

Expert Comment

by:deighton
ID: 36501964
you seem mixed up as to what t and r are, there seems to be 2 t's defined, but no r

try something like

SELECT id,thing1,ISNULL(Monitor, 0) AS Monitor
FROM Things as t  
LEFT OUTER JOIN ThingRelationships as r  
ON r.ID=t.ChildDeviceID  
WHERE t.AccountID =1
AND COALESCE(r.ParentDeviceID,27) = 27
0
 
LVL 9

Expert Comment

by:mimran18
ID: 36501985
Hi,
   Both the table is alias as "t". You need to change "Things as t  " or "ThingRelationships as t "
to as "r"
 
SELECT id,thing1,ISNULL(Monitor, 0) AS Monitor
FROM Things as t
LEFT OUTER JOIN ThingRelationships as rON t.ID=r.ChildDeviceID  
WHERE t.AccountID =1
AND r.ParentDeviceID= (27)
0
Industry Leaders: 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!

 

Author Closing Comment

by:Spike_66
ID: 36502020
Easy when you know how!
0
 

Author Comment

by:Spike_66
ID: 36502091
Not that it matters now but

"...Things as t
LEFT OUTER JOIN ThingRelationships as r..."

both tables aren't define as t
0
 

Author Comment

by:Spike_66
ID: 36502102
Oops- sorry - you were right- both were t- my bad!
0

Featured Post

Industry Leaders: 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

Suggested Solutions

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

730 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