我有如下数据:
created_at | 地位 ---------------------+------------ 2016-04-05 1:27:15 | 信息 2016-04-05 3:27:15 | 信息 2016-04-05 5:27:15 | 警告 2016-04-05 10:27:15 | 信息 2016-04-05 11:27:15 | 警告
有了这些数据,我想转换如下:
状态 | 2016-04-05 1:00:00 | 2016-04-05 4:00:00 | 2016-04-05 8:00:00 | 2016-04-05 12:00:00 ---------+--------------------+------------ -+--------------------+-------------------- 信息 | 1 | 1 | 0 | 1 警告 | 0 | 0 | 1 | 1
谁能建议最好的方法来做到这一点?
我自己有一个相当复杂的问题,我希望有人可以帮助我。
我想使用 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)