This document contains a SQL query that performs the following:
1. Defines a time range using a config table
2. Looks up child power sources associated with a parent power source ID
3. Gets the declared capacity of the child power sources
4. Summarizes the scheduled power for the child power sources over the defined time range and groups it by time
5. Averages the total scheduled power over the time period to produce a final result.
This document contains a SQL query that performs the following:
1. Defines a time range using a config table
2. Looks up child power sources associated with a parent power source ID
3. Gets the declared capacity of the child power sources
4. Summarizes the scheduled power for the child power sources over the defined time range and groups it by time
5. Averages the total scheduled power over the time period to produce a final result.
This document contains a SQL query that performs the following:
1. Defines a time range using a config table
2. Looks up child power sources associated with a parent power source ID
3. Gets the declared capacity of the child power sources
4. Summarizes the scheduled power for the child power sources over the defined time range and groups it by time
5. Averages the total scheduled power over the time period to produce a final result.
' as c1, 'Type of Open Access' as c2, 'LTA' as c3, 'LTA' as c4, 'MTA' as c5, 'MTA' as c6, 'MTA' as c7, 'MTA' as c8, 'MTA' as c9, 'MTA' as c10*/
with config as(
select '2023-05-31T18:30:00Z'::timestamp + '5 hr 30 min'::interval as sdate, '2023-09-15T18:29:59.999Z'::timestamp + '5 hr 30 min'::interval as edate , (1) as p_psid ), child_ps_list AS ( SELECT source_id FROM ems_private.power_source_table WHERE source_id IN ( SELECT CHILD_POWER_SOURCE_ID FROM ems_private.POWER_SOURCE_LEVEL_MAPPING_TABLE WHERE PARENT_POWER_SOURCE_ID IN (1) ) ), tied_up AS ( SELECT source_id, name, approved_capacity FROM ems_private.power_source_table WHERE source_id IN (SELECT source_id FROM child_ps_list AS power_source_id) ), declared_capacity AS ( SELECT source_id, sum((SELECT avg(dc) FROM get_latest_dc(source_id, (SELECT sdate FROM config), (SELECT edate FROM config)))) AS dc_kw FROM tied_up GROUP BY 1 ORDER BY 1 ), dc AS ( SELECT COALESCE(sum(dc_kw) / 1000, 0) FROM declared_capacity ),---SELECT * FROM dc received_schedule as( select log_time, (json_each_text(scheduled_power)).key, (json_each_text(scheduled_power)).value from ems_public.received_schedule where log_time >= (select sdate from config) and log_time < (select edate from config) ), final_data as( select log_time, case when key like '%_gss' then replace(key,'_gss','')::integer else key::integer end as power_source_id, key, case when key like '%_gss' then value::real else value::real end as sch_power from received_schedule where value != '' ), final_data1 as( select log_time, power_source_id, max(case when key like '%_gss' then sch_power * 0.95 else sch_power end) as sch_power from final_data group by 1,2 order by 1 ), sum_of_all_state as( select log_time, (sum(sch_power)*4)as sum from final_data1 where power_source_id in (select source_id from child_ps_list) group by 1 order by 1
),----SELECT * FROM sum_of_all_state
Total_Schedule as (select avg(sum) from sum_of_all_state order by 1)