sql first refactor
Some checks failed
CI and deploy / migration-check (push) Successful in 5s
CI and deploy / deploy (push) Failing after 20s

This commit is contained in:
Dusan Vojacek
2026-04-19 20:02:20 +02:00
parent a02e11ee13
commit 93f883f5e0
74 changed files with 6022 additions and 4014 deletions

View File

@@ -0,0 +1,23 @@
-- denní součet |battery_power_w|/60 Wh → poměr k 2× usable (orientační cykly/den)
create or replace view ems.vw_battery_cycle_daily as
select
ti.site_id,
(ti.measured_at at time zone 'Europe/Prague')::date as day_prague,
sum(abs(ti.battery_power_w::numeric) / 60.0) as throughput_wh,
max(ab.usable_wh) as usable_wh,
round(
(sum(abs(ti.battery_power_w::numeric) / 60.0)
/ nullif(max(ab.usable_wh) * 2, 0))::numeric,
4
) as equiv_full_cycles
from ems.telemetry_inverter ti
cross join lateral (
select usable_capacity_wh::numeric as usable_wh
from ems.asset_battery b
where b.site_id = ti.site_id
order by b.id
limit 1
) ab
where ti.battery_power_w is not null
group by ti.site_id, (ti.measured_at at time zone 'Europe/Prague')::date;