0
我有这个表分区OVER场另一场
ID UPC Sales Date
1 333 10 1/1/2015
1 222 20 1/1/2015
1 111 30 1/1/2015
1 444 10 2/1/2015
1 555 20 2/1/2015
2 333 20 1/1/2015
2 222 50 1/1/2015
2 111 30 1/1/2015
2 444 20 2/1/2015
2 555 20 2/1/2015
而且希望这个输出
我已经试过这
SELECT ID, AVG(Sales) OVER (Partition BY Date) as basket_size
FROM Transactions
GROUP BY ID
但我得到这个错误分组时:
Column 'Transactions.Date' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
想法?
这工作。谢谢TT – user2502836
@ user2502836思考了一会儿,我想出了一个更简单的查询:):SELECT ID,SUM(Sales)AS sum,SUM(Sales)/ COUNT(DISTINCT [Date])AS [avg] ,COUNT(DISTINCT [Date])AS [count] FROM @t GROUP BY ID;'。我相信这对你的要求也是正确的,只是简单得多。 –