【发布时间】:2019-08-27 01:59:32
【问题描述】:
我想根据表中其他列的值创建一个新列,其中包含 50%、60% 等值。从下面显示的输出中,我希望有基于“cnt”列中的值的“Desired Results”列。目前我的输入数据如下图所示
我只能从下面的查询中获取记录的数量。但是我无法生成百分比。你能帮帮我吗?
with test as
(
select subject_id,hadm_id,case
when valuenum between 80 and 110 then 1
else 0
end as "within_range"
from labevents where itemid in ('50809','50931','51529') and
hadm_id is not null
) select subject_id,hadm_id,within_range,count(*) as cnt
from test group by subject_id,hadm_id,within_range
我希望输出如下所示
【问题讨论】:
-
这些百分比背后的逻辑是什么
-
如果你看上面的数据,对于 subject_id = 3,有两行,总 cnt 是 22,所以 14/22 是 64,8/22 是 36。其余的类似。对于只有一条记录的科目,则为 100 pc
标签: sql postgresql group-by percentage