hola a todos estoy creando una consulta para que me regrese por mes cierta información filtrando por mes y año, en el año tengo una variable @YEAR pero si uso la variable no me filtra y cuando quito la variable y la sustituyo por el año con numero si me filtra.
este el mi consulta
estoy usando SQL server
SELECT
SUM(CASE WHEN MONTH(A.Due_Date) = 1 AND YEAR(A.Due_Date) = @YEAR AND A.Work_Request_Type_Key = 54 AND A.Work_Request_Status_Key = 49 THEN 1 ELSE 0 END) as 'MANTENIMIENTO PM CERRADAS',
SUM(CASE WHEN MONTH(A.Due_Date) = 1 AND YEAR(A.Due_Date) = @YEAR AND A.Work_Request_Type_Key = 54 THEN 1 ELSE 0 END) as 'TOTAL MANTENIMIENTO PM',
SUM(CASE WHEN MONTH(A.Due_Date) = 1 AND YEAR(A.Due_Date) = @YEAR AND A.Work_Request_Type_Key = 54 AND A.Work_Request_Status_Key = 49 THEN 1
ELSE 0 END) * 100 /NULLIF(SUM(CASE WHEN MONTH(A.Due_Date) = 1 AND YEAR(A.Due_Date) = @YEAR AND A.Work_Request_Type_Key = 54 THEN 1
ELSE 0 END),0) as 'PORCENTAJE REALIZADO MANTENIMIENTO PM',
SUM(CASE WHEN MONTH(A.Due_Date) = 2 AND YEAR(A.Due_Date) = @YEAR AND A.Work_Request_Type_Key = 54 AND A.Work_Request_Status_Key = 49 THEN 1 ELSE 0 END) as 'MANTENIMIENTO PM CERRADAS',
SUM(CASE WHEN MONTH(A.Due_Date) = 2 AND YEAR(A.Due_Date) = @YEAR AND A.Work_Request_Type_Key = 54 THEN 1 ELSE 0 END) as 'TOTAL MANTENIMIENTO PM',
SUM(CASE WHEN MONTH(A.Due_Date) = 2 AND YEAR(A.Due_Date) = @YEAR AND A.Work_Request_Type_Key = 54 AND A.Work_Request_Status_Key = 49 THEN 1
ELSE 0 END) * 100 /NULLIF(SUM(CASE WHEN MONTH(A.Due_Date) = 2 AND YEAR(A.Due_Date) = @YEAR AND A.Work_Request_Type_Key = 54 THEN 1
ELSE 0 END),0) as 'PORCENTAJE REALIZADO MANTENIMIENTO PM',
SUM(CASE WHEN MONTH(A.Due_Date) = 3 AND YEAR(A.Due_Date) = @YEAR AND A.Work_Request_Type_Key = 54 AND A.Work_Request_Status_Key = 49 THEN 1 ELSE 0 END) as 'MANTENIMIENTO PM CERRADAS',
SUM(CASE WHEN MONTH(A.Due_Date) = 3 AND YEAR(A.Due_Date) = @YEAR AND A.Work_Request_Type_Key = 54 THEN 1 ELSE 0 END) as 'TOTAL MANTENIMIENTO PM',
SUM(CASE WHEN MONTH(A.Due_Date) = 3 AND YEAR(A.Due_Date) = @YEAR AND A.Work_Request_Type_Key = 54 AND A.Work_Request_Status_Key = 49 THEN 1
ELSE 0 END) * 100 /NULLIF(SUM(CASE WHEN MONTH(A.Due_Date) = 3 AND YEAR(A.Due_Date) = @YEAR AND A.Work_Request_Type_Key = 54 THEN 1
ELSE 0 END),0) as 'PORCENTAJE REALIZADO MANTENIMIENTO PM',
FROM Maintenance_v_Work_Request_e A
DECLARE @YEAR int = 2019;
y ejecutas, no funciona?WHERE
en vez de en losCASE
.