Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 使用窗口功能统计销售额_Sql_Sql Server_Sql Server 2008 - Fatal编程技术网

Sql 使用窗口功能统计销售额

Sql 使用窗口功能统计销售额,sql,sql-server,sql-server-2008,Sql,Sql Server,Sql Server 2008,我有一个疑问: USE AdventureWorks2012; GO SELECT CustomerID,COUNT(*) AS CountOfSales, COUNT(CustomerID) OVER (PARTITION BY CountOfSales) Total, RANK() OVER(ORDER BY COUNT(*) DESC) AS Ranking, ROW_NUMBER() OVER(ORDER BY COUNT(CustomerID) DESC) AS Row, DENSE

我有一个疑问:

USE AdventureWorks2012;
GO
SELECT CustomerID,COUNT(*) AS CountOfSales,
COUNT(CustomerID) OVER (PARTITION BY CountOfSales) Total,

RANK() OVER(ORDER BY COUNT(*) DESC) AS Ranking,
ROW_NUMBER() OVER(ORDER BY COUNT(CustomerID) DESC) AS Row,
DENSE_RANK() OVER(ORDER BY COUNT(*)) AS DenseRanking
FROM Sales.SalesOrderHeader
GROUP BY CustomerID
ORDER BY COUNT(*) DESC;
这就得出了这个结果:

CustomerID  CountOfSales    Total   Ranking Row DenseRanking
11091   28  1   1   2   17
11176   28  1   1   1   17
11200   27  1   3   9   16
11331   27  1   3   12  16
11287   27  1   3   5   16
11262   27  1   3   8   16
11276   27  1   3   11  16
11330   27  1   3   6   16
11711   27  1   3   13  16
11277   27  1   3   3   16
但现在我想有一个类似于total Countof Sales的列,所以:2加28,11加27

所以我的问题是如何管理


谢谢你

如果我正确理解了你的问题,你可以将你的查询包装成一个子查询,并通过分区进行计数(*):

SELECT *, COUNT(*) OVER (PARTITION BY CountOfSales) AS total_countOfSales
FROM (
SELECT CustomerID,COUNT(*) AS CountOfSales,
COUNT(CustomerID) OVER (PARTITION BY CountOfSales) Total,

RANK() OVER(ORDER BY COUNT(*) DESC) AS Ranking,
ROW_NUMBER() OVER(ORDER BY COUNT(CustomerID) DESC) AS Row,
DENSE_RANK() OVER(ORDER BY COUNT(*)) AS DenseRanking
FROM Sales.SalesOrderHeader
GROUP BY CustomerID
) a

MySQL真的支持那些windows功能吗?11加27背后的逻辑是什么?因为只有9条记录的id为27。这只是查询结果的一部分。你的问题没有任何意义。。。当你用身份证要一笔钱的时候?COUNT()OVER(按Id分区)只希望有CountOfsales的总数。我的意思很清楚。