如何根据 postgresql 中的另一列对字符串聚合进行排序?
How to order a string aggregation based on another column in postgresql?
基本上我有 2 个多边形表,tableA,我想用来自 tableB 的聚合字符串进行更新。 tableB 有 2 列
TableB 的结构类似于
viable | fruit_id
yes | banana1
no | apple2
maybe | watermelon1
no | peach3
我的更新查询如下所示:
update TableA a set
fruitids = (select string_agg(fruit_id, ', ' order by fruit_id)
from tableB b st_contains(b.geom, a.geom))
但这只会 return 我按字母顺序排列水果 ID。我怎样才能让它首先列出可行的?在这种情况下,我的预期输出是:
banana1, watermelon1, apple2, peach3
您可以使用条件排序:
select string_agg(fruit_id, ', ' order by
case viable
when 'yes' then 1
when 'maybe' then 2
else 3
end,
fruit_id)
from tableB b st_contains(b.geom, a.geom)
基本上我有 2 个多边形表,tableA,我想用来自 tableB 的聚合字符串进行更新。 tableB 有 2 列
TableB 的结构类似于
viable | fruit_id
yes | banana1
no | apple2
maybe | watermelon1
no | peach3
我的更新查询如下所示:
update TableA a set
fruitids = (select string_agg(fruit_id, ', ' order by fruit_id)
from tableB b st_contains(b.geom, a.geom))
但这只会 return 我按字母顺序排列水果 ID。我怎样才能让它首先列出可行的?在这种情况下,我的预期输出是:
banana1, watermelon1, apple2, peach3
您可以使用条件排序:
select string_agg(fruit_id, ', ' order by
case viable
when 'yes' then 1
when 'maybe' then 2
else 3
end,
fruit_id)
from tableB b st_contains(b.geom, a.geom)