Solved

Need some help with an SQL Query...

Posted on 2014-11-19
5
113 Views
Last Modified: 2014-11-19
HI all,

I have a query that I am struggling with at the moment and wondered if someone could help me?

I have two tables, one with a list of equipment and the other with alarms associated to each equipment.

For example, if I have:

tblEquipment
ID, SerialNo
0, "111"
1, "222"
2, "333"
3, "444"

tblAlarms
EquipmentID, AlarmID
1,66
1,67
2,66
3,33

What I need to do is get a list of all the Equipment that DOES NOT have alarm 66 associated with it.
This should give 0 and 3

Can someone help me please?

Many thanks,

James
0
Comment
Question by:jatkin
  • 3
  • 2
5 Comments
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40452402
>list of all the Equipment that DOES NOT have alarm 66 associated with it.
This should give 0 and 3
SELECT DISTINCT ID, SerialNo
FROM tblEquipment e
   --- LEFT JOIN means return all rows from e...
   LEFT JOIN tblAlarms a ON e.EquipmentID = a.EquipmentID
-- ... and then filter out those rows without a matching 66
WHERE a.AlarmId <> 66

Open in new window


For more reading on JOINs check out Tim Chapman's article SQL Server: Table Joins Explained!

Also to get your head wrapped around the different JOINS I recommend going to images.google.com and search for 'SQL TABLE JOINS', and check out all the pretty cartoons on joins.
0
 
LVL 4

Author Comment

by:jatkin
ID: 40452421
I had tried something similar to this, but if the ID does not appear in the tblAlarms table, the condition <> 66 does not seem to include 'null'
0
 
LVL 65

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 40452433
>the ID does not appear in the tblAlarms table
Typo, changed e.EquipmentID to e.ID in the below code.

>the condition <> 66 does not seem to include 'null'
Ok.  NULLs can't be compared to anything, so in order for NULLs to appear change the WHERE clause to include ISNULL and a dummy number.
SELECT DISTINCT ID, SerialNo
FROM tblEquipment e
   --- LEFT JOIN means return all rows from e...
   LEFT JOIN tblAlarms a ON e.ID = a.EquipmentID
-- ... and then filter out those rows without a matching 66
WHERE ISNULL(a.AlarmId, 1) <> 66

Open in new window

0
 
LVL 4

Author Closing Comment

by:jatkin
ID: 40452452
Perfect!
Thanks so much for the quick help :-)
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40452498
No prob.  Thanks for the grade, good luck with your project.  -Jim
0

Featured Post

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.

Question has a verified solution.

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

Suggested Solutions

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Via a live example, show how to shrink a transaction log file down to a reasonable size.

856 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