gpt4 book ai didi

mysql - 在 MySQL 中创建最近 "x"天的累积循环 SUM

转载 作者:行者123 更新时间:2023-11-30 21:45:58 25 4
gpt4 key购买 nike

我的业务销售经历了一个非常独特的年度周期,该周期基于全年的季节。我无法了解我每周或每月的成长情况。我真正需要的是显示过去 365 天的运行总计——每一天。当我跨越一整年时,我可以真正看到我的业务是如何增长的。

下面,我刚刚编了个数据来说明我的愿望。我已经有一个 SQL 查询,它给出了年、周和销售额。这是我的查询(它涉及一些连接,但它有效):

SELECT
YEAR(arrPurch.PurchaseDate) as YEAR,
Week(arrPurch.PurchaseDate) AS Week,
SUM(ROUND(arrPurch.Credits*1.85+arrPurch.Price,0)) AS `Sales`
FROM
arrangementPurchases AS arrPurch
INNER JOIN catalog_dev_arrangements AS catDEVarr ON arrPurch.ArrangementID = catDEVarr.ArrangementID
INNER JOIN members ON arrPurch.MemberID = members.MemberID
WHERE arrPurch.PurchaseDate >= '2015-01-04'
GROUP BY Year, Week
ORDER BY Year, Week

现在,我需要的是允许我获得第四列“累积年”的代码,其中考虑了当前周,加上所有 51 周的销售额。然后,每周,我都会清楚地了解我的销售增长状态,因为我知道每周累计都会考虑到我年度销售周期中的所有季节。

Year      Week         Sales           Cumulative Year
-------------------------------------------------------
2017 01 24,292 2,467,488
2017 02 32,838 2,471,433
2017 03 33,283 2,470,212

请帮忙:-)

最佳答案

我使用了一个子查询来获取从给定年份的一周到 51 周前的销售额总和。请参阅下面的示例演示

http://sqlfiddle.com/#!9/ce6dd0/1

select
YEAR(arrPurch.PurchaseDate) as YEAR,
Week(arrPurch.PurchaseDate) AS Week,
SUM(ROUND(arrPurch.Credits*1.85+arrPurch.Price,0)) AS `Sales`,
(select sum(ROUND(t.Credits*1.85+t.Price,0))
FROM arrangementPurchases AS t
INNER JOIN catalog_dev_arrangements AS catDEVarr ON t.ArrangementID = catDEVarr.ArrangementID
INNER JOIN members ON t.MemberID = members.MemberID
where YEARWEEK(t.purchaseDate)
between YEARWEEK(arrPurch.purchaseDate - INTERVAL 51 WEEK)
and YEARWEEK(arrPurch.purchaseDate)) as `Cumulative_Year`
FROM arrangementPurchases AS arrPurch
INNER JOIN catalog_dev_arrangements AS catDEVarr ON arrPurch.ArrangementID = catDEVarr.ArrangementID
INNER JOIN members ON arrPurch.MemberID = members.MemberID
WHERE arrPurch.PurchaseDate >= '2015-01-04'
GROUP BY Year, Week
ORDER BY Year, Week

关于mysql - 在 MySQL 中创建最近 "x"天的累积循环 SUM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49482050/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com