【问题标题】:SQL How to combine two tables into oneSQL如何将两张表合二为一
【发布时间】:2017-07-04 17:14:38
【问题描述】:

我在尝试使用当前表 (Table1) 中的一些列并结合第二个表 (Table2) 中的新列在 SQL (Table3) 中创建新表时遇到以下错误:“找不到任何一列“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

【问题讨论】:

    标签: sql sql-server


    【解决方案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] 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-21
      • 1970-01-01
      • 1970-01-01
      • 2019-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多