【发布时间】:2018-08-25 11:23:58
【问题描述】:
我一直在尝试从事件表中查询(下面我展示了一个带有预期输出的示例)。
此表有超过 3 个事件类型,但我只显示相关的。这些相关的 3 种类型是:“HRa1”(代码 211)、“HRa2”(代码 12712)和“HRy”(代码 220048)。 'HRa' 分为 'HRa1' 和 'HRa2' ,因为它们与将它们插入桌子的人不同,但它们不会同时发生。
我想选择以下:
如果仅出现这 3 种类型中的一种:如果是 'HRa1' 或 'HRa2',则选择其所有 'HRa1'/'HRa2' 值(列 valuenum)在 50 和 110 之间的那些;如果它是“HRy”,则选择所有值(列值)等于“sinus”的那些。见 ID = 1、2、3、4 和 5。可以出现其他类型的事件,没关系,但只出现上述 3 中的一个(见 ID = 3)。
如果同时出现“HRa1”/“HRa2”和“HRy”,则“HRa1”/“HRa2”(valuenum)必须在所有“HRa1”/“HRa2”条目中介于 50 和 110 之间,并且“HRy”(值)条目必须全部等于“sinus”。 ID = 8 显示了不满足此条件的示例。
如果这 3 个都没有出现,当然排除。
我怎样才能做到这一点?
drop table testevents cascade;
create table testevents(
hadm_id int not null,
subject_id int not null,
eventtype int not null,
eventlabel char(30) not null,
value char(360) not null,
valuenum int
);
insert into testevents(hadm_id, subject_id, eventtype, eventlabel, value, valuenum)
values
(1, 1, 211, 'HRa1', '59', 59),
(1, 1, 211, 'HRa1', '89', 59),
(1, 1, 211, 'HRa1', '54', 59),
(1, 1, 211, 'HRa1', '99', 59),
(1, 1, 211, 'HRa1', '69', 59),
(1, 1, 211, 'HRa1', '104', 59),
(2, 2, 211, 'HRa1', '59', 59),
(2, 2, 211, 'HRa1', '89', 89),
(2, 2, 211, 'HRa1', '54', 54),
(2, 2, 211, 'HRa1', '140', 140), -- not allowed
(2, 2, 211, 'HRa1', '69', 69),
(2, 2, 211, 'HRa1', '104', 104),
(3, 3, 12712, 'HRa2', '69', 69),
(3, 3, 12712, 'HRa2', '89', 89),
(3, 3, 12712, 'HRa2', '94', 94),
(3, 3, 12712, 'HRa2', '59', 59),
(3, 3, 12712, 'HRa2', '69', 69),
(3, 3, 12712, 'HRa2', '84', 84),
(3, 3, 353, 'RXa', 'OK', null),
(4, 4, 220048, 'HRy', 'sinus', null),
(4, 4, 220048, 'HRy', 'sinus', null),
(4, 4, 220048, 'HRy', 'sinus', null),
(4, 4, 220048, 'HRy', 'sinus', null),
(4, 4, 220048, 'HRy', 'sinus', null),
(4, 4, 220048, 'HRy', 'sinus', null),
(5, 5, 220048, 'HRy', 'sinus', null),
(5, 5, 220048, 'HRy', 'sinus', null),
(5, 5, 220048, 'HRy', 'sinus', null),
(5, 5, 220048, 'HRy', 'AF', null), -- not allowed
(5, 5, 220048, 'HRy', 'sinus', null),
(5, 5, 220048, 'HRy', 'sinus', null),
(6, 6, 220048, 'HRy', 'sinus', null),
(6, 6, 220048, 'HRy', 'sinus', null),
(6, 6, 211, 'HRa2', '94', 95),
(6, 6, 211, 'HRa2', '150', 150), -- not allowed
(6, 6, 220048, 'HRy', 'sinus', null),
(6, 6, 220048, 'HRy', 'sinus', null),
(7, 7, 220048, 'HRy', 'sinus', null),
(7, 7, 220048, 'HRy', 'sinus', null),
(7, 7, 211, 'HRa2', '94', 94),
(7, 7, 211, 'HRa2', '87', 87),
(7, 7, 220048, 'HRy', 'sinus', null),
(7, 7, 220048, 'HRy', 'sinus', null),
(7, 7, 4053, 'BP', '87', 87),
(7, 7, 503, 'RR', '72', 72),
(8, 8, 220048, 'HRy', 'sinus', null),
(8, 8, 211, 'HRa2', '94', 94),
(8, 8, 211, 'HRa2', '87', 87),
(8, 8, 220048, 'HRy', 'AF', null), -- not allowed
(8, 8, 220048, 'HRy', 'sinus', null),
(8, 8, 4053, 'BP', '87', 87),
(8, 8, 503, 'RR', '72', 72),
(9, 9, 220048, 'HRy', 'AF', null), -- not allowed
(9, 9, 211, 'HRa2', '134', 134), -- not allowed
(9, 9, 211, 'HRa2', '187', 187), -- not allowed
(9, 9, 220048, 'HRy', 'AF', null), -- not allowed
(9, 9, 220048, 'HRy', 'AF', null) -- not allowed
;
output:
hadm_id, subject_id
1 1
3 3
4 4
7 7
我正在尝试以下查询,但 ID = 8 失败:
select subject_id, hadm_id
from testevents
where eventlabel in ('HRa1', 'HRa2', 'HRy')
group by subject_id, hadm_id
having ((min(valuenum) >= 50 and max(valuenum) <= 110) or
(min(value) = 'sinus' and max(value) = 'sinus'))
order by subject_id;
有人可以帮助我吗? 非常感谢!
【问题讨论】:
标签: sql postgresql