小编dan*_*lin的帖子

在 PostgreSQL 9.0 中创建 crosstab() 数据透视表

我自己有一个相当复杂的问题,我希望有人可以帮助我。

我想使用 PostgreSQL 来从无几何表创建数据透视表。

为了简单起见,我将只显示我想要用于枢轴的表结构,然后使用给出的方法来创建其他表结构,我希望:)

我想使用的字段是postcode_nospace_, 和ageband

ageband顶部的列是行,postcode_nospace_每个邮政编码的计数是数据。

有 2132 条记录,其中一些记录有多个邮政编码,因此计数。

CREATE TABLE adult_social_care.activities_in_localities_asc
(
  ogc_fid integer NOT NULL,
  sort numeric(5,0),
  ageband character(12),
  postcode_nospace_ character(8),
  wkb_geometry geometry,
  CONSTRAINT activities_in_localities_asc_pkey PRIMARY KEY (ogc_fid)
);
Run Code Online (Sandbox Code Playgroud)

更新:

这是我想要实现的结果,如具有相同数据的 Excel 数据透视表所示。

postcode 18_24  25_34   35_44   45_54   55_64   65_74   Total Count
----------------------------------------------------------------------------
BB115DE     1           2                   2      3       8
FY38LZ                              1       1              2
Run Code Online (Sandbox Code Playgroud)

通过环顾四周,我编译了以下 SQL 查询。它按邮政编码分组并创建所需的字段名称。但是这些字段是空白的。理想情况下,我还希望total_count在表格末尾有一列。

SELECT * FROM crosstab(
    'SELECT postcode_nospace_, ageband, count(ageband) as total_count
     FROM …
Run Code Online (Sandbox Code Playgroud)

postgresql pivot postgresql-9.0

5
推荐指数
1
解决办法
4335
查看次数

在 2 个字符串片段之间查找子字符串

我试图在 Postgres 9.0 的 PostGIS 中填充一个视图,在这个视图中我希望它包含一个基于 2 个字符串位置的子字符串。请参阅下面的我的代码。

CREATE OR REPLACE VIEW vw_actions AS 
 SELECT ls.f_table_schema, ls.f_table_name, 
 (SELECT substr(ls.attribute_actions_text, 
 strpos(ls.attribute_actions_text, 'name="')+6, 
 strpos(ls.attribute_actions_text, '"/>') - 
 strpos(ls.attribute_actions_text, 'name="'))) AS actions
   FROM layer_styles ls;
Run Code Online (Sandbox Code Playgroud)

结果是它在使用 strpos 时不喜欢负数。我可以让它前进 6 个字符以从返回的子字符串中删除 'name="' 但不能删除 '"/>'。

它返回以下内容:

View SHED Database"/> 
Run Code Online (Sandbox Code Playgroud)

我希望它返回的地方:

View SHED Database
Run Code Online (Sandbox Code Playgroud)

任何建议将不胜感激。

补充:我发现如果我使用 9.1,我可以使用 strposrev 并且我认为以下代码会起作用:

CREATE OR REPLACE VIEW vw_actions AS 
 SELECT ls.f_table_schema, ls.f_table_name, 
 (SELECT substr(ls.attribute_actions_text::text, 
 strpos(ls.attribute_actions_text::text, 'name="'::text)+6, 
 strposrev(ls.attribute_actions_text::text, '"/>'::text)+3 - 
 strpos(ls.attribute_actions_text::text, 'name="'::text))) AS actions
   FROM layer_styles ls;
Run Code Online (Sandbox Code Playgroud)

postgresql view pattern-matching postgresql-9.0

3
推荐指数
1
解决办法
7614
查看次数

将总和列添加到 PostgreSQL 9.0 中的 crosstab() 查询

继我之前的问题之后:

在 PostgreSQL 9.0 中创建 crosstab() 数据透视表

我设法创建了一个数据透视表来ageband使用该crosstab()函数。我可以使用它来创建基本无几何表的视图或表。

但是,这仍然没有多大用处,因为我需要将其链接到gazetteers_and_addresses.unit_postcode表格以分配几何图形以进行进一步分析。

我将附上两个表的表结构和用于创建交叉表的原始代码。

CREATE OR REPLACE VIEW adult_social_care.vw_ageband AS (
    SELECT * FROM crosstab(
        'SELECT postcode_nospace_, ageband, count(ageband) as total_count
         FROM adult_social_care.activities_in_localities_asc
         GROUP BY postcode_nospace_, ageband
         ORDER BY postcode_nospace_'

         ,$$VALUES ('18-24'::text), ('25-34'), ('35-44'), ('45-54'), ('55-64'), ('65-74'), ('75-84'), ('85-94'), ('95 AND OVER')$$)
    AS ct("postcode" text, "18-24" numeric, "25-34" numeric,"35-44" numeric, "45-54" numeric, "55-64" numeric, "65-74" numeric, "75-84" numeric, "85-94" numeric, "95 AND OVER" numeric));
Run Code Online (Sandbox Code Playgroud)

表定义:

activities_in_localities_asc

CREATE …
Run Code Online (Sandbox Code Playgroud)

postgresql datatypes cte pivot postgresql-9.0

2
推荐指数
1
解决办法
4518
查看次数