转置表保留ID

问题描述 投票:1回答:2

使用下面列出的转置表很难:

ID    Type  Col1  Col2  Col3
----------------------------
1     a      5     2     3
1     b      2     1     3
2     a      4     4     3
2     c      7     6     4

结果应如下所示:

ID    Col    a     b     c
----------------------------
1     Col1   5     2    null
1     Col2   2     1    null 
1     Col3   3     3    null
2     Col1   4    null   7
2     Col2   4    null   6
2     Col3   3    null   4

这里有很多类似的问题,但它们在我看来与我需要的略有不同,因为我希望在结果集中保留ID。试图使用tablefunc扩展没有运气。有什么想法怎么做?

postgresql pivot crosstab
2个回答
0
投票

通常我认为列名中的大写是有害的,所以我忽略了你的那部分请求。

你的数据:

create temp table d(id int,type text,col1 int,col2 int ,col3 int);
insert into d values (1,'a',5,2,3),(1,'b',2,1,3),(2,'a',4,4,3),(2,'c',7,6,4);

一种方法是转换为EAV,然后对其进行调整:

 with eav as (
   select id,type,'col1' as col,col1 as val from d
   union all
   select id,type,'col2',col2 from d
   union all
   select id,type,'col3',col3 from d) 
, ca as (select id,col, val from eav where type='a')
, cb as (select id,col, val from eav where type='b')
, cc as (select id,col, val from eav where type='c')
select id,col,ca.val as a ,cb.val as b, cc.val as c
 from ca 
 full outer join cb using (id,col)
 full outer join cc using (id,col)
 order by id,col;

结果:(在\pset null 'null' in psql之后)

 id | col  | a |  b   |  c   
----+------+---+------+------
  1 | col1 | 5 |    2 | null
  1 | col2 | 2 |    1 | null
  1 | col3 | 3 |    3 | null
  2 | col1 | 4 | null |    7
  2 | col2 | 4 | null |    6
  2 | col3 | 3 | null |    4

或者它可以安排如下:

with eav as (
   select id,type,'col1' as col,col1 as val from d
   union all
   select id,type,'col2',col2 from d
   union all
   select id,type,'col3',col3 from d) 
  , ca as (select id,col, val as a from eav where type='a')
  , cb as (select id,col, val as b from eav where type='b')
  , cc as (select id,col, val as c from eav where type='c')
 select *
  from ca 
  full outer join cb using (id,col)
  full outer join cc using (id,col)
  order by id,col;

0
投票

db<>fiddle

SELECT 
    id, 
    key as col,
    max(value::int) FILTER (WHERE type = 'a') as a,
    max(value::int) FILTER (WHERE type = 'b') as b,
    max(value::int) FILTER (WHERE type = 'c') as c
FROM 
    table, 
    jsonb_each_text(
        jsonb_build_object('col1', col1, 'col2', col2, 'col3', col3)
    )
GROUP BY id, key
ORDER BY id, key

主要思想是我需要使用id列将三列(以及它们的值作为键/值对)交叉连接。键/值对使我接受了JSON的想法。使用JSON功能,您可以创建JSON对象(jsonb_build_object()):

id  type   col1   col2   col3   jsonb_build_object
1   a      5      2      3      {"col1": 5, "col2": 2, "col3": 3}
1   b      2      1      3      {"col1": 2, "col2": 1, "col3": 3}
2   a      4      4      3      {"col1": 4, "col2": 4, "col3": 3}
2   c      7      6      4      {"col1": 7, "col2": 6, "col3": 4}

使用jsonb_each_text,您可以将JSON对象扩展为每个元素的一行,并为键和值提供额外的列(作为文本列):

id   type  (...)  key    value
1    a            col1   5
1    a            col2   2
1    a            col3   3
1    b            col1   2
1    b            col2   1
1    b            col3   3
2    a            col1   4
2    a            col2   4
2    a            col3   3
2    c            col1   7
2    c            col2   6
2    c            col3   4

其余的只是分组和过滤(做一些简单的旋转)。

© www.soinside.com 2019 - 2024. All rights reserved.