MySQL: Count of records with consecutive months

有些话、适合烂在心里 提交于 2019-12-01 05:22:33

You can do this in MySQL using variables (or very complicated correlated subqueries). In other databases, you would use window/analytic functions.

The logic is:

  1. Get one row per month and person with a purchase.
  2. Use variables to assign each group of consecutive months a "grouping" value.
  3. Aggregate by the person and the "grouping" value.

Here is a query that has been tested on your SQL Fiddle:

select person, count(*) as numMonths
from (select person, ym, @ym, @person,
             if(@person = person and @ym = ym - 1, @grp, @grp := @grp + 1) as grp,
             @person := person,
             @ym := ym
      from (select distinct person, year(purdate)*12+month(purdate) as ym
            from records r
           ) r cross join
           (select @person := '', @ym := 0, @grp := 0) const
      order by 1, 2
     ) pym
group by person, grp;
Jeffrey Linwood

I used this StackOverflow answer for guidance (Check for x consecutive days - given timestamps in database)

SELECT a.person, COUNT(1) AS consecutive_months
FROM
(

  SELECT a.person, IF(b.YearMonth IS NULL, @val:=@val+1, @val) AS consec_set
  FROM (
    SELECT DISTINCT person, EXTRACT(YEAR_MONTH from purdate) as YearMonth from records
    ) a
  CROSS JOIN (SELECT @val:=0) var_init
  LEFT JOIN (SELECT DISTINCT person, EXTRACT(YEAR_MONTH from purdate) as YearMonth from records) b ON
      a.person = b.person AND
      a.YearMonth = b.YearMonth + 1
   ) a
GROUP BY a.consec_set
HAVING COUNT(1) >= 2    

Here is the SQLFiddle - http://sqlfiddle.com/#!2/cc5c3/55

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!