SQL如何将两个表合并为一个

时间:2017-07-04 17:14:39

标签: sql sql-server

尝试使用当前表中的某些列(Table1)并使用第二个表中的新列(Table2)组合在SQL(Table3)中创建新表时,出现以下错误:&#34 ;找不到任何一列" a"或用户定义的函数或聚合" a.sum",或名称不明确。"

代码如下:

 select a. [Quarter]
  ,a. [Source Location]     
  ,a. [Destination Location]            
  ,a. sum([Total Volume]) as 'Total Volume' 
  ,a. sum([Total Cost]) as 'Total Cost' 
  ,b. [Source_City] 
  ,b. [Source_State]    
   Into [2016].[dbo].[Table3]
   FROM [2016].[dbo].[Table1] a
  ,[2016].[dbo].[Table2] b

  Where a. [Source Location]  = b. [Source_Location]

  Group By a. [Quarter] 
  ,a. [Source Location]     
  ,a. [Destination Location]            
  ,b. [Source_City] 
  ,b. [Source_State]

错误参考[Quarter];但是,[Quarter]是表1中定义的列。另外,[Source_Location]是表2中定义的列。

任何人都可以协助解决此问题,并成功将这两个表合并为第三个表吗?

感谢您的帮助!

GK

1 个答案:

答案 0 :(得分:1)

我不打算对格式的其他方面发表评论,但问题是您使用sum()。你有:

 a.sum([Total Volume]) as 'Total Volume',
 a.sum([Total Cost]) as 'Total Cost' 

正确的语法是:

 sum(a.[Total Volume]) as [Total Volume],
 sum(a.[Total Cost]) as [Total Cost]