0

我的问题的答案几乎就在这里: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/如果你对我从哪里得到医学词汇表感到好奇)

4

2 回答 2

1

是的,看起来我很糊涂,不需要窗口功能。这似乎有效:

 select  c.concept_name, 
         array_agg(c2.vocabulary_id||':'||c2.concept_name 
                   order by c2.vocabulary_id, c2.concept_name), 
         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'
 group by c.concept_name, ca.min_levels_of_separation
 order by min_levels_of_separation

我暂时不会接受我的回答,因为它只是回避问题而不是实际回答问题,并且有人可能会对此事有更有用的说法。

于 2016-03-14T10:32:54.403 回答
0

像这样 :

select distinct c.concept_name, 
    array_agg(c2.vocabulary_id||':'||c2.concept_name ) over (partition by ca.min_levels_of_separation  order by c2.vocabulary_id, c2.concept_name), 
    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;
于 2016-03-14T11:42:01.320 回答