0

所以我试图从另一个表的选择查询中插入一个表。我想我必须这样做,因为我试图对插入中的几列求和。最终,我希望这是一个将数据从临时表移动到更规范化的数据的存储过程。我得到的错误是查询中最后一个字符的语法错误。您可以提供的任何帮助或建议将不胜感激。这是我的查询:

    INSERT INTO ShippingCost (WebOrder, Zone, NetAmount, EnteredWeight, BilledWeight,      Carrier)
SELECT WebOrder, Zone, NetAmount, EnteredWeight, BilledWeight, Carrier
FROM

    (select  substring([Package Reference Number 5],4,9) as WebOrder
             , zone
             , sum(CAST([Net Amount] as Money)) as NetAmount
             , sum(CAST([Entered Weight] as decimal)) as EnteredWeight
             , sum(CAST([Billed Weight] as INT)) as BilledWeight
             , 'UPS' as Carrier

               from UpsStaging
               where [Package Reference Number 5] LIKE 'WEB%[^0-9]%'
               group by [Package Reference Number 5],zone
    )
4

2 回答 2

0

我认为您需要为派生表加上别名,即使您没有引用别名。在右括号后面加上一个“x”。

于 2013-05-20T20:06:09.183 回答
0

在子查询的末尾添加一个别名:

INSERT INTO ShippingCost (WebOrder, Zone, NetAmount, EnteredWeight, BilledWeight,      Carrier)
SELECT WebOrder, Zone, NetAmount, EnteredWeight, BilledWeight, Carrier
FROM
(select  substring([Package Reference Number 5],4,9) as WebOrder
 , zone
 , sum(CAST([Net Amount] as Money)) as NetAmount
 , sum(CAST([Entered Weight] as decimal)) as EnteredWeight
 , sum(CAST([Billed Weight] as INT)) as BilledWeight
 , 'UPS' as Carrier

   from UpsStaging
   where [Package Reference Number 5] LIKE 'WEB%[^0-9]%'
   group by [Package Reference Number 5],zone
) as [subq]

子查询有效吗?我不希望按行分组正常工作 - 相反,使用这个:GROUP BY substring([Package Reference Number 5],4,9), zone

于 2013-05-20T20:06:41.533 回答