enter image description here如何从一个表复制列值并将相同的值作为一行存储到另一个表中。
我有两个表,一个是firsttbl,第二个是testtbl。然后第一个表有更多记录,第二个表需要在第一个表中复制相同的值。
下面的firsttbl原始记录
CORRELATION_ID NAME VALUE TYPE OBJECT_ID ARCHIVE_FLAG ARCHIVE_DATE WF_ID REC_TIME KEY_ID VALUE_UPPER
43344255015b9b192916node1 TransactionSenderID SANMINACORT DOCUMENT 14804515b9b1924f9node1 -1 null 1912341 2017-04-23 08:56:09.0 20 SANMINACORT
43391255115b9b192916node1 TransactionReceiverID 4017395800 DOCUMENT 14804515b9b1924f9node1 -1 null 1912341 2017-04-23 08:56:09.0 21 4017395800
43376255215b9b192916node1 Level Transaction DOCUMENT 14804515b9b1924f9node1 -1 null 1912341 2017-04-23 08:56:09.0 41 TRANSACTION
43399255315b9b192916node1 GroupSenderID SANMINACORT DOCUMENT 14804515b9b1924f9node1 -1 null 1912341 2017-04-23 08:56:09.0 28 SANMINACORT
43356255415b9b192916node1 GroupReceiverID 4017395800 DOCUMENT 14804515b9b1924f9node1 -1 null 1912341 2017-04-23 08:56:09.0 30 4017395800
仅创建了第二个表尚未存储在任何值中。
TransactionSenderID TransactionReceiverID Level GroupSenderID GroupReceiverID
第二个表库名已经存储在NAME coulmn下的firsttbl中。
我要从firsttbl表复制记录,因此firsttbl列名称VALUE所有5个值都需要作为一行存储在第二个表(testtbl)中。 这个查询我尝试但尚未成功。
INSERT INTO testtbl(TransactionSenderID,TransactionReceiverID,Level,GroupSenderID,GroupReceiverID) Select value from firsttbl;
这是PostgreSQL中的错误。
错误:INSERT的目标列多于表达式LINE 1:INSERT INTO testtbl(TransactionSenderID,TransactionReceiverI ...... ^
**********错误**********
错误:INSERT的目标列多于表达式SQL状态: 42601字符:41
答案 0 :(得分:0)
这看起来像一个支点/组聚合问题。我没有postgresql,但因为问题是标记mysql这里是一个mysql解决方案(如果问题是postgresql,方法应该类似)
drop table if exists t;
create table t (
CORRELATION_ID varchar(30), NAME varchar(30),VALUe varchar(30), TYPE VARCHAR(30), OBJECT_ID VARCHAR(30), ARCHIVE_FLAG VARCHAR(30),
ARCHIVE_DATE datetime, WF_ID int, REC_TIME datetime, KEY_ID int, VALUE_UPPER varchar(20));
insert into t values
('43344255015b9b192916node1', 'TransactionSenderID' ,'SANMINACORT' ,'DOCUMENT' ,'14804515b9b1924f9node1', '-1' ,null ,1912341 ,'2017-04-23 08:56:09.0', 20 ,'SANMINACORT'),
('43391255115b9b192916node1', 'TransactionReceiverID' ,'4017395800' ,'DOCUMENT' ,'14804515b9b1924f9node1', '-1' ,null ,1912341 ,'2017-04-23 08:56:09.0' ,21 ,'4017395800' ),
('43376255215b9b192916node1', 'Level', 'Transaction', 'DOCUMENT' ,'14804515b9b1924f9node1' ,'-1' ,null ,1912341 ,'2017-04-23 08:56:09.0', 41 ,'TRANSACTION' ),
('43399255315b9b192916node1', 'GroupSenderID' ,'SANMINACORT', 'DOCUMENT' ,'14804515b9b1924f9node1' ,'-1' ,null ,1912341 ,'2017-04-23 08:56:09.0' ,28, 'SANMINACORT'),
('43356255415b9b192916node1', 'GroupReceiverID', '4017395800', 'DOCUMENT' ,'14804515b9b1924f9node1' ,'-1' ,null ,1912341 ,'2017-04-23 08:56:09.0',30, '4017395800');
drop table if exists t2;
create table t2(wf_id int,TransactionSenderID varchar(20), TransactionReceiverID varchar(20), Level varchar(20), GroupSenderID varchar(20), GroupReceiverID varchar(20));
insert into t2
select wf_id,
max(case when name = 'TransactionSenderID' then value else null end),
max(case when name = 'TransactionReceiverID' then value else null end),
max(case when name = 'Level' then value else null end),
max(case when name = 'GroupSenderID' then value else null end),
max(case when name = 'GroupReceiverID' then value else null end)
from t
group by wf_id;
select * from t2;
MariaDB [sandbox]> select * from t2;
+---------+---------------------+-----------------------+-------------+---------------+-----------------+
| wf_id | TransactionSenderID | TransactionReceiverID | Level | GroupSenderID | GroupReceiverID |
+---------+---------------------+-----------------------+-------------+---------------+-----------------+
| 1912341 | SANMINACORT | 4017395800 | Transaction | SANMINACORT | 4017395800 |
+---------+---------------------+-----------------------+-------------+---------------+-----------------+
1 row in set (0.00 sec)