# Compare start of month data and end of month data

**URL:** <https://forum.inductiveautomation.com/t/compare-start-of-month-data-and-end-of-month-data/85598>\
**Category:** Ignition\
**Tags:** ignition79, database, vision\
**Created:** [February 12, 2024, 11:14pm UTC](https://forum.inductiveautomation.com/t/compare-start-of-month-data-and-end-of-month-data/85598 "2024-02-12T23:14:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![antonio.sanchez](https://avatars.discourse-cdn.com/v4/letter/a/a587f6/32.png) [@antonio.sanchez](https://forum.inductiveautomation.com/u/antonio.sanchez)\
**Post date:** [February 12, 2024, 11:14pm UTC](https://forum.inductiveautomation.com/t/compare-start-of-month-data-and-end-of-month-data/85598/1 "2024-02-12T23:14:40Z")

</div>

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:

```auto
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;

```

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [February 12, 2024, 11:47pm UTC](https://forum.inductiveautomation.com/t/compare-start-of-month-data-and-end-of-month-data/85598/2 "2024-02-12T23:47:06Z")

</div>

When you say "compare", do you mean you just want two rows returned - one for the first date and one for the second?

If so you can use SQL's UNION to join two separate queries.

> **[SQL UNION Operator](https://www.w3schools.com/sql/sql_union.asp)**
>
> W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.
