Mysql
 sql >> Baza danych >  >> RDS >> Mysql

Zliczaj aktywnych użytkowników na podstawie logowań z ostatnich 90 dni

Takie problemy są łatwiejsze do rozwiązania, jeśli masz tabelę kalendarza ze wszystkimi potrzebnymi datami. Jeśli nie masz takiej tabeli, możesz ją utworzyć za pomocą zapytania takiego:

create table `calendar` (
    `date` DATE NOT NULL,
    PRIMARY KEY (`date`)
)  
    select DATE_ADD('1900-01-01',INTERVAL t4.c*10000 + t3.c*1000 + t2.c*100 + t1.c*10 + t0.c DAY) as `date`
    from 
    (select 0 c union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) t0,
    (select 0 c union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) t1,
    (select 0 c union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) t2,
    (select 0 c union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) t3,
    (select 0 c union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) t4

Spowoduje to utworzenie tabeli z datami od 1900-01-01 do 2173-10-15 (100K dni) i zużyje tylko około 2,5 MB. Możesz dostosować go do swoich potrzeb.

Korzystając z tabeli kalendarzowej, możesz uzyskać trzymiesięczne przedziały:

select 
    DATE_FORMAT(date_sub(c.date, INTERVAL 1 day), '%Y-%m') as month,
    date_sub(c.date, INTERVAL 3 month) as first_day,
    date_sub(c.date, INTERVAL 1 day)   as last_day
from calendar c
where day(c.date) = 1
  and c.date between '2015-02-01' and '2015-09-01'

Wynik:

| month   | first_day  | last_day   |
| 2015-01 | 2014-11-01 | 2015-01-31 |
| 2015-02 | 2014-12-01 | 2015-02-28 |
| 2015-03 | 2015-01-01 | 2015-03-31 |
| 2015-04 | 2015-02-01 | 2015-04-30 |
| 2015-05 | 2015-03-01 | 2015-05-31 |
| 2015-06 | 2015-04-01 | 2015-06-30 |
| 2015-07 | 2015-05-01 | 2015-07-31 |
| 2015-08 | 2015-06-01 | 2015-08-31 |

Dostosuj to, jeśli naprawdę chcesz użyć około 90-dniowych odstępów.

Teraz wystarczy proste lewe sprzężenie z tabelą logowania, aby uzyskać to, czego chcesz:

select i.month as `Date`, count(distinct l.user_id) as `Active users`
from (
    select 
        date_format(date_sub(c.date, interval 1 day), '%Y-%m') as month,
        date_sub(c.date, interval 3 month) as first_day,
        date_sub(c.date, interval 1 day)   as last_day
    from calendar c
    where day(c.date) = 1
      and c.date between '2015-02-01' and '2015-09-01'
) i
left join login_table l on l.login_date between i.first_day and i.last_day
group by i.month

http://sqlfiddle.com/#!9/d1bb0/3



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. mysql odrębne wartości bez pustego ciągu i NULL

  2. Jak sprawdzić, czy ciąg jest poprawną DATĄ, CZASEM lub DATETIME?

  3. mysql, zapytanie z to where condation, jeśli jedna wartość =cokolwiek

  4. Jak znaleźć największą tabelę w bazie danych MySQL?

  5. Dynamiczne tworzenie i zapisywanie pól formularza w bazie danych php mysql