Sum over partition not working(分区总和不起作用)
问题描述
我有一些关于分区函数的代码,但它不起作用.
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'
有人知道为什么吗?看了其他分区问题,没找到答案,
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) 表达式总结每个区域的销售额,然后按价格区域 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)
如果您能发布带有正确答案的代码,将不胜感激!
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
这篇关于分区总和不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:分区总和不起作用
基础教程推荐
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- 在 SQL 中连接多个表 2021-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
- 是否可以执行按位分组功能? 2021-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- 无法解决整理冲突 2021-01-01
