繁体   English   中英

MYSQL 按同一工作日比较上一年

[英]MYSQL Comparing previous year by same week day

我有一系列的销售交易。 我想创建一个 MySql 查询,该查询将按日期对销售交易进行分组,但也有一列显示最近工作日上一年的销售额。

我知道我可以使用 DATE_ADD 来获取正确的日期。 例如,下面的公式将给出我想要的与 2020 年 3 月 1 日相关的日期:

DATE_ADD('2020-03-01', INTERVAL -52 WEEK)

以上返回2019年3月3日。但我不知道如何将数据变成这样:

+-----------+---------+
|Date       | Sales   |
|-----------+---------+
|2020-03-01 | 45.00   |
|2020-03-01 | 1.23    |
|2020-03-01 | 30.00   |
|2020-03-01 | 5.75    |
|2020-03-01 | 25.63   |
|2020-02-29 | 85.85   |
|2020-02-29 | 26.23   |
|2020-02-29 | 56.85   |
|2020-02-29 | 8.96    |
|2020-02-29 | 89.48   |
.
.
.
.
|2019-03-03 | 3.50    |
|2019-03-03 | 76.89   | 
|2019-03-03 | 1003.50 |
|2019-03-03 | 1.34    |
|2019-03-02 | 6.58    |
|2019-03-02 | 90.48   | 
|2019-03-02 | 32.12   |
|2019-03-02 | 45.89   |
|2019-03-02 | 353.21  |
|2019-03-02 | 3.71    | 
|2019-03-02 | 22.22   |
|2019-03-02 | 353.65  |
.
.
.
.
+-----------+---------+

变成这样的表:

+-----------+--------------+--------------+
|Date       | Curr Sales   | Last Yr Sales|
|-----------+--------------+--------------+
|2020-03-01 | 107.61       | 1085.23      |
+-----------+--------------+--------------+
|2020-02-29 | 267.37       | 907.86       |
+-----------+--------------+--------------+
.
.
.

数据包含可追溯到 2015 年的一年中几乎每一天的销售额,而不仅仅是 2020 年 3 月 1 日。因此返回的表将包含多行日期

select t1.date, t1.Sales as 'Curr Sales', t2.sales as 'Last Yr Sales'
from(
    SELECT Date, Sum(Sales) as sales
    from table1 
    group by date
) t1
left join(
    SELECT Date, Sum(Sales) as sales
    from table1 
    group by date
) t2
on DATE_SUB(t1.date, INTERVAL 52 WEEK) = t2.date
where t1.date >= '2020-01-01'
order by t1.date desc

一种选择是条件聚合:

select
    concat(date_format(current_date, '%Y'), '-', date_format(date, '%m-%d')) date,
    sum(case when date > current_date - interval 1 year then sales end) current_year_sales,
    sum(case when date <= current_date - interval 1 year then sales end) last_year_sales
from mytable
where date > current_date - interval 2 year
group by date_format(date, '%m-%d')
order by date_format(date, '%m-%d')

这将为您提供过去 365 天的每日销售额,以及上一年同一天的相应销售额。

这会将今年的所有日期与去年的收入进行比较,以工作日为基准

我使用了这个MySQL - select 日期的算法形成上一年相应的工作日

SELECT `Date`,ROUND(SUM(`Sales`),2) SUmthisyear,(SELECT ROUND(SUM(IF(`Date` = DATE(DATE_SUB(t1.`Date`, INTERVAL 1 YEAR)) + INTERVAL IF ( WEEKDAY(t1.`Date`).= WEEKDAY( DATE(DATE_SUB(t1,`Date`,INTERVAL 1 YEAR)) + INTERVAL 1 DAY ), 2, 1 ) DAY,`Sales`,0) ),2) FROM table1) SUmpreviousyear FROM table1 t1 WHERE YEAR(`Date`) = YEAR(NOW()) GROUP BY `Date` ORDER BY `Date`
 CREATE TABLE table1 ( `Date` DATE, `Sales` FLOAT ); INSERT INTO table1 (`Date`, `Sales`) VALUES ('2020-03-01', '45.00'), ('2020-03-01', '1.23'), ('2020-03-01', '30.00'), ('2020-03-01', '5.75'), ('2020-03-01', '25.63'), ('2020-02-29', '85.85'), ('2020-02-29', '26.23'), ('2020-02-29', '56.85'), ('2020-02-29', '8.96'), ('2020-02-29', '89.48'), ('2019-03-03', '3.50'), ('2019-03-03', '76.89'), ('2019-03-03', '1003.50'), ('2019-03-03', '1.34'), ('2019-03-02', '6.58'), ('2019-03-02', '90.48'), ('2019-03-02', '32.12'), ('2019-03-02', '45.89'), ('2019-03-02', '353.21'), ('2019-03-02', '3.71'), ('2019-03-02', '22.22'), ('2019-03-02', '353.65');
  ✓ ✓
 SELECT `Date`,ROUND(SUM(`Sales`),2) SUmthisyear,(SELECT ROUND(SUM(IF(`Date` = DATE(DATE_SUB(t1.`Date`, INTERVAL 1 YEAR)) + INTERVAL IF ( WEEKDAY(t1.`Date`).= WEEKDAY( DATE(DATE_SUB(t1,`Date`,INTERVAL 1 YEAR)) + INTERVAL 1 DAY ), 2, 1 ) DAY,`Sales`,0) ),2) FROM table1) SUmpreviousyear FROM table1 t1 WHERE YEAR(`Date`) = YEAR(NOW()) GROUP BY `Date` ORDER BY `Date`
 日期 | 今年夏天 | 上一年:--------- |  ----------: |  --------------: 2020-02-29 |  267.37 |  907.86 2020-03-01 |  107.61 |  1085.23

db<> 在这里摆弄

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM