2015-10-01 142 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