如何以其他方式离开加入列

时间:2017-03-06 17:11:13

标签: sql sql-server

我想将右列映射到左侧,但我在select语句中收到错误,因为'select'附近的语法不正确。期待ID

create table [AdventureWorks2014].[abc] as
select a.*,
        b.*
from [Production].[Product] a
left join(
select distinct ProductID,Shelf
from [Production].[ProductInventory]
)b
on a.ProductID = b. ProductID;

1 个答案:

答案 0 :(得分:4)

尝试使用SELECT INTO

select p.*, pi.shelf
into [AdventureWorks2014].[abc]
from [Production].[Product] p left join
     (select distinct ProductID, Shelf
      from [Production].[ProductInventory] pi
     ) pi
     on p.ProductID = pi.ProductID;

注意:

  • 我不知道SQL Server支持CREATE TABLE AS
  • 表中的列需要具有唯一的名称。如果您使用*,则会ProductId两次。
  • 表名的缩写是最佳命名约定;应避免使用无意义的字母,例如ab