如何使用条件选择从另一个表插入表

时间:2017-05-01 23:54:17

标签: sql sql-server-2008

我有UCP表和SalesPerProduct包含数据 我想插入SalesPerProduct来自UCP条件选择 当我选择它工作正常我使用此查询

select t.ID, sum(Qty) as sum_BioxellTerritories , 
sum(SalesResult) as Sales , BioxellTerritories 
from UCP as t group by ID ,BioxellTerritories
ORDER BY BioxellTerritories ASC 

当我使用insert

insert into SalesPerProduct 
(ProductID , Volume , Sales ,Territories )values
select t.ID, sum(Qty) as sum_BioxellTerritories , sum(SalesResult) 
as Sales , BioxellTerritories from UCP as t group by ID ,BioxellTerritories   ORDER BY BioxellTerritories ASC 

我没有为我工作..对于数据样本这是我要插入的选择结果

enter image description here

2 个答案:

答案 0 :(得分:2)

您只想要insert . . . select

insert into SalesPerProduct (ProductID, Volume, Sales, Territories )
    select t.ID, sum(Qty) as sum_BioxellTerritories, sum(SalesResult) 
as Sales, BioxellTerritories
    from UCP as t
    group by ID, BioxellTerritories
    order by BioxellTerritories ASC ;

VALUES关键字是不必要的。

答案 1 :(得分:1)

查看[Parsed_atrim_0 @ 00000000000c8700] Media type mismatch between the 'Parsed_atrim_0' filter output pad 0 (audio) and the 'Parsed_concat_2' filter input pad 0 (video) [AVFilterGraph @ 0000000002435260] Cannot create the link atrim:0 -> concat:0 Error initializing complex filters. Invalid argument 语句的此参考: https://www.w3schools.com/sql/sql_insert_into_select.asp

简而言之,在INSERT INTO SELECT语句与VALUES合并时,您不需要SELECT关键字。

如果您仍有问题我建议发布您的表格定义,以便人们可以为您提供进一步的帮助。