如何在Postgresql

时间:2018-07-23 10:39:51

标签: postgresql group-by count subquery

除了我确定threshold AS的步骤之外,下面的代码运行良好。它无需计算在polygon_dump AS步骤中创建的每个栅格的计数,而是对所有栅格进行计数。我一直在尝试以有限的成功使用GROUP BY。

WITH
--  Select Features
    feat AS 
        (SELECT toid AS building_id,
                wkb_geometry AS geom 
                FROM buildings
        ),
    polygon_dump AS
        (SELECT (ST_DumpAsPolygons(ST_Clip(a.st_roughness,1,b.geom,-9999,true))).val AS polygon_vals,building_id AS building_id2
            FROM grosvenor_raster_roughness a, feat b
        ),
    threshold AS
        (SELECT Count(*) AS thres_val
            FROM polygon_dump
                WHERE polygon_vals >= 0 AND polygon_vals < 0.5
        GROUP BY building_id2
        ),
    b_stats AS
        (SELECT  building_id, (stats).*
            FROM (SELECT building_id, ST_SummaryStats(ST_Clip(a.st_roughness,1,b.geom,-9999,true)) AS stats
                FROM grosvenor_raster_roughness a
                    INNER JOIN feat b
                ON ST_Intersects(b.geom,a.st_roughness)
            ) AS foo
        )
--  Summarise statistics
    SELECT count As pixel_count,
           thres_val AS threshold_val,
           cast(thres_val as real)/cast(count as real)*100 AS percent_value,
           min AS min_pixel_val,
           max AS max_pixel_val,
           mean AS avg_pixel_val,
           stddev AS pixel_stddev
        FROM b_stats, threshold
            WHERE count > 0;

我得到以下结果:

enter image description here

红色的两列是正确的结果,我该怎么做才能只得到那些结果?

1 个答案:

答案 0 :(得分:2)

您正在做交叉联接。您需要在docker-compose logs --no-color --tail=1000 CONTAINER_NAME > logs.txt CTE中添加带有building_id的列,以便可以与threshold联接。我不确定应该是LEFT还是INNER JOIN,所以我要使用INNER。

b_stats