查询中位数结果不正确

时间:2019-05-16 17:04:44

标签: postgresql

我需要在数据集中计算两个中位数。由于我使用的是数据可视化软件,因此我需要在数据的所有行上同时拥有两个中值。首先,我需要找到一个基线期的中位数,然后再找到总体中位数(基线期和随访期)。

我现在得到的结果是一对大于任何单个值的中值

SELECT gen_info_reporting_period, gen_info_data_type, (SUM(data_elements_newborn_births_de_2)::NUMERIC + SUM(data_elements_newborn_births_de_4)::NUMERIC + SUM(data_elements_newborn_births_de_5)::NUMERIC) as indicator,
    split_part (qed.nuis_region_district_label,',', 2) as district,
    split_part (qed.nuis_region_district_label,',', 1) as region,
    fl.nuis_facility_id_label as facility_label,
    (SELECT percentile_disc(0.5) WITHIN GROUP (ORDER BY indicator) FROM (SELECT (SUM(data_elements_newborn_births_de_2)::NUMERIC + SUM(data_elements_newborn_births_de_4)::NUMERIC + SUM(data_elements_newborn_births_de_5)::NUMERIC) as indicator FROM qed WHERE gen_info_data_type = '1' GROUP BY gen_info_reporting_period) as poo) as baseline_median,
    (SELECT percentile_disc(0.5) WITHIN GROUP (ORDER BY indicator) FROM (SELECT (SUM(data_elements_newborn_births_de_2)::NUMERIC + SUM(data_elements_newborn_births_de_4)::NUMERIC + SUM(data_elements_newborn_births_de_5)::NUMERIC) as indicator FROM qed GROUP BY gen_info_reporting_period) as foo) as overall_median
    FROM qed
    JOIN facility_profile fp ON qed.nuis_facility_id = fp.nuis_facility_id
    INNER JOIN facility_ids_labels fl ON fl.facility_id = qed.nuis_facility_id
    GROUP BY gen_info_reporting_period,gen_info_data_type, qed.nuis_region_district_label, qed.nuis_facility_id, fl.nuis_facility_id_label

0 个答案:

没有答案