Solved

SQL Query help required

Posted on 2011-09-08
6
234 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

In this article I will describe the Detach & Attach 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.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now