我有两个如下所示的表
我正在尝试类似以下的内容
select person_id,
min(value),max(value),count(value),stddev(value)
percentile_cont(0.25) within group (order by value_as_number asc) as "25pc",
percentile_cont(0.75) within group (order by value_as_number asc) as "75pc"
from cdm.measurement
group by person_id
如果是person doesn't have a specific reading, the value should NA as shown in screenshot below
我想做两件事
1)创建与Readings
表中的读数数一样多的列(只有唯一的ID)。 例如,如果“读数”表具有800个阅读ID,则我们将为一个人提供800 * 6 = 4800列。 6 is used here because of min,max,count,stddev,25th percentile,75 percentile
。列的名称将为R_name followed by 25%,75%,min,max etc
。例如:Read_1_25%,Read_1_min
2)如果某人没有任何读数,则其缺少读数的值为NA。例如,“ person_id = 1”仅具有R1读数。因此其余4794(4800-6)列将为NA
我希望我的输出如下所示。 由于图像较宽,请单击图像将其放大。忽略我的屏幕截图中值的正确性。格式是我在寻求您的帮助
答案 0 :(得分:1)
如果我理解正确,则可以使用条件聚合:
select person_id,
count(*) filter (where reading = 'R_1') as cnt_r_1,
min(value) filter (where reading = 'R_1') as min_r_1,
max(value) filter (where reading = 'R_1') as max_r_1,
avg(value) filter (where reading = 'R_1') as avg_r_1,
stdev(value) filter (where reading = 'R_1') as stdev_r_1,
count(*) filter (where reading = 'R_2') as cnt_r_2,
min(value) filter (where reading = 'R_2') as min_r_2,
max(value) filter (where reading = 'R_2') as max_r_2,
avg(value) filter (where reading = 'R_2') as avg_r_2,
stdev(value) filter (where reading = 'R_2') as stdev_r_2,
. . .
from t
group by person_id;