views:

344

answers:

2

I've got a table of hardware and a table of incidents. Each hardware has a unique tag, and the incidents are tied to the tag.

How can I select all the hardware which has at least one incident listed as unresolved?

I can't just do a join, because then if one piece of hardware had multiple unresolved issues, it would show up multiple times.

+9  A: 
select distinct(hardware_name) 
from hardware,incidents 
where hardware.id = incidents.hardware_id and incidents.resolved=0;
Richard Harrison
I don't think the hardware table would have an indident_id since there can be multiple incidents for the same hardware. It should be the other way around.
Eric Hogue
thanks - you're right and I've corrected the answer.
Richard Harrison
+3  A: 

Something like this should do it:

Select A.HardwareID A.HadwareName, B.UnresolvedCount
From (Hardware A) 
Inner Join 
(
  Select HardwareID, Count(1) As UnresolvedCount 
  From Incidents 
  Where Resolved = 0 
  Group By HardwareID
) As B On A.HardwareID = B.HardwareID
Eric Hogue