惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Y
Y Combinator Blog
B
Blog
S
SegmentFault 最新的问题
Vercel News
Vercel News
博客园 - 聂微东
宝玉的分享
宝玉的分享
C
Check Point Blog
有赞技术团队
有赞技术团队
IT之家
IT之家
V
V2EX
爱范儿
爱范儿
GbyAI
GbyAI
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Microsoft Azure Blog
Microsoft Azure Blog
P
Proofpoint News Feed
博客园 - 司徒正美
博客园_首页
Last Week in AI
Last Week in AI
博客园 - 叶小钗
量子位
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
F
Fortinet All Blogs
腾讯CDC
J
Java Code Geeks

博客园 - flyingfish

ORACLE 监听日志文件太大停止写监听日志引起数据库连接不上问题 基于陕西省地理信息公共服务平台的2011西安世园会电子地图正式发布 Toad Connecting: "cannot load OCI DLL: C:\oracle\product\10.2.0\db_2\BIN\oci.dll" - flyingfish ArcSDE启动遇到ORA-12560: TNS: 协议适配器错误解决办法 【转】Javascript 获取链接(url)参数的方法 [转]VisualStudio LightSwitch 7大顶级功能 [转]Silverlight 之轻 有关敏捷软件测试的文章 [转]SSL应用系列之三:去掉讨厌的证书提示警告(多图精解) 【转】Skyline软件介绍 【转】TB(TerraBuilder)转换日志:解决影像数据坐标转换后,在Skyline平台不能显示的问题 如何知道ORACLE表最后被修改,访问的时间 【转】如何成功实施结对编程 【转】结对编程及其优势 诚聘Silverlight、WCF程序员(或实习生) [转]VS2008在64位Windows平台上编译ArcEngine导致HRESULT:0x80040154 (REGDB_E_CLASSNOTREG)错误的解决办法 Web系统突然爆”Asp.net ajax客户端框架未能加载“的一种可能原因(误改服务器系统时间) 再读《没有银弹》 [转]华为总裁任正非谈企业管理:正确的方向来自于妥协
PostgreSQL Crosstab Query (交叉表)试用随记
flyingfish · 2012-11-27 · via 博客园 - flyingfish

PostgreSQL Crosstab Query

You can use the crosstab() function of the additional module tablefunc - which you have to installonce per database. Since PostgreSQL 9.1 you can use CREATE EXTENSION for that:

CREATE EXTENSION tablefunc;

http://stackoverflow.com/questions/3002499/postgresql-crosstab-query

初用PostgreSQL的交叉表,遇到问题,这篇文章帮助解决了问题。

特别注意:
1、extension的安装方法,最后使用pgAdminiii的扩展工具右键功能加上的。
2、crosstab方法在使用中一定要注意强制类型转换,此前好几次试验失败都是默认没加类型转换。例如row_name::text。
3、一定要注意croostab在行转列过程中不管列的排序问题,必须将ct(...)表达式中的值枚举与此前的select中排序顺序对应起来,否则会导致转列后的数值填充错位。资料的例子中是这么说的:

Proper answer

Install the additional module tablefunc which provides the function crosstab() once per database. Since PostgreSQL 9.1 you can use CREATE EXTENSION for that:

CREATE EXTENSION tablefunc;

Improved test case

