这是我的起点(工作查询):
select
count(boot_est_pct > 0) as visited,
count(*) as num_parcels,
sum((CAST(init_est_pct AS REAL) / 100) * dp.build_val) as impact,
sum((CAST(boot_est_pct AS REAL) / 100) * dp.build_val) as pda,
p.st_name as street
FROM v_damaged_parcels dp
LEFT JOIN i_parcels p ON dp.parcel = p.qpid
LEFT JOIN parcels pg ON pg.qpid = p.qpid
WHERE dp.event_id = 3
AND (init_est_pct > 0 OR boot_est_pct > 0 OR dp.boot_type = 'IA')
AND p.num_buildings > 0
group by p.st_name
order by p.st_name
这给了我每条街道的摘要信息。我的问题是街道可以跨越城市边界,以及团队边界,我希望能够在我的HTML页面上按城市和团队进行过滤。我想要的是一组团队,以及我的结果集中每个街道(行)的城市之一。我不想将团队和城市添加到GROUP BY。我该怎么办呢? 宗地表(上面的连接之一)具有每个宗地的几何(每条街道的许多宗地)。 有一个带有几何的城市表和一个带几何的团队表。
上述查询中的表格:
CREATE TABLE damaged_parcels
(
id serial NOT NULL,
parcel integer NOT NULL,
st_name character(100),
init_est_pct integer,
boot_est_pct integer
}
CREATE TABLE i_parcels
(
id serial NOT NULL,
qpid integer, -- parcel column in damaged_parcels
st_name character(50),
st_number character(100),
value integer,
num_buildings integer
}
CREATE TABLE parcel
(
id serial NOT NULL,
qpid integer NOT NULL,
gis_acres real,
the_geom geometry,
shapeid integer
}
团队&城市表:
CREATE TABLE event_team_region
(
id integer NOT NULL DEFAULT nextval('event_team_region_id_seq'::regclass),
event_id integer,
team_id integer,
the_geom geometry
}
CREATE TABLE teams
(
team_id serial NOT NULL,
team_name character(20),
transid integer,
lastmodified timestamp without time zone NOT NULL DEFAULT now(),
item_id integer NOT NULL,
CONSTRAINT teampk PRIMARY KEY (team_id)
)
CREATE TABLE cities
(
id integer NOT NULL DEFAULT nextval('municipal_id_seq1'::regclass),
alt_id character(30),
name character(60),
code character(60),
gis_acres real,
the_geom geometry
}
当前查询的结果:
+---------+---------+-----+---------+---------+ | street | visited | num | impact | pda | +---------+---------+-----+---------+---------+ | ABACO | 3 | 14 | 1018776 | 322707 | | | | | | | | ACORN | 1 | 1 | 23012 | 111608 | | | | | | | | AMENITY | 5 | 5 | 498364 | 1145531 | | | | | | | +---------+---------+-----+---------+---------+ What I want:+---------+---------+-----+---------+---------+-----------------+--------------------+ | street | visited | num | impact | pda | cities | teams | +---------+---------+-----+---------+---------+-----------------+--------------------+ | ABACO | 3 | 14 | 1018776 | 322707 | Mudville,Gothem | A-team | | | | | | | | | | ACORN | 1 | 1 | 23012 | 111608 | Orlando | Zulu,Wonder-twerps | | | | | | | | | | AMENITY | 5 | 5 | 498364 | 1145531 | Cityland | NULL | | | | | | | | | +---------+---------+-----+---------+---------+-----------------+--------------------+
答案 0 :(得分:0)
a_horse_with_no_name(美国!)指出了我正确的方向。他说尝试使用string_agg,但我正在使用8.4版,所以我尝试使用array_agg。这是工作查询:
select
p.st_name as street,
count(boot_est_pct > 0) as visited,
count(*) as num_parcels,
sum((CAST(init_est_pct AS REAL) / 100) * dp.build_val) as impact,
sum((CAST(boot_est_pct AS REAL) / 100) * dp.build_val) as pda,
array_agg(trim(t.team_name)) AS team_name,
array_agg(trim(m.name)) AS city
FROM v_idam_damaged_parcels dp
LEFT JOIN idam_parcels p ON dp.parcel = p.qpid
LEFT JOIN parcel pg on pg.qpid = p.qpid
LEFT JOIN v_event_team_region etr on etr.event_id = dp.event_id AND st_intersects(etr.the_geom,pg.the_geom)
LEFT JOIN v_teams t on t.team_id = etr.team_id
LEFT JOIN municipal m ON st_intersects(m.the_geom,pg.the_geom)
WHERE dp.event_id = 3
AND (init_est_pct > 0 OR boot_est_pct > 0 OR dp.boot_type = 'IA')
AND p.num_buildings > 0
group by p.st_name, dp.boot_est_pct
order by p.st_name