2016-12-17 19 views
1

我想從數組列中選擇某些元素。我知道你可以按位置來做,但我想過濾內容。這是我的數據:postgres表達式來選擇數組中的元素

 table_name  |  column_names 
---------------------+--------------------------------------------------------------- 
attribute_definition | {attribute_type_concept_id} 
cohort_definition | {definition_type_concept_id,subject_concept_id} 
condition_occurrence | {condition_concept_id,condition_source_concept_id,condition_type_concept_id} 
death    | {cause_concept_id,cause_source_concept_id,death_impute_concept_id,death_type_concept_id} 
device_exposure  | {device_concept_id,device_source_concept_id,device_type_concept_id} 
drug_exposure  | {dose_unit_concept_id,drug_concept_id,drug_source_concept_id,drug_type_concept_id,route_concept_id} 

我想說的是一樣的東西:

SELECT table_name, 
     array_agg(SELECT colname FROM column_names WHERE colname LIKE '%type%') AS type_cols, 
     array_agg(SELECT colname FROM column_names WHERE colname NOT LIKE '%type%') AS other_cols 
FROM mytable 
GROUP BY table_name 

而且我想結果會是:

 table_name  |  type_cols    |  other_cols 
----------------------+-------------------------------------------------------------------------------------------------------------- 
attribute_definition | {attribute_type_concept_id} | {}                   
cohort_definition | {definition_type_concept_id} | {subject_concept_id}              
condition_occurrence | {condition_type_concept_id} | {condition_concept_id,condition_source_concept_id}       
death    | {death_type_concept_id}  | {cause_concept_id,cause_source_concept_id,death_impute_concept_id}    
device_exposure  | {device_type_concept_id}  | {device_concept_id,device_source_concept_id}         
drug_exposure  | {drug_type_concept_id}  | {dose_unit_concept_id,drug_concept_id,drug_source_concept_id,route_concept_id} 

所以,我想結束具有相同的行數但不同的列。有一個簡單的方法來做到這一點。爲什麼我找不到它?

回答

2

unnest是你的朋友。在:

SELECT table_name, 
     array(SELECT colname FROM unnest(column_names) AS colname WHERE colname LIKE '%type%') AS type_cols, 
     array(SELECT colname FROM unnest(column_names) AS colname WHERE colname NOT LIKE '%type%') AS other_cols 
FROM mytable 
GROUP BY table_name, column_names 
+0

,我得到一個錯誤:'''錯誤:列 「colname的」 不存在 LINE 2:陣列(SELECT colname的FROM UNNEST(COLUMN_NAMES)WHERE ...'''執行中的項目。該數組有一個可選擇的列名? – Sigfried

+0

糟糕,需要在'unnest'之後添加'as colname'。回答 –

+1

也需要將column_names添加到組中。但就是這樣。謝謝! – Sigfried

1

這是丹Getz的答案,但在一個獨立的聲明,所以它很容易運行,而無需複製我的數據。

with grps as 
(
    with numlist as 
    (
    select '1 - 10' as grp, generate_series(1,10) num 
    union 
    select '11 - 20', generate_series(11,20) order by 1,2 
) 
    select grp, array_agg(num) as nums 
    from numlist 
    group by 1 
) 
select grp, 
     (select array_agg(evens) from unnest(nums) as evens where evens % 2 = 0) as evens, 
     (select array_agg(odds) from unnest(nums) as odds where odds % 2 != 0) as odds 
from grps 
group by grp, nums; 

    grp |  evens  |  odds 
---------+------------------+------------------ 
11 - 20 | {12,14,16,18,20} | {11,13,15,17,19} 
1 - 10 | {2,4,6,8,10}  | {1,3,5,7,9}