I'm going crazy. I've been working on this query for two days on/off now and I can't figure it out. Maybe someone here can see what I'm doing wrong?
I have a temp table #Ward consisting of a varchar, the same datatype as used in my main table.
I want this paired up with Operation table to get the number of operations on each ward for a period of time. The key is I want to show every ward even if it has had no operations.
Let's say the Operation table looks like this:
My current code is:
When I run the query on a period of time I have operations on the first four wards (1-4) I get those numbers and zeros for the rest of the wards (5-10). This is good.
But when I try it on a different period of time (outside the dates I have operations) I do not get wards 1-4 in my return set!!?? I only get wards 5-10 with zeros... I don't get it.
I would be most grateful for any help.
graabein
I have a temp table #Ward consisting of a varchar, the same datatype as used in my main table.
I want this paired up with Operation table to get the number of operations on each ward for a period of time. The key is I want to show every ward even if it has had no operations.
Let's say the Operation table looks like this:
Code:
Ward | Hosp | Dept | Date | InTime | OutTime | Type
My current code is:
Code:
select
w.Ward as "Ward",
count(o.Date) as "Total",
sum(case o.Type when 0 then 1 else 0 end) as "Type0",
sum(case when o.Type > 0 then 1 else 0 end) as "Type1",
from
#Ward w left outer join Operation o on w.Ward = o.Ward
where
(o.Hosp = 'TR' or o.Hosp is null) and
(o.Dept = 'KIR' or o.Dept is null) and
(o.Date >= '2006-10-01' and o.Date < '2006-11-01' or o.Date is null)
group by
w.Ward
order by
w.Ward
When I run the query on a period of time I have operations on the first four wards (1-4) I get those numbers and zeros for the rest of the wards (5-10). This is good.
But when I try it on a different period of time (outside the dates I have operations) I do not get wards 1-4 in my return set!!?? I only get wards 5-10 with zeros... I don't get it.
I would be most grateful for any help.
graabein