2016-01-18 9 views
6

Sto lavorando a una query in questo momento che conterrà il numero di giorni di accesso unici per utente, inserirli in un bucket (1-2, 3-4 giorni di accesso ecc.) E restituire il numero di ciascun utente in ciascun bucket per dipartimento. Mi scuso se questo non è troppo chiaro, e si spera che gli esempi di seguito illustrino la mia domanda.MySQL - Raggruppa per numero di utenti entro intervalli di giorni di accesso unici e per dipartimento

Ho una tabella come questa:

+-------------+-----------+------------+-----------------+ 
| time  | user_name | dept |  event  | 
+-------------+-----------+------------+-----------------+ 
| 2016-01-04 | Joe  | finance | logged in  | 
| 2016-01-04 | Jeff  | marketing | logged in  | 
| 2016-01-04 | Jeff  | marketing | project created | 
| 2016-01-04 | Bob  | finance | logged in  | 
| 2016-01-04 | Mark  | accounting | logged in  | 
| 2016-01-05 | Bob  | finance | logged in  | 
| 2016-01-08 | Bob  | finance | logged in  | 
| 2016-01-09 | Jeff  | marketing | logged in  | 
| 2016-01-10 | Jeff  | marketing | logged in  | 
| 2016-01-11 | Nate  | accounting | logged in  | 
| 2016-01-11 | Nate  | accounting | project created | 
+-------------+-----------+------------+-----------------+ 

voglio restituire una tabella come questa:

+------------------+-----------------+------------+ 
| number of logins | number of users | dept | 
+------------------+-----------------+------------+ 
| 1-2    |    1 | finance | 
| 3-4    |    1 | finance | 
| 5+    |    0 | finance | 
| 1-2    |    0 | marketing | 
| 3-4    |    1 | marketing | 
| 5+    |    0 | marketing | 
| 1-2    |    2 | accounting | 
| 3-4    |    0 | accounting | 
| 5+    |    0 | accounting | 
+------------------+-----------------+------------+ 

A partire da ora, la mia domanda si presenta come:

select 
(case when count(distinct(`time`)) between 1 and 2 then '1-2' 
     when count(distinct(`time`)) between 3 and 4 then '3-4' 
     else '5+' 
     end) as buckets, dept, user_name 
    from change_log where event in ('logged in') 
    group by dept, user_name 

Tuttavia, questo sta restituendo una tabella come di seguito, che è il più vicino possibile a quello che voglio, ma non sono sicuro di come farlo rotolare secchi e reparto.

+---------+------------+-----------+ 
| buckets | dept | user_name | 
+---------+------------+-----------+ 
| 1-2  | accounting | Mark  | 
| 1-2  | accounting | Nate  | 
| 3-4  | finance | Bob  | 
| 1-2  | finance | Joe  | 
| 3-4  | marketing | Jeff  | 
+---------+------------+-----------+ 

risposta

2

Qualcosa di simile?

select buckets, dept, count(user_name) no_of_u from 
(select 
(case when count(distinct(`time`)) between 1 and 2 then '1-2' 
     when count(distinct(`time`)) between 3 and 4 then '3-4' 
     else '5+' 
     end) as buckets, dept, user_name 
    from change_log where event in ('logged in') 
    group by dept, user_name) 
group by buckets, dept 
+0

Questo sembra funzionare! Ho ottenuto un "Ogni tabella derivata deve avere il proprio alias". messaggio di errore ma dopo aver dato un alias alla tabella derivata dalla seconda istruzione select ha funzionato. Molte grazie! Sto ancora cercando di sentirmi a mio agio con le istruzioni select annidate. – user5805996

+0

Li uso se davvero devo davvero. Nella maggior parte dei casi ho più stacks select annidati e mi confondo. In quei casi cerco di creare tabelle temporanee e usarle per ottenere il mio risultato finale. – Nomeaning25

-1

Giusto per essere sicuro di capire, si sta fondamentalmente cercando di fare questo:

select 
    (case when count(distinct(`time`)) between 1 and 2 then '1-2' 
      when count(distinct(`time`)) between 3 and 4 then '3-4' 
      else '5+' 
    end) as buckets, dept, count(*) as `number of users` 
from change_log where event in ('logged in') 
group by dept, buckets 

Ora, non è possibile farlo in MySQL, perché non è possibile raggruppare sull'uscita del una dichiarazione di un caso, ma funzionalmente questo è il risultato desiderato?

0

Ma si può fare in questo modo:

select 
    ELT(LEAST(count(*),5), '1-2', '1-2', '3-4', '3-4','5+') as buckets, 
    dept, 
    count(*) as `number of users` 
FROM change_log where event in ('logged in') 
WHERE event in ('logged in') 
GROUP BY 
    dept, 
    ELT(LEAST(count((*),5), '1-2', '1-2', '3-4', '3-4','5+'); 
Problemi correlati