这是查询的结果:
landuse列包含多个多边形属性。
现在我想为同一个表创建n个新列,这些列具有该查询结果字符串中的名称(分配,墓地,商业....)。
我会手动完成,但我不知道存在的所有属性。因此,我需要一个程序化的解决方案。
我在其他帖子中找不到合适的解决方案。
CREATE TABLE wu_stations_karlsruhe_landcover_features AS (
WITH city_stations AS (
SELECT city, centerid, loc
--,ST_Buffer(loc :: GEOGRAPHY, 1000) AS stat_surr -- Take 1km radius to find close land coverage polygons
FROM wunderground_stations_within_large_cities
WHERE city = 'Karlsruhe'
),
stations_landuse_landcover AS
(
SELECT city, centerid, landuse, "natural", AVG(cs1.loc::geography <-> gp1.way::geography) AS dist, SUM(ST_Area(way::geography))
FROM ger_polygon_landcover AS gp1, city_stations AS cs1
WHERE ST_DWithin(cs1.loc::geography, gp1.way::geography, 1000)
GROUP BY city, centerid, landuse, "natural"
ORDER BY city, centerid, landuse, "natural"
)
SELECT *
FROM stations_landuse_landcover
);
答案 0 :(得分:0)
最简单的方法可能是编写一个SQL查询来生成SQL,然后可以对数据库运行。这在使用大型数据集时并不罕见,但您需要注意生成的SQL在运行时不会引起问题!
我在下面概述了一个简单的例子:
CREATE TABLE Example (
ID int NOT NULL,
Name text NOT NULL,
PRIMARY KEY (ID)
);
INSERT INTO Example VALUES (1,'Grass'), (2,'Allotments'), (3,'Meadow');
SELECT concat('ALTER TABLE Example ADD COLUMN ' , NAME, ' text NULL;')
FROM Example
这是SQL的输出,您应该在针对数据库运行之前仔细检查。