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}
所以,我想結束具有相同的行數但不同的列。有一個簡單的方法來做到這一點。爲什麼我找不到它?
,我得到一個錯誤:'''錯誤:列 「colname的」 不存在 LINE 2:陣列(SELECT colname的FROM UNNEST(COLUMN_NAMES)WHERE ...'''執行中的項目。該數組有一個可選擇的列名? – Sigfried
糟糕,需要在'unnest'之後添加'as colname'。回答 –
也需要將column_names添加到組中。但就是這樣。謝謝! – Sigfried