我的目标是计算等待在3家医院接受服务5个周期的患者的第90个百分位等待时间的累积值。
每个时期对应一个特定的时间表(例如,时期1(P01)表示患者在2018年4月1日至2018年5月3日之间接受服务,时期2(P02,2018年5月5日至5月之间服务) ,2018年3月31日)。
我的原始查询非常复杂,这是它的简化版本。 我的表格由3列组成:
CREATE TABLE Mytable1(
period varchar2(10),
hospital varchar2(100),
wait_days int
);
然后插入等待时间(以天为单位)的值,因为有5个周期,每个医院要插入五个。
INSERT ALL
INTO Mytable1(period,hospital,wait_days) VALUES ('P01', 'NANAIMO HOSPITAL',182)
INTO Mytable1(period,hospital,wait_days) VALUES ('P02','NANAIMO HOSPITAL',187)
INTO Mytable1(period,hospital,wait_days) VALUES ('P03','NANAIMO HOSPITAL',188)
INTO Mytable1(period,hospital,wait_days) VALUES ('P04','NANAIMO HOSPITAL',182)
INTO Mytable1(period,hospital,wait_days) VALUES ('P05','NANAIMO HOSPITAL',190)
INTO Mytable1(period,hospital,wait_days) VALUES ('P01','VICTORIA HOSPITAL',230)
INTO Mytable1(period,hospital,wait_days) VALUES ('P02','VICTORIA HOSPITAL',241)
INTO Mytable1(period,hospital,wait_days) VALUES ('P03','VICTORIA HOSPITAL',245)
INTO Mytable1(period,hospital,wait_days) VALUES ('P04','VICTORIA HOSPITAL',237)
INTO Mytable1(period,hospital,wait_days) VALUES ('P05','VICTORIA HOSPITAL',240)
INTO Mytable1(period,hospital,wait_days) VALUES ('P01','ARBUTUS HOSPITAL',156)
INTO Mytable1(period,hospital,wait_days) VALUES ('P02','ARBUTUS HOSPITAL',163)
INTO Mytable1(period,hospital,wait_days) VALUES ('P03','ARBUTUS HOSPITAL',144)
INTO Mytable1(period,hospital,wait_days) VALUES ('P04','ARBUTUS HOSPITAL',158)
INTO Mytable1(period,hospital,wait_days) VALUES ('P05','ARBUTUS HOSPITAL',165)
SELECT 1 FROM DUAL;
现在,我使用不带Group By子查询的解析版本来计算第90个百分位等待时间:
SELECT period, hospital,
PERCENTILE_CONT(0.9) WITHIN GROUP (ORDER BY wait_days) OVER (PARTITION BY hospital) as percentile
FROM Mytable1
order by period, hospital;
这是我的结果:
PERIOD HOSPITAL PERCENTILE
P01 ARBUTUS HOSPITAL 164.2
P01 NANAIMO HOSPITAL 189.2
P01 VICTORIA HOSPITAL 243.4
P02 ARBUTUS HOSPITAL 164.2
P02 NANAIMO HOSPITAL 189.2
P02 VICTORIA HOSPITAL 243.4
P03 ARBUTUS HOSPITAL 164.2
P03 NANAIMO HOSPITAL 189.2
P03 VICTORIA HOSPITAL 243.4
P04 ARBUTUS HOSPITAL 164.2
P04 NANAIMO HOSPITAL 189.2
P04 VICTORIA HOSPITAL 243.4
P05 ARBUTUS HOSPITAL 164.2
P05 NANAIMO HOSPITAL 189.2
P05 VICTORIA HOSPITAL 243.4
每个时期我得到相同的90%。我不知道如何获得每个后续周期的第90个百分位数的累计值。基本上,我需要基于前一时期的结果来计算第90个百分位数,并期望第90个百分位数的值略有不同(以下是我希望获得的示例):
P01 ARBUTUS HOSPITAL 164.2
P02 (=P01+P02) ARBUTUS HOSPITAL 165
P03(=P01+P02+P03) ARBUTUS HOSPITAL 162
P04 (=P01+P02+P03+P04) ARBUTUS HOSPITAL 163
P05(=P01+P02+P03+P04+P05) ARBUTUS HOSPITAL 164
能请你帮忙吗?谢谢!
P.S。我之前曾问过一个关于第90个百分位数计算的问题,但这与另一个问题有关。
答案 0 :(得分:0)
您应将句点添加到分区中,然后使用sum sum。您的查询将如下所示:
SELECT distinct hospital, period,
PERCENTILE_CONT(0.9) WITHIN GROUP (order by cumDays) OVER (PARTITION BY hospital, period) as percentile
from(
select t.hospital, t.period, t.wait_days, c.period cumPeriod, c.wait_days cumDays from mytable1 t
left outer join mytable1 c on t.hospital = c.hospital and t.period >= c.period
) x
p.s。我没有访问Oracle数据库的权限,所以我尝试使用MSSQL DB并成功了。
编辑:在Natalia的第一条评论之后,对答案进行了编辑以返回正确的结果。如果修改后的查询现在返回正确的结果,请将此问题标记为已回答。