我有一张医院(点)的空间表,与一个(非空间)化合价表(cirurgy,urgency等)有一对多的关系。
医院(id,name,geom); 效价(id,name,valence)
"名称"是常见的领域。
如何在PostgreSQL / Postgis中构建一个有效的Geojson,每个医院(点)可以有一个或多个化合价?
我已经尝试过这个查询的一些变体,但总是给出相同的错误"由用作表达式"的子查询返回的多行。
SELECT row_to_json(fc) FROM
(
SELECT 'FeatureCollection' As type, array_to_json(array_agg(f)) As features from
(
SELECT 'Feature' As type, ST_AsGeoJSON(lg.geom)::json As geometry, row_to_json
((select l from (select
v.*
FROM valence v
inner join hospital lg on lg."name" = v."name" ) As l
)) As properties
from hospital as lg)
As f
) As fc;
非常感谢!
答案 0 :(得分:2)
使用此查询,"效价列表"在属性properties.valences
SELECT
json_build_object(
'type', 'FeatureCollection',
'features', json_agg(
json_build_object(
'type', 'Feature',
'geometry', ST_AsGeoJSON(h.geom)::json,
'properties', json_build_object(
'name', h.name,
'valences', (
-- Generate json array of "valences":
SELECT array_to_json(array_agg(v.valence))
FROM valence v
WHERE v.name = h.name
GROUP BY v.name
)
)
)
)
) json
FROM
hospital h
,返回的GeoJSON对象有效