I'm having a bit of a SQL problem and I can't figure it out. I have a
working solution, but it could be better.
I've got two tables, tblInspections and tblViolations. Each inspection
record has an associated establishment id. Each inspection record can have
multiple violation records associated with it. Each violation can be either
of type 1 (critical) or 0 (noncritical).
What I want is to get a list of inspections by establishmentid with a column
for the count of noncritical violations, and a count of critical violations.
EG.
Date NonCriticalViol ations CriticalViolati ons
5/30/2003 6 2
Here is my SQL and what it returns.
----------------------------------------------------------------------------
----------------------------------------------------------------
select
tblInspections. ReceivedByDate,
tblViolations.T ype,
count(tblViolat ions.Type) as ViolationCount
from
tblinspections
join tblViolations on tblInspections. ID = tblviolations.I nspID
where tblInspections. iestabid = 100
group by
tblViolations.T ype,
tblInspections. ReceivedByDate
----------------------------------------------------------------------------
----------------------------------------------------------------
and it returns:
+++++++++++++++ ++++++++
Date: Type Count
2003-05-20 0 2
2003-05-20 1 2
+++++++++++++++ ++++++++
I can work with this (because it is correct), but would prefer it all on one
record.
Here's what I've tried:
----------------------------------------------------------------------------
----------------------------------------------------------------
select
tblInspections. ReceivedByDate,
count(NCritVios .Type) as NCritViolationC ount,
count(CritVios. Type) as CritViolationCo unt
from
tblInspections
join tblViolations as NCritVios on NCritVios.Type = 0 and
tblInspections. ID = NCritVios.InspI D
join tblViolations as CritVios on CritVios.Type = 1 and
tblInspections. ID = CritVios.InspID
where
tblInspections. iestabid = 100
group by
tblInspections. ReceivedByDate, NCritVios.Type
----------------------------------------------------------------------------
----------------------------------------------------------------
and it retuns:
+++++++++++++++ ++++++++
Date: NCrit Crit
2003-05-20 4 4
+++++++++++++++ ++++++++
which is not correct.
Note that without the second count in the select and without the second
join, it works (counts ncrits correctly). It's that second join.
Any ideas?
Thanks.
Thom
working solution, but it could be better.
I've got two tables, tblInspections and tblViolations. Each inspection
record has an associated establishment id. Each inspection record can have
multiple violation records associated with it. Each violation can be either
of type 1 (critical) or 0 (noncritical).
What I want is to get a list of inspections by establishmentid with a column
for the count of noncritical violations, and a count of critical violations.
EG.
Date NonCriticalViol ations CriticalViolati ons
5/30/2003 6 2
Here is my SQL and what it returns.
----------------------------------------------------------------------------
----------------------------------------------------------------
select
tblInspections. ReceivedByDate,
tblViolations.T ype,
count(tblViolat ions.Type) as ViolationCount
from
tblinspections
join tblViolations on tblInspections. ID = tblviolations.I nspID
where tblInspections. iestabid = 100
group by
tblViolations.T ype,
tblInspections. ReceivedByDate
----------------------------------------------------------------------------
----------------------------------------------------------------
and it returns:
+++++++++++++++ ++++++++
Date: Type Count
2003-05-20 0 2
2003-05-20 1 2
+++++++++++++++ ++++++++
I can work with this (because it is correct), but would prefer it all on one
record.
Here's what I've tried:
----------------------------------------------------------------------------
----------------------------------------------------------------
select
tblInspections. ReceivedByDate,
count(NCritVios .Type) as NCritViolationC ount,
count(CritVios. Type) as CritViolationCo unt
from
tblInspections
join tblViolations as NCritVios on NCritVios.Type = 0 and
tblInspections. ID = NCritVios.InspI D
join tblViolations as CritVios on CritVios.Type = 1 and
tblInspections. ID = CritVios.InspID
where
tblInspections. iestabid = 100
group by
tblInspections. ReceivedByDate, NCritVios.Type
----------------------------------------------------------------------------
----------------------------------------------------------------
and it retuns:
+++++++++++++++ ++++++++
Date: NCrit Crit
2003-05-20 4 4
+++++++++++++++ ++++++++
which is not correct.
Note that without the second count in the select and without the second
join, it works (counts ncrits correctly). It's that second join.
Any ideas?
Thanks.
Thom
Comment