I'm currently using this report...
SELECT CONVERT(DateTime,
LTRIM(MONTH(DateTime)) + '/01/' + LTRIM(YEAR(DateTime)),
101) AS SummaryMonth,
Nodes.NodeID AS NodeID,
Nodes.VendorIcon AS Vendor_Icon,
Nodes.Caption AS NodeName,
Nodes.IP_Address AS IP_Address,
AVG(DailyNodeAvailability.Availability) AS AVERAGE_of_Availability
FROM
Nodes INNER JOIN DailyNodeAvailability ON (Nodes.NodeID = DailyNodeAvailability.NodeID)
WHERE
( MONTH(DateTime) = (MONTH(GETDATE()-DAY(GETDATE())))
AND
YEAR(DateTime) = YEAR(GETDATE()-DAY(GETDATE())))
GROUP BY CONVERT(DateTime, LTRIM(MONTH(DateTime)) + '/01/' + LTRIM(YEAR(DateTime)), 101),
Nodes.NodeID, Nodes.VendorIcon, Nodes.Caption, Nodes.IP_Address
HAVING AVG(DailyNodeAvailability.Availability) <= 90
ORDER BY SummaryMonth ASC, 4 ASC
...and it works great! However I really need this to only report daily business hours (8am to 5pm) on weekdays. I've tried tons of variations but the report keeps coming up blank. An example of a variation I made is this...
DECLARE
@LastMonth1 datetime = (SELECT DATEADD(month, DATEDIFF(month, -1, getdate()) - 2, 0)),
@Lastmonth2 datetime = (SELECT DATEADD(ss, -1, DATEADD(month, DATEDIFF(month, 0, getdate()), 0))),
@GetDateMinusMonth datetime;
SET @GetDateMinusMonth = DATEADD(day, -30, GETDATE());
SELECT CONVERT(DateTime,
LTRIM(MONTH(DateTime)) + '/01/' + LTRIM(YEAR(DateTime)),
101) AS SummaryMonth,
Nodes.NodeID AS NodeID,
Nodes.VendorIcon AS Vendor_Icon,
Nodes.Caption AS NodeName,
Nodes.IP_Address AS IP_Address,
AVG(DailyNodeAvailability.Availability) AS AVERAGE_of_Availability
FROM
Nodes INNER JOIN DailyNodeAvailability ON (Nodes.NodeID = DailyNodeAvailability.NodeID)
WHERE
( DateTime BETWEEN @LastMonth1 AND @LastMonth2 )
AND
(DATEPART(weekday, DateTime) >= 2) --Monday
AND (DATEPART(weekday, DateTime) <= 6) --Friday
AND (Convert(Char,DateTime,108) >= '08:00')
AND (Convert(Char,DateTime,108) <= '18:00')
AND
MONTH(DateTime) = (MONTH(@GetDateMinusMonth-DAY(@GetDateMinusMonth)))
AND
YEAR(DateTime) = YEAR(@GetDateMinusMonth-DAY(@GetDateMinusMonth))
GROUP BY CONVERT(DateTime, LTRIM(MONTH(DateTime)) + '/01/' + LTRIM(YEAR(DateTime)), 101),
Nodes.NodeID, Nodes.VendorIcon, Nodes.Caption, Nodes.IP_Address
ORDER BY SummaryMonth ASC, 4 ASC
...which causes the report to become completely blank. What's wrong with my logic? Any help would be greatly appreciated!