根据全年的季节,我的业务销售经历了一个非常独特的年度周期。我无法了解每周或每个月的增长情况。我真正需要的是一个显示过去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
现在,我需要的是能让我获得第四列的代码,#34;累积年份"考虑到当前周,加上所有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
请帮助: - )
答案 0 :(得分:0)
我使用了一个子查询,它可以获得从给定年份到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