我有一些带分区功能的代码,但是没有用。
我收到一条错误消息,提示
“销售”附近的语法不正确
有人知道为什么吗?我看着其他分区问题,没有找到答案,
下面的代码应该从“汇总的销售历史”表中选择PriceZoneID和Sales,然后使用OVER函数汇总总销售额,并将该数据放在一个名为Total Sales的新列中。
然后应在新的名为TotalSalesByZone的列中使用OVER(PARTITION)表达式来汇总每个区域的销售额,然后按价格区域ID和销售额排序数据
Select PriceZoneID,
Sales,
SUM(Sales) OVER () AS Total Sales,
SUM(Sales) OVER (PARTITION BY PriceZoneID) AS TotalSalesByZone
From AggregatedSalesHistory
ORDER BY PriceZoneID AND Sales;
(分区依据将结果划分为分区,例如区域)
如果您可以以正确的答案发布代码,将不胜感激!
答案 0 :(得分:3)
现在从注释中出来,因为纠正那里的错误变得有点傻。您的代码中有1个打字错误和1个语法错误:
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