From a9f801e28c3458374c77e7e067b4a624a8f9b976 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pol=20Mons=C3=B3=20Purt=C3=AD?= Date: Fri, 22 Dec 2023 12:17:07 +0100 Subject: [PATCH] fix: inverter monthly agg (et/somenergia-jardiner!60) * add forgotten with in CTE * add max/min signal_value for inverter energy to debug it * split dset_energy_inverter__agg for_om into two models, hourly and monthly * add materialized_at column to be able to debug incremental rows * delete outdated info in afegir planta * fix: wrong capitalization on model --- ...et_energy_inverter__agg_monthly_for_om.sql | 35 +---------------- ...set_energy_inverter__agg_hourly_for_om.sql | 39 +++++++++++++++++++ 2 files changed, 41 insertions(+), 33 deletions(-) create mode 100644 dbt_jardiner/models/jardiner/marts/int_dset_energy_inverter__agg_hourly_for_om.sql diff --git a/dbt_jardiner/models/jardiner/marts/dm_dset_energy_inverter__agg_monthly_for_om.sql b/dbt_jardiner/models/jardiner/marts/dm_dset_energy_inverter__agg_monthly_for_om.sql index ff6955a0..eb66e359 100644 --- a/dbt_jardiner/models/jardiner/marts/dm_dset_energy_inverter__agg_monthly_for_om.sql +++ b/dbt_jardiner/models/jardiner/marts/dm_dset_energy_inverter__agg_monthly_for_om.sql @@ -1,43 +1,12 @@ {{ config(materialized='view') }} -with inverters_energy as ( - select - date_trunc('hour', ts) as start_hour, - plant_name, - device_name, - metric_name, - signal_unit, - signal_value - from {{ ref('int_dset_responses__values_incremental') }} - where device_type in ('inverter') and metric_name = 'energia_activa_exportada' -), production_hourly as ( - select - start_hour, - plant_name, - device_name, - signal_unit, - (extract(hour from start_hour) > 3)::integer * (max(signal_value) - min(signal_value)) as inverter_energy - from inverters_energy - group by start_hour, plant_name, device_name, metric_name, signal_unit -), signal_unit_standardization as ( - select - start_hour, - plant_name, - device_name, - signal_unit, - case - when signal_unit = 'MWh' then round(inverter_energy,3) - when signal_unit = 'kWh' then round(inverter_energy/1000,3) - else NULL - end as inverter_energy_mwh - from production_hourly -), production_monthly as ( +with production_monthly as ( select date_trunc('month', start_hour, 'Europe/Madrid') as month_date, plant_name, device_name, sum(inverter_energy_mwh) as inverter_energy_mwh - from signal_unit_standardization + from {{ ref("int_dset_energy_inverter__agg_hourly_for_om") }} group by date_trunc('month', start_hour, 'Europe/Madrid'), device_name, plant_name ), aliased as ( diff --git a/dbt_jardiner/models/jardiner/marts/int_dset_energy_inverter__agg_hourly_for_om.sql b/dbt_jardiner/models/jardiner/marts/int_dset_energy_inverter__agg_hourly_for_om.sql new file mode 100644 index 00000000..6f0353cc --- /dev/null +++ b/dbt_jardiner/models/jardiner/marts/int_dset_energy_inverter__agg_hourly_for_om.sql @@ -0,0 +1,39 @@ +{{ config(materialized='view') }} + +with inverters_energy as ( + select + date_trunc('hour', ts) as start_hour, + plant_name, + device_name, + metric_name, + signal_unit, + signal_value + from {{ ref('int_dset_responses__values_incremental') }} + where device_type in ('inverter') and metric_name = 'energia_activa_exportada' +), production_hourly as ( + select + start_hour, + plant_name, + device_name, + signal_unit, + max(signal_value) as max_abs_inverter_energy, + min(signal_value) as min_abs_inverter_energy, + (extract(hour from start_hour) > 3)::integer * (max(signal_value) - min(signal_value)) as inverter_energy + from inverters_energy + group by start_hour, plant_name, device_name, metric_name, signal_unit +), signal_unit_standardization as ( + select + start_hour, + plant_name, + device_name, + case + when signal_unit = 'MWh' then round(inverter_energy,3) + when signal_unit = 'kWh' then round(inverter_energy/1000,3) + else NULL + end as inverter_energy_mwh, + max_abs_inverter_energy, + min_abs_inverter_energy + from production_hourly +) +select * from signal_unit_standardization +