SELECT * FROM ( SELECT TB1.NM, TB1.YYYY,TB1.MES, TB1.DIA,TB1.CASE_DATECREATE,TB1.CASO,TB1.CASE_DESCRIPTION, (SELECT CI_GENERICNAME FROM CMDBCI WHERE IDCMDBCI = TB1.IDUSER) LOGIN_USUARIO, ISNULL(TB1.FIRSTNAME+' '+TB1.MIDDLENAME+' '+TB1.LASTNAME,'') NOMBRE_USUARIO, (SELECT CI_GENERICNAME FROM CMDBCI WHERE IDCMDBCI = TB1.IDHANDLER) ASESOR, ISNULL(TB1.SLANAME,'') SLANAME, TB1.ESTADO, tb1.CASE_COUNTTIME,TB1.MAXTIME,CAST(100 AS FLOAT)/ CAST(TB1.TOTAL AS FLOAT) PORC, CASE WHEN TB1.CASE_COUNTTIME > TB1.MAXTIME THEN 'NO CUMPLE' ELSE 'CUMPLE' END CUMPLE_SLA, TB1.CATEGORYNAME, 'TOTAL CASOS' CASOS FROM ( SELECT DATEPART(MONTH, A.CASE_DATECREATE) NM,DATENAME(MONTH,A.CASE_DATECREATE) MES, DATEPART(DAY,A.CASE_DATECREATE) DIA,DATEPART(YEAR,A.CASE_DATECREATE) YYYY, B.CASESTATUSNAME ESTADO ,A.CASE_DESCRIPTION, A.IDSDCASE CASO, A.CASE_COUNTTIME, C.MT_MAXTIME, D.SLA_MAXTIME , CASE WHEN D.SLA_MAXTIME IS NULL THEN C.MT_MAXTIME ELSE D.SLA_MAXTIME END MAXTIME ,(SELECT COUNT(*) FROM SDCASE WHERE DATEPART(YEAR,SDCASE.CASE_DATECREATE) = DATEPART(YEAR,GETDATE())) TOTAL, A.CASE_DATECREATE, A.IDOWNER, A.IDUSER, A.IDHANDLER, F.FIRSTNAME,F.MIDDLENAME, F.LASTNAME, D.SLANAME, D.IDMDSLA,G.CATEGORYNAME FROM SDCASE A LEFT JOIN SDCASESTATUS B ON A.IDSDCASESTATUS = B.IDSDCASESTATUS LEFT JOIN SDCASEMT C ON A.IDSDCASE = C.IDSDCASE LEFT JOIN MDSLA D ON C.IDSLA = D.IDMDSLA LEFT JOIN CMDBCI E ON A.IDUSER = E.IDCMDBCI LEFT JOIN CMDB_EFPEOPLEINFORMATION F ON E.IDCMDBCI = F.IDCMDBCI LEFT JOIN MDCATEGORYDETAIL G ON A.IDMDCATEGORYDETAIL_FINAL = G.IDMDCATEGORYDETAIL WHERE A.IDSDCASESTATUS <> 6 AND DATEPART(YEAR, A.CASE_DATECREATE) = DATEPART(YEAR,GETDATE()) ) TB1 ) TB2 ORDER BY TB2.NM,TB2.DIA,TB2.CASO