I have the following sql query:
Declare @Month as varchar
Declare @Turf as varchar
Set @Month = '1'
SELECT COUNT(workorderID) AS WO_Total, YEAR(dateCreated) AS RAAG_Year, franchiseID
FROM WRK_ORDER
WHERE (@Month <> NULL) AND (YEAR(dateCreated) = '2010') AND (franchiseID = 'C368610D4B6D0D78862574580049390F')
AND (@Turf IS NULL) OR
(YEAR(dateCreated) = '2010') AND (franchiseID = 'C368610D4B6D0D78862574580049390F')
AND (MONTH(dateCreated) = (SELECT MAX(MONTH(dateCreated)) AS mMonth
FROM WRK_ORDER AS WRK_ORDER_1
WHERE (franchiseID = 'C368610D4B6D0D78862574580049390F')
AND (YEAR(dateCreated) = '2011'))) AND (@Turf IS NULL) OR
(@Month <> NULL) AND (YEAR(dateCreated) = '2010') AND (franchiseID = 'C368610D4B6D0D78862574580049390F') AND (turfID = 'Phoenix')
GROUP BY YEAR(dateCreated), franchiseID
I am trying to account for a few different scenarios within the WHERE clause. But when I run this as is, it times out.
When I remove the last OR scenario, it works fine. Not sure why this is happening??
Declare @Month as varchar
Declare @Turf as varchar
Set @Month = '1'
SELECT COUNT(workorderID) AS WO_Total, YEAR(dateCreated) AS RAAG_Year, franchiseID
FROM WRK_ORDER
WHERE (@Month <> NULL) AND (YEAR(dateCreated) = '2010') AND (franchiseID = 'C368610D4B6D0D78862574580049390F')
AND (@Turf IS NULL) OR
(YEAR(dateCreated) = '2010') AND (franchiseID = 'C368610D4B6D0D78862574580049390F')
AND (MONTH(dateCreated) = (SELECT MAX(MONTH(dateCreated)) AS mMonth
FROM WRK_ORDER AS WRK_ORDER_1
WHERE (franchiseID = 'C368610D4B6D0D78862574580049390F')
AND (YEAR(dateCreated) = '2011'))) AND (@Turf IS NULL) OR
(@Month <> NULL) AND (YEAR(dateCreated) = '2010') AND (franchiseID = 'C368610D4B6D0D78862574580049390F') AND (turfID = 'Phoenix')
GROUP BY YEAR(dateCreated), franchiseID
I am trying to account for a few different scenarios within the WHERE clause. But when I run this as is, it times out.
When I remove the last OR scenario, it works fine. Not sure why this is happening??