Solved

Need some help with an SQL Query...

Posted on 2014-11-19
5
108 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
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.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

762 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

23 Experts available now in Live!

Get 1:1 Help Now