[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

SQL Query help required

Posted on 2011-09-08
6
Medium Priority
?
252 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 2000 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Suggested Courses

834 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