我在postgresql中有下表。对于一个试用id的每个id,我想保留那些name\u split=digest\u part\u匹配的行。如果对于试用id的id,name\u split=digest\u part\u matched条件不满足,我希望保留name\u split=normalized\u name所在的行(如果存在)。
id trial_id intervention_name name_split digest_part_matched normalized_name code
98262908855 NCT02582996 acetaminophen acetaminophen acetaminophen acetaminophen A
98262908855 NCT02582996 acetaminophen acetaminophen (null) acetaminophen B
98262908901 NCT02582996 acetaminophen+caffeine+dihydroergotamine+metoclopramide. acetaminophen dihydroergotamine acetaminophen+caffeine+dihydroergotamine+metoclopramide. C
98262908901 NCT02582996 acetaminophen+caffeine+dihydroergotamine+metoclopramide. acetaminophen acetaminophen acetaminophen D
98262908901 NCT02582996 acetaminophen+caffeine+dihydroergotamine+metoclopramide. caffeine caffeine acetaminophen+caffeine+dihydroergotamine+metoclopramide. E1
98262908901 NCT02582996 acetaminophen+caffeine+dihydroergotamine+metoclopramide. caffeine caffeine acetaminophen+caffeine+dihydroergotamine+metoclopramide. E2
98262908901 NCT02582996 acetaminophen+caffeine+dihydroergotamine+metoclopramide. dihydroergotamine dihydroergotamine acetaminophen+caffeine+dihydroergotamine+metoclopramide. F
98262908902 NCT02582996 metoclopramide.+dihydroergotamine dihydroergotamine metoclopramide. dihydroergotamine F
所需输出为:
id trial_id intervention_name name_split digest_part_matched normalized_name
98262908855 NCT02582996 acetaminophen acetaminophen acetaminophen acetaminophen A
98262908901 NCT02582996 acetaminophen+caffeine+dihydroergotamine+metoclopramide. acetaminophen acetaminophen acetaminophen D
98262908901 NCT02582996 acetaminophen+caffeine+dihydroergotamine+metoclopramide. caffeine caffeine acetaminophen+caffeine+dihydroergotamine+metoclopramide. E1
98262908901 NCT02582996 acetaminophen+caffeine+dihydroergotamine+metoclopramide. caffeine caffeine acetaminophen+caffeine+dihydroergotamine+metoclopramide. E2
98262908901 NCT02582996 acetaminophen+caffeine+dihydroergotamine+metoclopramide. dihydroergotamine dihydroergotamine acetaminophen+caffeine+dihydroergotamine+metoclopramide. F
98262908902 NCT02582996 metoclopramide.+dihydroergotamine dihydroergotamine metoclopramide. dihydroergotamine F
我正在尝试以下查询。
select distinct on (id, trial_id) * from tbl
where name_split = digest_part_matched
1条答案
按热度按时间zdwk9cvp1#
distinct on
我能做到。我想你想要:中的第三个表达式
order by
子句优先于name_split
上的匹配项digest_part_matched
.您没有指定如何处理
(id, trial_id)
两列都不匹配的元组name_split
. 上面的查询只会消除它们。如果您有另一个条件来决定应保留哪一行,则可以很容易地调整查询以在每一行组中保留一行: