MySQL:数连续几个月

时间:2015-10-01 04:04:27

标签: mysql sql date

我正在尝试计算用户在连续几个月内执行某些操作时的事件。实施例

| Person     | Datetime              | 
| person_01  | '2015-01-02 10:40:15' | 
| person_01  | '2015-02-02 10:40:15' | 
| person_01  | '2015-07-05 10:40:15' | 
| person_02  | '2015-01-02 10:40:15' | 
| person_02  | '2015-04-03 10:40:15' | 
| person_02  | '2015-07-09 10:40:15' | 

结果:

| Created    |  Consecutive  |
| person_01  |  1            |
| person_02  |  0            |

(我试图按年份(日期时间),月份(日期时间)开始,但是如果可能的话,我希望看到一些解决方案示例。)

有什么建议或例子可以帮助我吗?

4 个答案:

答案 0 :(得分:1)

您可以通过组合rownumber生成和timestampdiff

来实现此目的

SQL Fiddle Demo

SET @row_number1:=0; 

SET @row_number2:=0; 

SELECT T1.person, 
       ,SUM(CASE WHEN TIMESTAMPDIFF(MONTH,T1.ddatetime,T2.ddatetime)=1 THEN 1 ELSE 0 END) CNT CNT 
FROM   (SELECT @row_number1 := @row_number1 + 1 AS row_number, 
               person, 
               ddatetime 
        FROM   datedata 
        ORDER  BY person, 
                  ddatetime) T1 
       INNER JOIN 
       (SELECT @row_number2 := @row_number2 + 1 AS row_number, 
                          person, 
                          ddatetime 
                   FROM   datedata 
                   ORDER  BY person, 
                             ddatetime) T2 
               ON T1.row_number + 1 = T2.row_number 
                  AND T1.person = T2.person 
GROUP  BY T1.person 

答案 1 :(得分:0)

你可能会做这样的事情:

machine-name\INSTANCE

SQLFiddle:http://sqlfiddle.com/#!9/f1fed/6

答案 2 :(得分:0)

假设您的表名为table(我不太可能知道)

像这样的东西会列出所有

select base.person, count(subq.dt) consequtive
-- get distinct person
from (select distinct person from test) base
-- join with a subquery
left join

(
  select a.* 
  from 
    -- convert date to first day of the month
    (select person, 
      cast(concat(left(dt,7), '-01') as date) as dt 
    from test) a 

  join 

    -- convert date to first day of the month
    -- and subtract a month
    (select person, 
       date_sub(cast(concat(left(dt,7), '-01') as  date), interval 1 month) as dt 
     from test) b 

  -- join the above 2 by person and date
  on a.person = b.person and a.dt = b.dt
) subq

on subq.person = base.person
group by base.person;

Result:
| person | consequtive |
|--------|-------------|
|     01 |           1 |
|     02 |           0 |

这样的事情应该总结一下

select A.Person from table A join table B on A.Person = B.Person
where A.Datetime = date_add(B.Datetime, interval 1 day) order by A.Datetime

我还没试过。

答案 3 :(得分:0)

这是你必须使用的逻辑。我在oracle中尝试过,但逻辑也适用于mysql。你只需要找到相应的功能。

  1. 找到一种从日期中删除日期和时间戳部分的方法。所以它应该只有月份和年份(如201501)。称之为curr_month。
  2. 创建另一个临时表,并通过向新的month_year添加1个月来添加另一列。称之为next_month
  3. 现在在idcurr_month=next_month
  4. 上对此临时表进行自我加入
  5. 这将为您提供连续几个月的ID。按id和count(*)进行分组以获得结果。

                with tbl (id ,Month_year) as
                (select 1,'20150101' from dual union
                 select 1,'20150201' from dual union
                  select 1,'20150701' from dual union
                 select 2,'20150101' from dual union
                  select 2,'2015041' from dual union
                 select 2,'20150701' from dual
                 ),tbl2 as(
                 select tbl.*, to_date(Month_year,'YYYYMMDD') as curr_month, add_months( to_date(Month_year,'YYYYMMDD'),1) as next_month from tbl order by id,curr_month)
                 ,tbl3 as(
                 select t1.id from tbl2 t1 inner join
                 tbl2 t2 on
                 t1.id=t2.id
                 and t1.curr_month=t2.next_month)
                 select distinct id as created,count(*) as consecutive from tbl3 group by id