重复一列,将其复制到同一行中更相同的列中

时间:2012-10-31 12:49:49

标签: postgresql pivot crosstab

我的表格有3列,如下所示:(ID, city,city_pop).我只想复制 city_pop 列20次。所以决赛桌看起来像这样:(ID, city, city_pop, city_pop1, city_pop2, ...city_pop20) city_pop 具有相同的值。有可能吗?

2 个答案:

答案 0 :(得分:4)

-- CREATE TABLE cities20 AS
SELECT id,city
    , city_pop AS city_pop0
    , city_pop AS city_pop1
    , city_pop AS city_pop2
    , city_pop AS city_pop3
    , city_pop AS city_pop4
    , city_pop AS city_pop5
    , city_pop AS city_pop6
    , city_pop AS city_pop7
    , city_pop AS city_pop8
    , city_pop AS city_pop9
    , city_pop AS city_pop10
    , city_pop AS city_pop11
    , city_pop AS city_pop12
    , city_pop AS city_pop13
    , city_pop AS city_pop14
    , city_pop AS city_pop15
    , city_pop AS city_pop16
    , city_pop AS city_pop17
    , city_pop AS city_pop18
    , city_pop AS city_pop19
 FROM cities
     ;

答案 1 :(得分:2)

这是crosstab from the tablefunc extension的另一项工作,但again it seems like a truly bizarre thing to want to do

设置演示数据并加载tablefunc扩展程序:

CREATE TABLE cities (ID integer, city text, city_pop integer);

INSERT INTO cities VALUES (1,'Boganville',200), (2, 'Amity', 543);

CREATE EXTENSION tablefunc;

查询:

SELECT city, ct.* FROM crosstab(
  'SELECT id, ''city_pop''||CASE WHEN x = 1 THEN '''' ELSE x::text END AS colname, city_pop  FROM generate_series(1,20) x CROSS JOIN cities ORDER BY 1, x;'
) ct(
    cityid integer,
    city_pop integer, city_pop2 integer, city_pop3 integer, city_pop4 integer,
    city_pop5 integer, city_pop6 integer, city_pop7 integer, city_pop8 integer,
    city_pop9 integer, city_pop10 integer, city_pop11 integer, city_pop12 integer,
    city_pop13 integer, city_pop14 integer, city_pop15 integer, city_pop16 integer,
    city_pop17 integer, city_pop18 integer, city_pop19 integer, city_pop20 integer
) INNER JOIN cities ON (ct.cityid = cities.id);

结果:

    city    | cityid | city_pop | city_pop2 | city_pop3 | city_pop4 | city_pop5 | city_pop6 | city_pop7 | city_pop8 | city_pop9 | city_pop10 | city_pop11 | city_pop12 | city_pop13 | city_pop14 | city_pop15 | city_pop16 | city_pop17 | city_pop18 | city_pop19 | city_pop20 
------------+--------+----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------
 Boganville |      1 |      200 |       200 |       200 |       200 |       200 |       200 |       200 |       200 |       200 |        200 |        200 |        200 |        200 |        200 |        200 |        200 |        200 |        200 |        200 |        200
 Amity      |      2 |      543 |       543 |       543 |       543 |       543 |       543 |       543 |       543 |       543 |        543 |        543 |        543 |        543 |        543 |        543 |        543 |        543 |        543 |        543 |        543
(2 rows)

生成的列表:

SELECT string_agg( 'city_pop'||CASE WHEN x = 1 THEN '' ELSE x::text END || ' integer', ', ')  FROM generate_series(1,20) x;