If you use left outer join
, you need to use it for all the joins in the query (typically). Otherwise, you might be "undoing" the earlier join.
However, I don't think that is your only issue. The expression count(d.name)
has to return at least 1 (unless name
can be NULL
).
SELECT d.type, count(h471.id)
FROM device d INNER JOIN
(SELECT type, name
FROM device
) b
ON d.name = b.name
WHERE d.changedon < (((SYSDATE - DATE '1970-01-01') * 24 * 60 * 60) - (90 * 24 * 60 * 60)) and
d.status != 6 AND d.status != 8 AND d.status != 13
GROUP BY d.type;
When using outer joins, you also have to pay attention to the where
clause. In this case, though, you are only refering to the device
table. If the switch to left outer join
doesn't fix the problem, then the issue is in the where
clause -- it is filtering out the additional devices. If that is the case, then move the where
clause to a conditional aggregation:
SELECT d.type,
sum(case when d.changedon < (((SYSDATE - DATE '1970-01-01') * 24 * 60 * 60) - (90 * 24 * 60 * 60)) and
d.status != 6 AND d.status != 8 AND d.status != 13
then 1 else 0
end)
FROM device d LEFT OUTER JOIN
(SELECT type, name
FROM device
) b
ON d.name = b.name
GROUP BY d.type;
You might be able to move some of the conditions back to the where
clause for performance reasons.
Also, I don't get the join to table b
. It would just be multiplying the number of rows (if multiple rows in device
have the same name) or doing nothing.