I want to count the number of interface down and up and write this code that count them from two tables Nodes and Interface. This code works but I want to know any other way or optimize of this code? what is the best way to count them?
SELECT
q1.NodeName,
q1.Nup as up,
q2.ndown as down
FROM (
SELECT
Nodes.NodeID AS NodeID,
Interfaces.NodeID AS InterfaceID,
Nodes.Caption AS NodeName,
Interfaces.Status as Status,
Count(Nodes.Caption) as Nup
FROM
Nodes INNER JOIN Interfaces ON (Nodes.NodeID = Interfaces.NodeID)
WHERE
(
(
(Interfaces.Status = '2'))
)
GROUP BY Nodes.NodeID, Nodes.Caption, Interfaces.Status, Interfaces.NodeID
) AS q1
INNER JOIN (
SELECT
Interfaces.NodeID AS InterfaceID,
Nodes.Caption AS NodeName,
Interfaces.Status as Status,
Count(Nodes.Caption) as ndown
FROM
Nodes INNER JOIN Interfaces ON (Nodes.NodeID = Interfaces.NodeID)
WHERE
(
(
(Interfaces.Status = '1'))
)
GROUP BY Nodes.NodeID, Nodes.Caption, Interfaces.Status, Interfaces.NodeID
) AS q2
ON (q1.NodeID = q2.InterfaceID)
order by down Desc
You can combine the queries:
Note: in your original query, if a interface is always up, or always down, it would not show up in the resultset since you were using a
INNER JOIN.