?
Solved

Need some help with an SQL Query...

Posted on 2014-11-19
5
Medium Priority
?
116 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:James Atkin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 66

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:James Atkin
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 66

Accepted Solution

by:
Jim Horn earned 2000 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:James Atkin
ID: 40452452
Perfect!
Thanks so much for the quick help :-)
0
 
LVL 66

Expert Comment

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

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
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 videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Suggested Courses

770 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