How can I break down query result into separate columns?

后端 未结 1 1465
星月不相逢
星月不相逢 2021-01-22 04:32

Following up from my previous question:

Pixel values of raster records to be inserted in the table as columns

Imagine my query result has 5300 row as results lik

1条回答
  •  别那么骄傲
    2021-01-22 05:17

    Here's a possibly somewhat saner approach using multi-dimensional arrays.

    Please never, ever, ever, ever use the following query:

    CREATE EXTENSION tablefunc;
    
    SELECT * FROM crosstab(
      'SELECT ''row1''::text, width_bucket(x, 1, 53001, 100), array_agg(x order by x)::integer[] from generate_series(1,53000) x GROUP BY 2'
    ) ct(
     rowname text,
        col1 integer[], col2 integer[], col3 integer[], col4 integer[], col5
        integer[], col6 integer[], col7 integer[], col8 integer[], col9 integer[],
        col10 integer[], col11 integer[], col12 integer[], col13 integer[], col14
        integer[], col15 integer[], col16 integer[], col17 integer[], col18 integer[],
        col19 integer[], col20 integer[], col21 integer[], col22 integer[], col23
        integer[], col24 integer[], col25 integer[], col26 integer[], col27 integer[],
        col28 integer[], col29 integer[], col30 integer[], col31 integer[], col32
        integer[], col33 integer[], col34 integer[], col35 integer[], col36 integer[],
        col37 integer[], col38 integer[], col39 integer[], col40 integer[], col41
        integer[], col42 integer[], col43 integer[], col44 integer[], col45 integer[],
        col46 integer[], col47 integer[], col48 integer[], col49 integer[], col50
        integer[], col51 integer[], col52 integer[], col53 integer[], col54 integer[],
        col55 integer[], col56 integer[], col57 integer[], col58 integer[], col59
        integer[], col60 integer[], col61 integer[], col62 integer[], col63 integer[],
        col64 integer[], col65 integer[], col66 integer[], col67 integer[], col68
        integer[], col69 integer[], col70 integer[], col71 integer[], col72 integer[],
        col73 integer[], col74 integer[], col75 integer[], col76 integer[], col77
        integer[], col78 integer[], col79 integer[], col80 integer[], col81 integer[],
        col82 integer[], col83 integer[], col84 integer[], col85 integer[], col86
        integer[], col87 integer[], col88 integer[], col89 integer[], col90 integer[],
        col91 integer[], col92 integer[], col93 integer[], col94 integer[], col95
        integer[], col96 integer[], col97 integer[], col98 integer[], col99 integer[],
        col100 integer[]
    );
    

    The correct way to solve this problem is almost certainly in your application. SQL is good at many things. This isn't one of them, especially in PostgreSQL with its very limited pivot support. In any database this would be a crazy thing to do.

    As far as I'm concerned, if I have to use a query just to generate the column-list, that's a sign the system is being forced to do something it isn't well suited for:

    select string_agg('col'||n||' integer[]',', ') FROM generate_series(1,100) n;
    

    0 讨论(0)
提交回复
热议问题