SQL Server查询中的错误 - 内部联接

时间:2017-07-17 15:58:16

标签: sql sql-server inner-join

我正在尝试将每个记录的Percentile值添加为新列。但我在SQL查询中收到错误。任何人都可以帮忙解决它。

错误讯息: Msg 156,Level 15,State 1,Line 3 关键字' inner'附近的语法不正确。 Msg 102,Level 15,State 1,Line 6 ' a'附近的语法不正确。

 Select b.* , a.[Rank_1]/count(b.[Date]) * 100 as Percentile from 
  [Country_table1$] b  where [Country] = 'AUSTRALIA' 
  inner join 
   (
 select [MSCI_Price_idx], [Country], rank() OVER (PARTITION BY [Country] 
   ORDER BY [MSCI_Price_idx] DESC) AS [Rank_1]
   from [Country_table1$] 
  GROUP BY [MSCI_Price_idx],[Country] 
  ) a
ON a.[Country] = b.[Country]  

1 个答案:

答案 0 :(得分:2)

您的where声明位置错误。联接正式成为from语句的一部分,因此符合标准。要让您的标准位于底部,请检查正确的表格,使用别名。

Select b.* , a.[Rank_1]/count(b.[Date]) * 100 as Percentile from 
  [Country_table1$] b
  inner join 
   (
 select [MSCI_Price_idx], [Country], rank() OVER (PARTITION BY [Country] 
   ORDER BY [MSCI_Price_idx] DESC) AS [Rank_1]
   from [Country_table1$] 
  GROUP BY [MSCI_Price_idx],[Country] 
  ) a
ON a.[Country] = b.[Country]  
  where b.[Country] = 'AUSTRALIA'