問題描述
我有一些關于分區(qū)函數的代碼,但它不起作用.
I've got some code with the partition function, but it's not working.
我收到一條錯誤消息,內容為
I get an error message that says
'Sales' 附近的語法不正確
Incorrect syntax near 'Sales'
有人知道為什么嗎?看了其他分區(qū)問題,沒找到答案,
Does anyone know why? I looked at the other partition questions, didn't find an answer,
代碼(下面)應該從 Aggregated Sales History 表中選擇 PriceZoneID 和 Sales,然后使用 OVER 函數總結總銷售額,并將該數據放入名為 Total Sales 的新列中.
The code (below) is supposed to select PriceZoneID and Sales from the Aggregated Sales History table then sum up the total sales using the OVER function and put that data in a new column called Total Sales.
然后應該在名為 TotalSalesByZone 的新列中使用 OVER (PARTITION) 表達式總結每個區(qū)域的銷售額,然后按價格區(qū)域 ID 和銷售額對數據進行排序
It should then sum up the sales for each zone using the OVER (PARTITION) expression in a new column called TotalSalesByZone then order the data by Price Zone ID and Sales
Select PriceZoneID,
Sales,
SUM(Sales) OVER () AS Total Sales,
SUM(Sales) OVER (PARTITION BY PriceZoneID) AS TotalSalesByZone
From AggregatedSalesHistory
ORDER BY PriceZoneID AND Sales;
(Partition By 將結果劃分為 Partitions,例如 Zones)
(Partition By divides the result into Partitions eg Zones)
如果您能發(fā)布帶有正確答案的代碼,將不勝感激!
If you could post the code with the correct answer, it would be greatly appreciated!
推薦答案
現在退出評論,因為糾正那里的錯誤有點愚蠢.您的代碼中有 1 個印刷錯誤和 1 個語法錯誤:
Coming out of the comments now, as it's getting a little silly to correct the errors in there. There is 1 typograhical error in your code, and 1 syntax error:
Select PriceZoneID,
Sales,
SUM(Sales) OVER () AS Total Sales, --There's a space in the alias
SUM(Sales) OVER (PARTITION BY PriceZoneID) AS TotalSalesByZone
FROM AggregatedSalesHistory
ORDER BY PriceZoneID AND Sales; --AND is not valid in an ORDER BY clause
正確的查詢是:
Select PriceZoneID,
Sales,
SUM(Sales) OVER () AS TotalSales, --Removed Space
SUM(Sales) OVER (PARTITION BY PriceZoneID) AS TotalSalesByZone
FROM AggregatedSalesHistory
ORDER BY PriceZoneID, Sales; --Comma delimited
這篇關于分區(qū)總和不起作用的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!