Try this for speed. Make sure the needed columns are indexed.SELECT d.Line, SUM(CASE WHEN d.Hour = 0 THEN 1 ELSE 0 END) AS '00', SUM(CASE WHEN d.Hour = 1 THEN 1 ELSE 0 END) AS '01', SUM(CASE WHEN d.Hour = 2 THEN 1 ELSE 0 END) AS '02', SUM(CASE WHEN d.Hour = 3 THEN 1 ELSE 0 END) AS '03', SUM(CASE WHEN d.Hour = 4 THEN 1 ELSE 0 END) AS '04', SUM(CASE WHEN d.Hour = 5 THEN 1 ELSE 0 END) AS '05', SUM(CASE WHEN d.Hour = 6 THEN 1 ELSE 0 END) AS '06', SUM(CASE WHEN d.Hour = 7 THEN 1 ELSE 0 END) AS '07', SUM(CASE WHEN d.Hour = 8 THEN 1 ELSE 0 END) AS '08', SUM(CASE WHEN d.Hour = 9 THEN 1 ELSE 0 END) AS '09', SUM(CASE WHEN d.Hour = 10 THEN 1 ELSE 0 END) AS '10', SUM(CASE WHEN d.Hour = 11 THEN 1 ELSE 0 END) AS '11', SUM(CASE WHEN d.Hour = 12 THEN 1 ELSE 0 END) AS '12', SUM(CASE WHEN d.Hour = 13 THEN 1 ELSE 0 END) AS '13', SUM(CASE WHEN d.Hour = 14 THEN 1 ELSE 0 END) AS '14', SUM(CASE WHEN d.Hour = 15 THEN 1 ELSE 0 END) AS '15', SUM(CASE WHEN d.Hour = 16 THEN 1 ELSE 0 END) AS '16', SUM(CASE WHEN d.Hour = 17 THEN 1 ELSE 0 END) AS '17', SUM(CASE WHEN d.Hour = 18 THEN 1 ELSE 0 END) AS '18', SUM(CASE WHEN d.Hour = 19 THEN 1 ELSE 0 END) AS '19', SUM(CASE WHEN d.Hour = 20 THEN 1 ELSE 0 END) AS '20', SUM(CASE WHEN d.Hour = 21 THEN 1 ELSE 0 END) AS '21', SUM(CASE WHEN d.Hour = 22 THEN 1 ELSE 0 END) AS '22', SUM(CASE WHEN d.Hour = 23 THEN 1 ELSE 0 END) AS '23'FROM ( SELECT 'Line1' AS 'Line', DATEPART(hour, EnterTime) AS 'Hour' FROM ffhistory WHERE StationID = 49 AND UnitStateID = 305 AND EnterTime >= DATEADD(day, DATEDIFF(day, 0, @UserSelectedDateTimeValue), 0) AND EnterTime < DATEADD(day, DATEDIFF(day, 0, @UserSelectedDateTimeValue), 1) UNION ALL SELECT 'Line2', DATEPART(hour, EnterTime) FROM ffhistory WHERE StationID = <x2> AND UnitStateID = <y2> AND EnterTime >= DATEADD(day, DATEDIFF(day, 0, @UserSelectedDateTimeValue), 0) AND EnterTime < DATEADD(day, DATEDIFF(day, 0, @UserSelectedDateTimeValue), 1) UNION ALL SELECT 'Line3', DATEPART(hour, EnterTime) FROM ffhistory WHERE StationID = <x3> AND UnitStateID = <y3> AND EnterTime >= DATEADD(day, DATEDIFF(day, 0, @UserSelectedDateTimeValue), 0) AND EnterTime < DATEADD(day, DATEDIFF(day, 0, @UserSelectedDateTimeValue), 1) UNION ALL SELECT 'Line4', DATEPART(hour, EnterTime) FROM ffhistory WHERE StationID = <x4> AND UnitStateID = <y4> AND EnterTime >= DATEADD(day, DATEDIFF(day, 0, @UserSelectedDateTimeValue), 0) AND EnterTime < DATEADD(day, DATEDIFF(day, 0, @UserSelectedDateTimeValue), 1) UNION ALL SELECT 'Line5', DATEPART(hour, EnterTime) FROM ffhistory WHERE StationID = <x5> AND UnitStateID = <y5> AND EnterTime >= DATEADD(day, DATEDIFF(day, 0, @UserSelectedDateTimeValue), 0) AND EnterTime < DATEADD(day, DATEDIFF(day, 0, @UserSelectedDateTimeValue), 1) UNION ALL SELECT 'Line6', DATEPART(hour, EnterTime) FROM ffhistory WHERE StationID = <x6> AND UnitStateID = <y6> AND EnterTime >= DATEADD(day, DATEDIFF(day, 0, @UserSelectedDateTimeValue), 0) AND EnterTime < DATEADD(day, DATEDIFF(day, 0, @UserSelectedDateTimeValue), 1) UNION ALL SELECT 'Line7', DATEPART(hour, EnterTime) FROM ffhistory WHERE StationID = <x7> AND UnitStateID = <y7> AND EnterTime >= DATEADD(day, DATEDIFF(day, 0, @UserSelectedDateTimeValue), 0) AND EnterTime < DATEADD(day, DATEDIFF(day, 0, @UserSelectedDateTimeValue), 1) ) dGROUP BY d.LineORDER BY d.Line
Peter LarssonHelsingborg, Sweden