fix month yield
This commit is contained in:
67
schema/all-in-one-yield.sql
Normal file
67
schema/all-in-one-yield.sql
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
with
|
||||||
|
first_day_in_year as (
|
||||||
|
select
|
||||||
|
date_trunc('day', min(time)) as day
|
||||||
|
from pv_power_measurement_t
|
||||||
|
where
|
||||||
|
time between date_trunc('year', time) and now()
|
||||||
|
),
|
||||||
|
first_value_in_year as (
|
||||||
|
select
|
||||||
|
time_bucket('1 day', time) as interval,
|
||||||
|
first(exportenergyactive, time) as energy
|
||||||
|
from pv_power_measurement_t
|
||||||
|
where
|
||||||
|
time between (select day from first_day_in_year) and (select day from first_day_in_year) + interval '1 day' and
|
||||||
|
status = 'Ok'
|
||||||
|
group by interval
|
||||||
|
),
|
||||||
|
first_day_in_month as (
|
||||||
|
select
|
||||||
|
date_trunc('day', min(time)) as day
|
||||||
|
from pv_power_measurement_t
|
||||||
|
where
|
||||||
|
time between date_trunc('month', now()) and now()
|
||||||
|
),
|
||||||
|
first_value_in_month as (
|
||||||
|
select
|
||||||
|
time_bucket('1 day', time) as interval,
|
||||||
|
first(exportenergyactive, time) as energy
|
||||||
|
from pv_power_measurement_t
|
||||||
|
where
|
||||||
|
time between (select day from first_day_in_month) and (select day from first_day_in_month) + interval '1 day' and
|
||||||
|
status = 'Ok'
|
||||||
|
group by interval
|
||||||
|
),
|
||||||
|
first_value_in_day as (
|
||||||
|
select
|
||||||
|
time_bucket('1 day', time) as interval,
|
||||||
|
first(exportenergyactive, time) as energy
|
||||||
|
from pv_power_measurement_t
|
||||||
|
where time >= date_trunc('day', now())
|
||||||
|
group by interval
|
||||||
|
),
|
||||||
|
last_value as (
|
||||||
|
select
|
||||||
|
time_bucket('1 day', time) as interval,
|
||||||
|
last(exportenergyactive, time) as energy
|
||||||
|
from pv_power_measurement_t
|
||||||
|
where
|
||||||
|
time between date_trunc('day', now()) and date_trunc('day', now()) + interval '1 day' and
|
||||||
|
status = 'Ok'
|
||||||
|
group by interval
|
||||||
|
)
|
||||||
|
select
|
||||||
|
extract(year from (select day from first_day_in_year))::text as period_value,
|
||||||
|
'Year' as period_name,
|
||||||
|
round(((select energy from last_value) - (select energy from first_value_in_year))::numeric, 2) as yield
|
||||||
|
union
|
||||||
|
select
|
||||||
|
to_char((select day from first_day_in_month), 'Month') as period_value,
|
||||||
|
'Month' as period_name,
|
||||||
|
round(((select energy from last_value) - (select energy from first_value_in_month))::numeric, 2) as yield
|
||||||
|
union
|
||||||
|
select
|
||||||
|
now()::date::text as period_value,
|
||||||
|
'Day' as period_name,
|
||||||
|
round(((select energy from last_value) - (select energy from first_value_in_day))::numeric, 2) as yield;
|
@ -41,7 +41,7 @@ with
|
|||||||
date_trunc('day', min(time)) as day
|
date_trunc('day', min(time)) as day
|
||||||
from pv_power_measurement_t
|
from pv_power_measurement_t
|
||||||
where
|
where
|
||||||
time between date_trunc('month', time) and now()
|
time between date_trunc('month', now()) and now()
|
||||||
),
|
),
|
||||||
first_value as (
|
first_value as (
|
||||||
select
|
select
|
||||||
@ -64,6 +64,9 @@ with
|
|||||||
group by interval
|
group by interval
|
||||||
)
|
)
|
||||||
select
|
select
|
||||||
|
(select day from first_day_in_month) as v1,
|
||||||
|
(select energy from first_value) as v2,
|
||||||
|
(select energy from last_value) as v3,
|
||||||
to_char((select day from first_day_in_month), 'Month') as period_value,
|
to_char((select day from first_day_in_month), 'Month') as period_value,
|
||||||
'Month' as period_name,
|
'Month' as period_name,
|
||||||
(select energy from last_value) - (select energy from first_value) as yield;
|
(select energy from last_value) - (select energy from first_value) as yield;
|
||||||
|
Reference in New Issue
Block a user