Código SQL:
Ver original
DECLARE @FECHAX DATETIME SET @FECHAX=GETDATE() SELECT TOP 1 T0.ARTICULO, T0.DESCRIPCION, T0.Clasificacion6 AS PRESENTACION, T0.Clasificacion2 AS LABORATORIO, /**************************BLOQUE**************************/ ISNULL(( SELECT SUM(T1.CANTIDAD) FROM ALDER_GGG_VtasTotal_UNION T1 WHERE T1.DIA BETWEEN (SELECT DATEADD(DAY, -(SELECT DAY(DATEADD(MONTH, - 13, @FECHAX))-15),DATEADD(MONTH, - 13, @FECHAX))) AND (SELECT DATEADD(DAY, -(SELECT DAY(DATEADD(MONTH, - 12, @FECHAX))-15),DATEADD(MONTH, - 12, @FECHAX))) AND T1.ARTICULO=@ARTICULO AND T1.RUTA='F02' ),0) AS IPRIVADA1, ISNULL(( SELECT SUM(T1.CANTIDAD) FROM ALDER_GGG_VtasTotal_UNION T1 WHERE T1.DIA BETWEEN (SELECT DATEADD(DAY, -(SELECT DAY(DATEADD(MONTH, - 13, @FECHAX))-15),DATEADD(MONTH, - 13, @FECHAX))) AND (SELECT DATEADD(DAY, -(SELECT DAY(DATEADD(MONTH, - 12, @FECHAX))-15),DATEADD(MONTH, - 12, @FECHAX))) AND T1.ARTICULO=@ARTICULO AND T1.RUTA<>'F02'AND T1.RUTA<>'F12' ),0) AS FPRIVADA1, ISNULL(( SELECT SUM(T1.CANTIDAD) FROM ALDER_GGG_VtasTotal_UNION T1 WHERE T1.DIA BETWEEN (SELECT DATEADD(DAY, -(SELECT DAY(DATEADD(MONTH, - 13, @FECHAX))-15),DATEADD(MONTH, - 13, @FECHAX))) AND (SELECT DATEADD(DAY, -(SELECT DAY(DATEADD(MONTH, - 12, @FECHAX))-15),DATEADD(MONTH, - 12, @FECHAX))) AND T1.ARTICULO=@ARTICULO AND T1.RUTA='F12' AND [Cod. Cliente]<>'02094' AND [Cod. Cliente]<>'03165' ),0) AS FPUBLICA1, /**********SUMA DE TOTAL************/ ISNULL(( SELECT SUM(T1.CANTIDAD) FROM ALDER_GGG_VtasTotal_UNION T1 WHERE T1.DIA BETWEEN (SELECT DATEADD(DAY, -(SELECT DAY(DATEADD(MONTH, - 13, @FECHAX))-15),DATEADD(MONTH, - 13, @FECHAX))) AND (SELECT DATEADD(DAY, -(SELECT DAY(DATEADD(MONTH, - 12, @FECHAX))-15),DATEADD(MONTH, - 12, @FECHAX))) AND T1.ARTICULO=@ARTICULO AND T1.RUTA='F02' ),0)+ ISNULL(( SELECT SUM(T1.CANTIDAD) FROM ALDER_GGG_VtasTotal_UNION T1 WHERE T1.DIA BETWEEN (SELECT DATEADD(DAY, -(SELECT DAY(DATEADD(MONTH, - 13, @FECHAX))-15),DATEADD(MONTH, - 13, @FECHAX))) AND (SELECT DATEADD(DAY, -(SELECT DAY(DATEADD(MONTH, - 12, @FECHAX))-15),DATEADD(MONTH, - 12, @FECHAX))) AND T1.ARTICULO=@ARTICULO AND T1.RUTA<>'F02'AND T1.RUTA<>'F12' ),0)+ ISNULL(( SELECT SUM(T1.CANTIDAD) FROM ALDER_GGG_VtasTotal_UNION T1 WHERE T1.DIA BETWEEN (SELECT DATEADD(DAY, -(SELECT DAY(DATEADD(MONTH, - 13, @FECHAX))-15),DATEADD(MONTH, - 13, @FECHAX))) AND (SELECT DATEADD(DAY, -(SELECT DAY(DATEADD(MONTH, - 12, @FECHAX))-15),DATEADD(MONTH, - 12, @FECHAX))) AND T1.ARTICULO=@ARTICULO AND T1.RUTA='F12' AND [Cod. Cliente]<>'02094' AND [Cod. Cliente]<>'03165' ),0) AS TOTAL1, /********************************************************************************/
estoy utilizando subconsultas en donde hago betwen obteniendo la fecha del sistema, cada vez que lo ejecuto tarda 7.456 seg aproximadamente, el problema es que son 1000 articulos = 7456 seg
Alguna sugerencia para optimizarla??