如何在PostgreSQL中使用选择查询插入数据?

时间:2019-08-22 08:31:46

标签: postgresql

如何使用选择查询和插入数据?我试过了。结果是受影响的成功>受影响的行:1.但表中未添加行。

CREATE TABLE Clases(Segment1_ID char(4), Segment1_Name varchar(75), CompanyID char(4), amount Float(53));

INSERT INTO Clases (Segment1_ID, Segment1_Name, CompanyID, amount)
select Segment1_ID, Segment1_Name, dataupload1h_companyid CompanyID, sum(case when Segment3_Formula = '+' then dataupload1d_amount else dataupload1d_amount * -1 end) Amount
from t_dataupload1_header
inner join t_dataupload1_detail
    on dataupload1h_id = dataupload1d_id
inner join M_Account
    on dataupload1d_accountid = Account_ID
    and dataupload1h_companyid = Account_CompanyID
inner join M_Segment4
    on Account_Segment4ID = Segment4_ID
    and dataupload1h_companyid = Segment4_CompanyID
inner join M_Segment3
    on Segment4_Segment3ID = Segment3_ID
    and dataupload1h_companyid = Segment3_CompanyID
inner join M_Segment2
    on Segment3_Segment2ID = Segment2_ID
    and dataupload1h_companyid = Segment2_CompanyID
inner join M_Segment1
    on Segment2_Segment1ID = Segment1_ID
    and dataupload1h_companyid = Segment1_CompanyID
where dataupload1h_companyid = '1000'
group by Segment1_ID, Segment1_Name, dataupload1h_companyid
order by Segment1_ID, Segment1_Name, dataupload1h_companyid;

请帮助。谢谢

1 个答案:

答案 0 :(得分:0)

如果选择返回了所需的结果,则检查是否在PgAdmin中打开了自动提交(我想您正在使用它)或将COMMIT设置为;插入之后。