我有这个查询来获取在另一个geom中相交的geom的数量:
SELECT count(evidensapp_polystructures.brgy_locat) AS high,
evidensapp_polystructures.brgy_locat AS barangay,
evidensapp_polystructures.municipali AS municipality
FROM evidensapp_floodhazard
INNER JOIN evidensapp_polystructures
ON st_intersects(evidensapp_floodhazard.geom, evidensapp_polystructures.geom)
AND evidensapp_floodhazard.hazard= 'High'
GROUP BY evidensapp_polystructures.brgy_locat, evidensapp_polystructures.municipali;
如您所见,其hazard
等于High
。 我想获得与hazard
值相交的geom数量:Medium
和Low
。但是,如果某个几何图形已经在High
中相交,则在Medium
查询中排除,Low
同样排除那些在High
和Medium
中相交的几何图形。
我有这个想法,也许使用CASE或者我需要得到几何的id
然后在查询中尝试NOT IN
但不知道如何去做。可能是因为我是PostgreSQL或任何数据库工作的新手。
预期结果应如下:
表格详情:
CREATE TABLE evidensapp_floodhazard (
id serial NOT NULL,
hazard character varying(6) NOT NULL,
date_field character varying(60),
geom geometry(MultiPolygon,32651),
CONSTRAINT evidensapp_floodhazard_pkey PRIMARY KEY (id)
);
CREATE INDEX evidensapp_floodhazard_geom_id
ON evidensapp_floodhazard USING gist (geom);
ALTER TABLE evidensapp_floodhazard CLUSTER ON evidensapp_floodhazard_geom_id;
CREATE TABLE evidensapp_polystructures (
id serial NOT NULL,
bldg_name character varying(100) NOT NULL,
bldg_type character varying(50) NOT NULL,
brgy_locat character varying(50) NOT NULL,
municipali character varying(50) NOT NULL,
province character varying(50) NOT NULL,
geom geometry(MultiPolygon,32651),
CONSTRAINT evidensapp_polystructures_pkey PRIMARY KEY (id)
);
CREATE INDEX evidensapp_polystructures_geom_id
ON evidensapp_polystructures USING gist (geom);
ALTER TABLE evidensapp_polystructures CLUSTER ON evidensapp_polystructures_geom_id;
答案 0 :(得分:1)
由于你几乎无法处理字符串" High"," Medium"和"低"就比较而言,您必须使用子查询。一些CTE的解决方案可能是最干净的:
WITH hi AS (
SELECT ps.id, ps.brgy_locat, ps.municipali
FROM evidensapp_polystructures ps
JOIN evidensapp_floodhazard fh ON fh.hazard = 'High'
AND ST_Intersects(fh.geom, ps.geom)
), med AS (
SELECT ps.id, ps.brgy_locat, ps.municipali
FROM evidensapp_polystructures ps
JOIN evidensapp_floodhazard fh ON fh.hazard = 'Medium'
AND ST_Intersects(fh.geom, ps.geom)
EXCEPT SELECT * FROM hi
), low AS (
SELECT ps.id, ps.brgy_locat, ps.municipali
FROM evidensapp_polystructures ps
JOIN evidensapp_floodhazard fh ON fh.hazard = 'Low'
AND ST_Intersects(fh.geom, ps.geom)
EXCEPT SELECT * FROM hi
EXCEPT SELECT * FROM med
)
SELECT brgy_locat AS barangay, municipali AS municipality, high, medium, low
FROM (SELECT brgy_locat, municipali, count(*) AS high
FROM hi
GROUP BY 1, 2) cnt_hi
FULL JOIN (SELECT brgy_locat, municipali, count(*) AS medium
FROM med
GROUP BY 1, 2) cnt_med USING (brgy_locat, municipali)
FULL JOIN (SELECT brgy_locat, municipali, count(*) AS low
FROM low
GROUP BY 1, 2) cnt_low USING (brgy_locat, municipali);
在三个CTE中,您首先要确定属于"高"的行?危险类别,然后是"媒体"危险类别,但EXCEPT
那些已经在"高"类别,那么"低"危险类别除了那些列为"高"或"中等"。在主要查询中,然后加入3个CTE,其中每个barangay和市政府的计数在每个CTE的子查询中计算。使用FULL JOIN
,以便在高等级的城市中没有结构的城镇和城市。危险等级也出现在结果中。