2015-10-01 140 views
2

我试图计算用户在连续几个月执行某些操作时的发生次数。防爆。MySQL:计数连续月份

| 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   | 

任何推荐或示例能帮助我?

回答

0

您可能能够做这样的事情:

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 | 

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

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 

而且类似这样的s应该总结起来

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

我还没试过。

0

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

  1. 找到一种方法从日期中删除日期和时间戳部分。所以它应该只有一个月和一年(如201501)。称它为curr_month。
  2. 创建另一个临时表并添加另一个列,将1个月添加到新的month_year。说它next_month
  3. 现在做一个自我加入这个临时表上idcurr_month=next_month
  4. 这会给你这是在有连续几个月的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 
    
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

这看起来不错,但我做了不同时间的测试,它不返回任何记录。我想也许我没有解释清楚。它应该只检查月份,不检查时间或日期,只检查是否有连续2个月以上的事情发生(如果连续3个月,显示3 ...)谢谢。 –

+0

@IgorO:我更新了答案和小提琴。这应该如你所愿。 – DarkKnight