我有一张设施级数据点表,细分为每个设施的基线和后续周期。我需要计算基线期的中位数和每个设施的总体中位数。由于我使用的是数据可视化软件,因此我需要每个设施的所有提交日期都存在这两个中位数(即,即使在包含后续期间数据的行上,我也需要查看基线期间的中位数)。
我有以下查询,但它不返回特定于设施的中位数,而是返回所有设施的中位数。我尝试使用窗口函数,但收到错误OVER is not supported for ordered-set aggregate percentile_disc
。请注意,下面的indicator
本身就是一个聚合,因此我需要将其计算为这样的子查询。
SELECT gen_info_reporting_period, gen_info_data_type, indicator,
split_part (qed.nuis_region_district_label,',', 2) as district,
split_part (qed.nuis_region_district_label,',', 1) as region,
fp.nuis_facility_id_label as facility_label,
(SELECT percentile_disc(0.5) WITHIN GROUP (ORDER BY indicator) FROM (SELECT indicator FROM qed WHERE gen_info_data_type = '1') as poo) as baseline_median,
(SELECT percentile_disc(0.5) WITHIN GROUP (ORDER BY indicator) FROM (SELECT indicator FROM qed) as foo) as overall_median,
ROW_NUMBER() OVER (PARTITION BY fp.nuis_facility_id_label, gen_info_data_type ORDER BY gen_info_reporting_period) as row_num
FROM qed
JOIN facility_profile fp ON qed.nuis_facility_id = fp.nuis_facility_id
GROUP BY gen_info_reporting_period, gen_info_data_type, qed.nuis_region_district_label, district, region, facility_label