Overall Sensor Count
SELECT *
--AVG(xxx)
FROM (
SELECT T98.ItemName, T98.GroupNo, T98.UnitNo, T98.TotalCount, T99.CommCount, 100.0*(CAST(T99.CommCount AS float)/CAST(T98.TotalCount AS float)) [xxx] FROM
(
SELECT T0.ItemName ,T0.GroupNo, T0.UnitNo,COUNT(*) [TotalCount]
FROM
lbtSiteItemsMaster T0
INNER JOIN
lbtSiteMas T1
ON T0.McuId = T1.McuId
WHERE T0.qStatus = 1 AND T0.i_Status = 1
AND T1.qStatus = 1 AND T1.s_Status = 1 AND BankCode = 25
GROUP BY T0.ItemName, T0.GroupNo, T0.UnitNo
) T98
LEFT JOIN
(
SELECT T0.ItemName ,T2.GroupNo, T2.UnitNo, COUNT(*) [CommCount]
FROM
lbtSiteItemsMaster T0
INNER JOIN
lbtSiteMas T1
ON T0.McuId = T1.McuId
INNER JOIN
lbtAttendance T2
ON T0.McuId = T2.McuId AND T0.GroupNo = T2.GroupNo AND T0.UnitNo = T2.UnitNo
WHERE T0.qStatus = 1 AND T0.i_Status = 1
AND T1.qStatus = 1 AND T1.s_Status = 1 AND BankCode = 25
AND EventCode = '20'
GROUP BY T0.ItemName, T2.GroupNo, T2.UnitNo
HAVING DATEDIFF(HH, MAX(AtTime), GETDATE()) <= 2
) T99
ON T98.ItemName = T99.ItemName
WHERE T98.GroupNo NOT IN ('05', '00')
) a