HI
I have a currently query for compares a previous day against a current day, but now I need to compare the start day of the month against the last day of the month.
this my currently query:
DECLARE @FechaInicial DATE = '2024-01-08';
DECLARE @FechaFinal DATE = '2024-02-08';
SELECT dm.Fecha,
SUM(CASE WHEN dm.Linea = 'ELFO_006_Oficinas' THEN dm.Dato - ISNULL( dp.Dato, 0) ELSE 0 END) AS ELFO_006_Oficinas_Diferencia,
SUM(CASE WHEN dm.Linea = 'ELFO_007_Rim' THEN dm.Dato - ISNULL( dp.Dato, 0) ELSE 0 END) AS ELFO_007_Rim_Diferencia,
SUM(CASE WHEN dm.Linea = 'ELFO_008_ED' THEN dm.Dato - ISNULL( dp.Dato, 0) ELSE 0 END) AS ELFO_008_ED_Diferencia,
SUM(CASE WHEN dm.Linea = 'ELFO_009_FBW' THEN dm.Dato - ISNULL( dp.Dato, 0) ELSE 0 END) AS ELFO_009_FBW_Diferencia,
SUM(CASE WHEN dm.Linea = 'ELFO_010_Disco' THEN dm.Dato - ISNULL( dp.Dato, 0) ELSE 0 END) AS ELFO_010_Disco_Diferencia
FROM Dato_Medidor dm
LEFT JOIN Dato_Medidor dp ON DATEADD( YY, -1, dm.Fecha) = dp.Fecha
AND dm.Linea = dp.Linea
AND dp.Fecha BETWEEN DATEADD( YY, -1, @FechaInicial) AND DATEADD( YY, -1, @FechaFinal)
AND DATEPART(HOUR, dp.Fecha) = 7 AND DATEPART(MINUTE, dp.Fecha) = 0
WHERE dm.Fecha BETWEEN @FechaInicial AND @FechaFinal AND DATEPART(HOUR, dm.Fecha) = 7 AND DATEPART(MINUTE, dm.Fecha) = 0
GROUP BY dm.Fecha
ORDER BY dm.Fecha;