2016-03-14 44 views
0

的回答我的問題PostgreSQL的ARRAY_AGG順序幾乎是在這裏:PostgreSQL array_agg order的窗函數

除此之外,我想ARRAY_AGG在窗函數:

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||... 
            ^

我也許應該從一個子查詢中選擇喜歡的第一個答案的問題引述以上建議。我希望能得到像命令那樣簡單的東西(在這個問題的第二個答案中)。或者,也許我只是懶惰的查詢,應該做group 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/如果你恰巧是好奇,我得到了醫學詞彙表)

回答

0

是的,它看起來像我是頭腦混亂,並不需要窗口功能。這似乎工作:

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 

我不會接受我一會兒回答,因爲它只是迴避了這個問題,而不是真正回答它,有人可能會更有用的東西就此事說。

+0

快速瀏覽 – AlexM

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; 
+0

對不起看起來完全正常,我應該說我試過了。它給了我排序重複的行。我將編輯該問題以顯示我的意思(無法在評論中對其進行格式化)。 – Sigfried