2017-07-17 107 views
1

我試圖爲每條記錄添加百分位值作爲新列。但我在我的SQL查詢中出現錯誤。任何人都可以請幫忙解決它。SQL Server查詢中的錯誤 - 內部連接

錯誤消息: 消息156,級別15,狀態1,行3 關鍵字'內部'附近的語法不正確。 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] 
+2

可能會將'where [country] ='AUSTRALIA''放在您的線路下方ON A. [Country] = b。[Country]'? –

+0

你應該查詢查詢的基本語法。然後你會意識到where子句完全在錯誤的地方。 –

回答

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' 
+0

非常感謝。這工作 –