This is the table with the date parameter wich filter with the date input
The data is very latge, the query
WITH MAXKW AS (
SELECT
TS.CODIGO, TS.ORDEN_DNF,
MAX(TM.KWGEN) AS MAX_TOTAL_HIST
FROM T_MEDIDAS_TE TM
JOIN T_Sede TS ON TM.id_sede = TS.id_sede
WHERE TM.timestamp BETWEEN ADD_MONTHS(TRUNC(TO_DATE(:startdate, 'YYYY-MM-DD HH24:MI:SS')), -12) AND TRUNC(TO_DATE(:startdate, 'YYYY-MM-DD HH24:MI:SS')) + INTERVAL '1' YEAR - INTERVAL '1' SECOND
AND TS.ORDEN_DNF IS NOT NULL
GROUP BY TS.Codigo, TS.ORDEN_DNF
),
HISTMAXKW AS (
SELECT
TS.CODIGO, TS.ORDEN_DNF,
MAX(TM.KWGEN) AS HISTMAX
FROM T_MEDIDAS_TE TM
JOIN T_Sede TS ON TM.id_sede = TS.id_sede
WHERE TS.ORDEN_DNF IS NOT NULL
GROUP BY TS.Codigo, TS.ORDEN_DNF
),
AVGKW AS (
SELECT
TS.CODIGO, TS.ORDEN_DNF,
TS.COD_DNF,
TS.KWCON AS KWCON,
TS.KWLIM AS KWLIM,
AVG(TM.KWGEN) AS AVG_KW_GEN,
MAX(TM.KWGEN) AS MAX_KW_PERIOD,
AVG(TM.KWCLT) AS AVG_KW_CLT,
MAX(TM.KWCLT) AS MAX_KW_CLT,
AVG(TM.KWIT) AS AVG_KW_IT,
MAX(TM.KWIT) AS MAX_KW_IT,
TS.DNF_BAT AS DNF,
TS.DNF_BAT_10 AS DNF10,
TS.PRVDNF AS PRVDNF,
MAX(TM.PENEJE) KEEP (DENSE_RANK FIRST ORDER BY TM.TIMESTAMP DESC) AS "PENEJE",
0 AS RAMPA
FROM T_MEDIDAS_TE TM
JOIN T_Sede TS ON TM.id_sede = TS.id_sede
WHERE TM.timestamp >= TO_DATE(:startdate, 'YYYY-MM-DD HH24:MI:SS')
AND TM.timestamp < TO_DATE(:startdate, 'YYYY-MM-DD HH24:MI:SS') + INTERVAL '1' YEAR
AND TS.ORDEN_DNF IS NOT NULL
GROUP BY
TO_CHAR(TM.timestamp, 'YYYY'),
TO_CHAR(TM.timestamp, 'IW'),
TS.Codigo,
TS.ORDEN_DNF,
TS.KWCON,
TS.KWLIM,
TS.COD_DNF,
TS.DNF_BAT,
TS.DNF_BAT_10,
TS.PRVDNF
)
SELECT
STM.CODIGO AS "CODE",
STM.COD_DNF AS "DNF code",
STM.KWCON AS "kW Cont.",
STM.KWLIM AS "kW Lim.",
STM.AVG_KW_GEN AS "kW Used",
....