MySQL查询,获取月度数据差异

MySQL查询,获取月度数据差异,mysql,Mysql,我想要一个查询,让我每月的数据变化比较上个月 例如:1月的新加入者为3,2月的新加入者为2,因此与2月的1月相比-1月加入,因此查询应输出我: select * from new_joiner; +------+--------------+ | id | date_of_join | +------+--------------+ | 1 | 2020-01-10 | | 2 | 2020-01-02 | | 3 | 2020-01-05 | | 4 |

我想要一个查询,让我每月的数据变化比较上个月

例如:1月的新加入者为3,2月的新加入者为2,因此与2月的1月相比-1月加入,因此查询应输出我:

select * from new_joiner;
+------+--------------+
| id   | date_of_join |
+------+--------------+
|    1 | 2020-01-10   |
|    2 | 2020-01-02   |
|    3 | 2020-01-05   |
|    4 | 2020-02-10   |
|    5 | 2020-02-11   |
|    6 | 2020-07-11   |
|    7 | 2020-07-11   |
|    8 | 2020-07-11   |
|    9 | 2020-07-11   |
|   10 | 2020-07-11   |
|   11 | 2020-05-01   |
|   12 | 2020-05-02   |
|   13 | 2020-05-03   |
|   14 | 2020-05-04   |
|   15 | 2020-05-05   |
|   16 | 2020-05-05   |
|   17 | 2020-05-06   |
+------+--------------+


select   MONTHNAME(date_of_join) as MONTHNAME, 
         count(id) as JOINEE 
from     new_joiner 
where    MONTH(date_of_join)>=1 
group by MONTH(date_of_join);
+-----------+--------+
| MONTHNAME | JOINEE |
+-----------+--------+
| January   |      3 |
| February  |      2 |
| May       |      7 |
| July      |      5 |
+-----------+--------+

忽略一月,因为它没有前一个月,假设我们只有给定年份的数据,比如2020年。需要从2月到12月的所有月份的数据。

假设您有每个月的数据,您可以使用lag:

请注意,如果充满危险,则使用月份而不是年份。此外,格式正确日期的月份应大于1

所有这些都表明了一个更类似于:

select MONTHNAME(date_of_join) as MONTHNAME, 
       count(id) as JOINEE,
       (count(*) - lag(count(*)) over (order by  min(date_of_join)) as diff
from new_joiner 
where MONTH(date_of_join) >= 1 
group by MONTH(date_of_join);

使用相关子查询获取上个月的加入人数并将其减去:

select *
from (select MONTHNAME(date_of_join) as MONTHNAME, 
             count(id) as JOINEE,
             (count(*) - lag(count(*)) over (order by  min(date_of_join)) as diff,
             min(date_of_join) as min_date_of_join
      from new_joiner 
      where date_of_join >= '2020-01-01' and date_of_join < '2021-01-01'
      group by MONTH(date_of_join)
     ) t
where diff is not null
order by min_date_of_join;

您的MySql版本是什么?@forpas it MySql 5.6四月没有行。因此,五月你可以计算五月和二月之间的差异。这就是你想要的吗?Hi@forpas假设我们每个月都有数据。嗨,我试过了,没有一个有效。我使用的是MySQL 5.6.x这些都需要MySQL 8+。Hi@forpas您的查询看起来很好,需要做一些小改动,因为我们没有3月和4月的任何数据,所以它也应该显示这些月的数据。我的意思是我们需要从2月到12月的数据。这是我在你的问题下提出的一个问题的答案:假设我们每个月都有数据
select *
from (select MONTHNAME(date_of_join) as MONTHNAME, 
             count(id) as JOINEE,
             (count(*) - lag(count(*)) over (order by  min(date_of_join)) as diff,
             min(date_of_join) as min_date_of_join
      from new_joiner 
      where date_of_join >= '2020-01-01' and date_of_join < '2021-01-01'
      group by MONTH(date_of_join)
     ) t
where diff is not null
order by min_date_of_join;
SELECT 
  t.monthname,
  joinee - (SELECT COUNT(*) FROM new_joiner WHERE MONTH(date_of_join) = t.month - 1) JOINEE_DIFF 
FROM (
  SELECT MONTH(date_of_join) month, MONTHNAME(date_of_join) monthname, 
         COUNT(id) joinee 
  FROM new_joiner 
  GROUP BY month, monthname
) t 
WHERE t.month > 1;