WITH hi AS ( SELECT ps.id, ps.brgy_locat, ps.municipali, ps.bldg_name, fh.gridcode, ps.bldg_type FROM evidensapp_polystructures ps JOIN evidensapp_floodhazard fh ON fh.gridcode=3 AND ST_Intersects(fh.geom, ps.geom) ), med AS ( SELECT ps.id, ps.brgy_locat, ps.municipali ,ps.bldg_name, fh.gridcode, ps.bldg_type FROM evidensapp_polystructures ps JOIN evidensapp_floodhazard fh ON fh.gridcode=2 AND ST_Intersects(fh.geom, ps.geom) EXCEPT SELECT * FROM hi ), low AS ( SELECT ps.id, ps.brgy_locat, ps.municipali,ps.bldg_name, fh.gridcode, ps.bldg_type FROM evidensapp_polystructures ps JOIN evidensapp_floodhazard fh ON fh.gridcode=1 AND ST_Intersects(fh.geom, ps.geom) EXCEPT SELECT * FROM hi EXCEPT SELECT * FROM med ) SELECT brgy_locat, municipali, bldg_name, bldg_type, gridcode, count( bldg_name) FROM (SELECT brgy_locat, municipali, bldg_name, gridcode, bldg_type FROM hi GROUP BY 1, 2, 3, 4, 5) cnt_hi FULL JOIN (SELECT brgy_locat, municipali,bldg_name, gridcode, bldg_type FROM med GROUP BY 1, 2, 3, 4, 5) cnt_med USING (brgy_locat, municipali, bldg_name,gridcode,bldg_type) FULL JOIN (SELECT brgy_locat, municipali,bldg_name,gridcode, bldg_type FROM low GROUP BY 1, 2, 3, 4, 5) cnt_low USING (brgy_locat, municipali, bldg_name, gridcode, bldg_type)
The above query returns an error:
ERROR: column "cnt_hi.brgy_locat" should appear in the GROUP BY clause or be used in the aggregate function ********** Error **********
ERROR: column "cnt_hi.brgy_locat" should appear in the GROUP BY clause or be used in the state of the SQL aggregate: 42803
But if I omit count(bldg_name) , it works. But I need to calculate based on bldg_name .
EDIT: I wanted to get the number of buildings that intersect with the hazard value (gridcode): High (3), Medium (2) and Low (1). But if some geometry intersects already in High, exclude the middle query in it, and the same as Low, exclude the geometry that intersects in High and Medium.
PostgreSQL: 9.4, PostGIS: 2.1.7
Table Details:
CREATE TABLE evidensapp_floodhazard ( id integer NOT NULL DEFAULT nextval('evidensapp_floodhazard_id_seq'::regclass), gridcode integer 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;
The supposed conclusion is similar to this, but with the correct calculation: 
EDIT 2: In any case, I try to explain what the intended conclusion means:
- count
bldg_name , not id , in which network code does it cross into floodhazard with the condition, as indicated above, on EDIT 1 . - then group it with what
brgy_locat , brgy_municipali and what gridcode and bldg_type it belongs to.
Please take a look at the image above.