SELECT MySQL com DATE MIN MAX GROUP BY HAVING

Filtra nome de usuário, nome de serviço, data da primeira e ultima
reincidencia e total de reincidencias num intervalo de dias para um
determinado serviço agrupando serviço e usuário tendo mais de 1
reincidencia ordenado por usuário em ordem crescente com limite de
10000 linhas

SELECT t.customer_user_id AS 'usuario',
                s.name AS 'nome do servico',
                MIN(t.create_time) AS 'primeira reincidencia',
                MAX(t.create_time) AS 'ultima reincidencia',
                COUNT(*) AS reincidencias
         FROM ticket t, service s
         WHERE DATE( t.create_time ) >= DATE_SUB(CURDATE(),INTERVAL ? DAY)
         AND s.id=t.service_id
         GROUP BY t.customer_user_id, t.service_id
         HAVING reincidencias > 1
         ORDER BY t.customer_user_id ASC LIMIT 10000
Advertisements

One thought on “SELECT MySQL com DATE MIN MAX GROUP BY HAVING

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s