简体   繁体   English

如何在PostgreSQL中使用交叉表功能创建具有四列的数据透视表

[英]How can I create a pivot table with four columns using crosstab function in PostgreSQL

Unfortunately I couldn't find another questions regarding my issue, so I really hope someone can help me out here. 不幸的是,我找不到关于我的问题的其他问题,所以我真的希望有人可以在这里帮助我。

I am trying to create a pivot table using the crosstab function in PostgreSQL. 我正在尝试使用PostgreSQL中的交叉表功能创建数据透视表。 My problem is that I only can pivot three columns from my original table. 我的问题是我只能旋转原始表中的三列。 But I need to use four. 但是我需要使用四个。

The Table consists of four columns: kgs12, Date, V4, Hach. 该表由四列组成:kgs12,日期,V4,哈希。 I want to count the number of records of every category of V4 for every kgs12 and Date. 我想计算每个kgs12和Date的每个V4类别的记录数。 But with crosstab I only achieve to count the categories for either kgs12 or Date. 但是使用交叉表,我只能计算出kgs12或Date的类别。

My table looks like this: 我的桌子看起来像这样:

kgs12   Date   V4   Hach
5158020050   05.07.2016   IAB14   1
5158020050   05.07.2016   IAB14   2
5158020050   06.07.2016   IAB14   3
5158020050   06.07.2016   IAB12   4
5158020060   05.07.2016   IAB14   5
5158020060   05.07.2016   IAB12   6
5158020060   06.07.2016   IAB12   7
5158020060   06.07.2016   IAB12   8

I want a pivot table that looks the following way: 我想要一个透视表,其外观如下:

kgs12                Date         IAB12_count       IAB14_count
05158020050       2016-07-05      null               2
05158020050       2016-07-06       1                 1
05158020060       2016-07-05       1                 1
05158020060       2016-07-06       2                null
select * from crosstab
('select  kgs12, "V4", count(*) 
from pivot_test 
group by kgs12, "V4" order by kgs12')
as 
ct(kgs12 character varying, iab12 bigint, iab14 bigint)

But as stated I only can create a table with three columns like: 但是如上所述,我只能创建一个包含三列的表:

kgs12           IAB12_count  IAB14_count  
05158020050        1            3
05158020060        3            1

or 要么

Date             IAB12_count  IAB14_count
2016-07-05          1              3
2016-07-06          3              1

I am sure the solution is very easy but I am unable to find any. 我相信解决方案非常简单,但是我找不到任何解决方案。 So if someone could help me on this issue, that would be fantastic. 因此,如果有人可以在这个问题上为我提供帮助,那就太好了。

I've never used the crosstab function to do pivots, but if it is as you say and limited to one column for the keycolumn, why not merge the data going into it? 我从来没有使用过交叉表功能来进行数据透视,但是,如果按照您所说的那样,并且将关键列限制为一列,为什么不合并其中的数据呢?

select * from crosstab
('select  concat(kgs12, '--', Date) as key, "V4", count(*) 
from pivot_test 
group by kgs12, Date, "V4" order by kgs12')
as 
ct(key character varying, iab12 bigint, iab14 bigint)

(or maybe this version:) (或此版本:)

select * from crosstab
('select  concat(kgs12, '--', Date) as key, "V4", count(*) 
from pivot_test 
group by concat(kgs12, '--', Date), "V4" order by kgs12')
as 
ct(key character varying, iab12 bigint, iab14 bigint)

Maybe even split it apart again later: 甚至可能稍后再拆分:

select split_part(key, '--', 1), split_part(key, '--', 2), iab12_count, iab14_count from crosstab
(...)

I've always done my crosstabs in a database agnostic way: 我一直以与数据库无关的方式来完成交叉表的工作:

select 
  kgs12,
  date,
  count(CASE WHEN "V4" = 'IAB12' THEN 1 END) as iab12_count,
  count(CASE WHEN "V4" = 'IAB14' THEN 1 END) as iab14_count
from pivot_test 
group by kgs12, date

But it does require you to code for the variations in advance, you can't just point it at a table with 10k different values of v4 and automatically have it produce a 10002 column output 但这确实需要您预先为变化编码,您不能仅将其指向具有10k个不同v4值的表,并自动使它产生10002列输出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM