在MySQL中计算运行总数我有一个MySQL查询:SELECT DAYOFYEAR(`date`) AS d, COUNT(*) FROM `orders` WHERE `hasPaid` > 0GROUP BY dORDER BY d返回如下内容:d | COUNT(*) |20 | 5 |21 | 7 |22 | 12 |23 | 4 |我真正想要的是在结尾的另一篇专栏文章,以显示正在运行的总数:d | COUNT(*) | ??? |20 | 5 | 5 |21 | 7 | 12 |22 | 12 | 24 |23 | 4 | 28 |这个是可能的吗?
3 回答
杨魅力
TA贡献1811条经验 获得超6个赞
SET @runtot:=0;SELECT q1.d, q1.c, (@runtot := @runtot + q1.c) AS rtFROM (SELECT DAYOFYEAR(`date`) AS d, COUNT(*) AS c FROM `orders` WHERE `hasPaid` > 0 GROUP BY d ORDER BY d) AS q1
桃花长相依
TA贡献1860条经验 获得超8个赞
SELECT DAYOFYEAR(O.`date`) AS d, COUNT(*), (select count(*) from `orders` where DAYOFYEAR(`date`) <= d and `hasPaid` > 0)FROM `orders` as OWHERE O.`hasPaid` > 0GROUP BY dORDER BY d
神不在的星期二
TA贡献1963条经验 获得超6个赞
SELECT dayofyear(`date`) AS d, count(*), sum(count(*)) OVER (ORDER BY dayofyear(`date`))FROM `orders`WHERE `hasPaid` > 0GROUP BY dORDER BY d
count(*)
sum(..) OVER (..)
WITH
WITH daily (d, c) AS ( SELECT dayofyear(`date`) AS d, count(*) FROM `orders` WHERE `hasPaid` > 0 GROUP BY d)SELECT d, c, sum(c) OVER (ORDER BY d)ORDER BY d
添加回答
举报
0/150
提交
取消