我的问题的答案几乎就在这里:PostgreSQL array_agg order
除了我想对窗口函数进行数组聚合:
select distinct c.concept_name,
array_agg(c2.vocabulary_id||':'||c2.concept_name
order by c2.vocabulary_id, c2.concept_name)
over (partition by ca.min_levels_of_separation),
ca.min_levels_of_separation
from concept c
join concept_ancestor ca on c.concept_id = ca.descendant_concept_id
and max_levels_of_separation > 0
join concept c2 on ca.ancestor_concept_id = c2.concept_id
where
c.concept_code = '44054006'
order by min_levels_of_separation;
所以,也许这将在未来的某个版本中起作用,但我收到了这个错误
ERROR: aggregate ORDER BY is not implemented for window functions
LINE 2: select distinct c.concept_name, array_agg(c2.vocabulary_id||...
^
我可能应该从子查询中进行选择,就像上面引用的问题的第一个答案所暗示的那样。我希望像 order by 一样简单(在该问题的第二个答案中)。或者,也许我只是对查询很懒惰,应该做 agroup by
而不是select distinct
.
我确实尝试在窗口函数 ( over (partition by ca.min_levels_of_separation order by c2.vocabulary_id, c2.concept_name)
) 中按顺序排列,但这样我得到了这些重复的行:
"Type 2 diabetes mellitus";"{"MedDRA:Diabetes mellitus"}";1
"Type 2 diabetes mellitus";"{"MedDRA:Diabetes mellitus","MedDRA:Diabetes mellitus (incl subtypes)"}";1
"Type 2 diabetes mellitus";"{"MedDRA:Diabetes mellitus","MedDRA:Diabetes mellitus (incl subtypes)","SNOMED:Diabetes mellitus"}";1
(顺便说一句:http: //www.ohdsi.org/如果你对我从哪里得到医学词汇表感到好奇)