我有一个SQL查询,它使用WITH
生成一个新表。
如何将临时表存储在我的数据库中? 这是代码:
WITH gme AS(
SELECT distinct seller, buyer, currency, value, cost_type
FROM iotable
)
SELECT seller, array_agg(buyer), array_agg(cost_type),
array_agg(value), array_agg(currency)
FROM gme
GROUP by seller
这是表输出:
3;"{277,222,276,288,443,445,241,224,280,223,285,5,238,286,278,444,240,287,242,3,8,4}";"{2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2}";"{2,407,89,6,7,3,36,1212,604,15920,56,11,291,29,1,8,13,4,9,3163,8,356}";"{USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD}"
4;"{220,223,421,228,224,277,445,10,237,315,222,238,280,395,246,4,313,8,443,244,242,469,308,219,221,389,444,409,410,9,11,12,227,236,441,225}";"{2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2}";"{8799,226,5,184,248,37,1,2149,482,154,5745,329,3402,7,105,6443,450,270,3,1070,658,182,1033,932,9091,101,6,1,14,3778,256,1639,2935,1082,9,570}";"{USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD}"
5;"{242,441,458,444,6,443,332,456,410,469,439,5,237,313,421,317,241,466,227,445,240,228,423,467,238,438,424}";"{2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2}";"{1604,12,2,89,10,178,2,3,66,397,5,762,9,4,54,3,118,1,1939,14,20,1090,55,1,273,4,3}";"{USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD}"
6;"{444,469,225,226,245,242,228,246,241,237,456,389,441,232,238,227,439,239,240,445,443,6,410,236}";"{2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2}";"{15,37,54,884,2130,2623,2846,1,323,162,1,1,2,206,981,870,1,2891,150,6,12,171,10,155}";"{USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD}"
7;"{390,404,413,400,228,443,370,37,238,280,439,45,441,378,384,369,308,469,437,403,16,456,458,385,47,342,317,7,217,387,313,438,311,397,242}";"{2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2}";"{68,1,3,1,7,1,2,44,99,325,7,100,1,1,8,2,23,1527,10,740,315,10,2,20,188,1,277,3555,433,10,363,18,224,1,405}";"{USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD}"
8;"{46,47,28,237,308,43,242,44,3,244,240,389,369,11,9,39,23,224,247,37,42,24,27,469,40,238,443,45,439,241,8,223,445,4,250,437,226,249,36,239,315,41,277,395,10,285,403,232,444,25,22,280,21,38,225}";"{2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2}";"{22,598,10,19,58,57,38,23,242,194,29,24,2,178,1580,5,3,91,1760,550,8,4,5,246,62,2262,3,283,20,1695,594,259,1,1001,513,2,2586,1984,114,553,1,60,64,2,1568,18,37,5,4,9,13,758,9,48,5}";"{USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD}"
9;"{8,308,389,6,395,9,233,5,4,10,11,3,7,16}";"{2,2,2,2,2,2,2,2,2,2,2,2,2,2}";"{19,547,51,24,4,17335,42342,6,317,1447,377,30,11,26}";"{USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD,USD}"
10;"{469,389,229,230,11,231,232,242}";"{2,2,2,2,2,2,2,2}";"{15,2,17951,17058,4,5613,645,725}";"{USD,USD,USD,USD,USD,USD,USD,USD}"
我复制了pgadmin3
的输出答案 0 :(得分:0)
您可以在select中使用INTO语句,它基本上会接受您的查询并在您的数据库中创建一个新表。
所以这应该工作
SELECT distinct seller, buyer, currency, value, cost_type
into gme FROM iotable;
或者您可以这样做,具体取决于您在表格中的内容
WITH gme AS(
SELECT distinct seller, buyer, currency, value, cost_type
FROM iotable
),
gme1 AS (
Select seller, buyer, cost_type, value, currency
INTO MYNEWTABLE
FROM gme
)
更新: 解决方案(无论如何使用此方法)如下
WITH gme AS(
SELECT distinct seller, buyer, currency, value, cost_type
FROM iotable
),
gme1 AS (
Select seller, buyer, cost_type, value, currency FROM gme
)
SELECT seller,
array_agg(buyer) as AGG_BUYER,
array_agg(cost_type) as AGG_CT,
array_agg(value) as AGG_VAL,
array_agg(currency) as AGG_CUR
/*this is where the new table is generated*/
INTO MY_NEW_TABLE
FROM gme1
GROUP by seller