SQL - 尝试使用Statement创建表时出现ID错误

时间:2015-12-03 00:20:39

标签: sql sql-server

我正在尝试创建一个表但是它给了我一个错误,说它是Expecting ID。谁能告诉我这是什么问题?

我的查询如下:

Create Table ForecastSnapshot as

(
SELECT x.* 

FROM
(
(
SELECT [Opportunity ID] 
      ,[Last Modified Date]
      ,[Close Date] 
      ,[Stage] 
      ,[ARR] 
      ,[TCM] 
      ,[SYSTEMMODSTAMP] 
      ,'2015-07-06' as [Snapshot Date] 


  FROM table1


  ) x

RIGHT JOIN (

SELECT  max([SYSTEMMODSTAMP]) as [SYSTEMMODSTAMP], [Opportunity ID]
FROM table1
WHERE [SYSTEMMODSTAMP] < '2015-07-06'                                                         
GROUP BY [Opportunity ID]

) as y 

ON x.[Opportunity ID] = y.[Opportunity ID] AND x.[SYSTEMMODSTAMP] = y.[SYSTEMMODSTAMP] 

1 个答案:

答案 0 :(得分:1)

您需要的是SELECT ... INTO声明:

SELECT x.* INTO ForecastSnapshot 
FROM
(
SELECT [Opportunity ID] 
  ,[Last Modified Date]
  ,[Close Date] 
  ,[Stage] 
  ,[ARR] 
  ,[TCM] 
  ,[SYSTEMMODSTAMP] 
  ,'2015-07-06' as [Snapshot Date] 
  FROM table1
  ) x

RIGHT JOIN (

SELECT  max([SYSTEMMODSTAMP]) as [SYSTEMMODSTAMP], [Opportunity ID]
FROM table1
WHERE [SYSTEMMODSTAMP] < '2015-07-06'                                                         
GROUP BY [Opportunity ID]

) as y 

ON x.[Opportunity ID] = y.[Opportunity ID] AND x.[SYSTEMMODSTAMP] = y.[SYSTEMMODSTAMP]