Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 如何在新表的多行中插入同一行中的不同值_Sql_Postgresql - Fatal编程技术网

Sql 如何在新表的多行中插入同一行中的不同值

Sql 如何在新表的多行中插入同一行中的不同值,sql,postgresql,Sql,Postgresql,我有一张桌子: id bigint, a integer, b integer, c integer, date date 我必须在日期之前将每个值放入一个新表中,这样做: B table: id bigint, type integer, date date 例如,如果我的A表中有这样一行: id a b c date 13 5 4 7 2014-11-09 id type date 1 5 2014-11-09, 2 4 2014-11-09, 3 7 2014

我有一张桌子:

id bigint,
a integer,
b integer,
c integer,
date date
我必须在日期之前将每个值放入一个新表中,这样做:

B table:
id bigint,
type integer,
date date
例如,如果我的A表中有这样一行:

id a b c date
13 5 4 7 2014-11-09
id type date
1  5    2014-11-09,
2  4    2014-11-09,
3  7    2014-11-09
我想把这个值放在B表中,如下所示:

id a b c date
13 5 4 7 2014-11-09
id type date
1  5    2014-11-09,
2  4    2014-11-09,
3  7    2014-11-09

有什么建议吗

首先使用UNION ALL取消PIVOT数据,然后使用ROW\U编号分配新id:

如果新id是自动生成的,则根本不需要行号


在Oracle中,可以尝试使用REGEXP模拟sqame。希望这有帮助

SELECT DISTINCT LEVEL,
  TRIM(regexp_substr(a.colk,'[^,]+', 1, level)) TYPE,
  SYSDATE
FROM
  (SELECT LEVEL,
    COL1
    ||','
    ||COL2
    ||','
    ||COL3 colk,
    SYSDATE
  FROM
    (SELECT 13 AS ID,5 COL1,4 AS COL2,7 AS COL3,sysdate AS dt FROM DUAL
    )
    CONNECT BY LEVEL <= REGEXP_COUNT(COL1
    ||','
    ||COL2
    ||','
    ||COL3,',')+1
  )a
  CONNECT BY regexp_substr(a.colk, '[^,]+', 1, level) IS NOT NULL;

最短代码竞赛:我的变体是:

insert into b(type, date)
select unnest(array[a,b,c]), date from a;
假设b表中的id列是自动递增的

insert into b(type, date)
select unnest(array[a,b,c]), date from a;