CREATE TEMP TABLE t (
  section   text
 ,status    text
 ,ct        integer -- don't use "count" as column name.);INSERTINTO t VALUES('A','Active',1),('A','Inactive',2),('B','Active',4),('B','Inactive',5),('C','Inactive',7);-- no row for C with 'Active'

Simple form - not fit for missing attributes

SELECT*FROM   crosstab('SELECT section, status, ct
       FROM   t
       ORDER  BY 1,2')AS ct ("Section" text,"Active" int,"Inactive" int);

Returns:

 Section | Active | Inactive
---------+--------+----------
 A       |      1 |        2
 B       |      4 |        5
 C       |      7 |
  • No need for casting and renaming
  • Note the incorrect result for C: the value 7 is filled in for the first column.

Safe form

SELECT*FROM crosstab('SELECT section, status, ct
        FROM   t
        ORDER  BY 1,2',$$VALUES('Active'::text),('Inactive')$$)AS ct ("Section" text,"Active" text,"Inactive" int);

Returns:

 Section | Active | Inactive
---------+--------+----------
 A       |      1 |        2
 B       |      4 |        5
 C       |        |        7
  • Note the correct result for C.

  • The second parameter can be any query that returns one row per attribute in the appropriate order (VALUES expression in the example).
    Often you will want to query distinct attributes from the underlying table like this:

    'SELECT DISTINCT attribute FROM tbl ORDER BY 1'
  • I used dollar quoting in the second parameter query to make quoting easier.


按照croostab函数创建的相关视图脚本关键代码如下(环境为ArcGIS 10.1 + PostgreSQL 9.1.3,用到ArcGIS的空间视图和Query Layer特性):

----------------------------------------------------------------------地市统计图表相关视图------------------------------------------------------------------------------------------

--生成地市交叉统计表

SELECT

         ct.cityid,

         COALESCE(ct."cata1" , 0)  as  cata1,

         COALESCE( ct."cata2",0)  as  cata2,

         COALESCE(ct."cata3",0 )  as  cata3,

         COALESCE(ct."cata4",0)  as  cata4,

         COALESCE(ct."cata5", 0)  as  cata5,

         COALESCE(ct."cata6",0)  as  cata6,

         COALESCE(ct."cata7",0)  as  cata7,

         COALESCE(ct."cata8" ,0)  as  cata8

FROM house.crosstab

         ('SELECT   

                   rpad(districtid,4) ||''00''::text as cityid,

                   housetype::text,

                   count(*)::int

         FROM        house.poi

         GROUP BY cityid, housetype

         ORDER BY 1,2'::text,

         'SELECT distinct housetype

         FROM  house.poi

         ORDER BY 1'::text

         )

ct(cityid text, "cata1" integer, "cata2" integer, "cata3" integer, "cata4" integer, "cata5" integer, "cata6" integer, "cata7" integer, "cata8" integer);

--生成带统计值的地市空间视图

SELECT *

  FROM house.city,house.v_sum

  where city.pac=v_sum.cityid;

----------------------------------------------------------------------地市统计图表相关视图------------------------------------------------------------------------------------------

----------------------------------------------------------------------县区统计图表相关视图------------------------------------------------------------------------------------------

--生成县区交叉统计表

SELECT ct.countyid, COALESCE(ct."cata1", 0) AS "cata1", COALESCE(ct."cata2", 0) AS "cata2", COALESCE(ct."cata3", 0) AS "cata3", COALESCE(ct."cata4", 0) AS "cata4",

 COALESCE(ct."cata5", 0) AS "cata5", COALESCE(ct."cata6", 0) AS "cata6", COALESCE(ct."cata7", 0) AS "cata7", COALESCE(ct."cata8", 0) AS "cata8"

   FROM house.crosstab('SELECT   

                   districtid::text as countyid,

                   housetype::text,

                   count(*)::int

         FROM        house.poi

         GROUP BY countyid, housetype

         ORDER BY 1,2'::text, 'SELECT distinct housetype

         FROM  house.poi

         ORDER BY 1'::text) ct(countyid text, "cata1" integer, "cata2" integer, "cata3" integer, "cata4" integer, "cata5" integer, "cata6" integer, "cata7" integer, "cata8" integer);

--生成带统计值的县区空间视图

SELECT *

   FROM house.county, house.v_county_sum v_sum

  WHERE county.pac= v_sum.countyid;

----------------------------------------------------------------------县区统计图表相关视图------------------------------------------------------------------------------------------