Query Esplora dati per % di argomenti risolti da un gruppo specifico di utenti

Ciao a tutti quelli che stanno usando Data Explorer!

(Scusate la premessa @michebs :smiley: - avrò solo altre due domande, spero che possano essere utili anche ad altri)

Ha mai creato qualcuno una query di Data Explorer per la percentuale di argomenti risolti da un gruppo specifico, raggruppati per mese e anno?

Il risultato sarebbe quindi simile a questo, ma l’ultima colonna conterrà la percentuale

Possiamo senz’altro realizzare qualcosa del genere, tutti i dati sono disponibili.

“Post risolti in percentuale”… cosa intendi esattamente.

  • Il post è stato creato a gennaio
  • Il gruppo “awesome” ha ottenuto una risposta accettata a febbraio
  • Altri 73 argomenti hanno ricevuto soluzioni a febbraio da altri gruppi e utenti.

Quindi immagino che il “gruppo awesome” abbia un tasso di risoluzione dell’1,35% per febbraio?

Immaginiamo che in un mese ci siano 100 soluzioni. Ci sono venti persone che appartengono a un determinato gruppo e hanno iniziato a contrassegnare le loro risposte come soluzioni, per un totale di venti. Desidero avere una query in cui specificherò l’ID del gruppo principale nello script e recupererò i dati per mese, mostrando per questo mese 20/100 - 20%.

1 Mi Piace

Ciao Konrad, scusa il ritardo.
Sto ancora validando alcune query, ma colgo l’occasione per confermare: è questo il risultato che stavi aspettando?

year month group_name tt_groups total %
2021 1 team1 40 70 57
2021 1 team2 30 70 43
1 Mi Piace

Ho cercato di rendere le query piĂš dettagliate, per facilitarne la comprensione e la manutenzione in futuro.

WITH users_groups AS (
    SELECT 
        user_id, 
        g.id,
        g.name AS group_name 
    FROM users u
    INNER JOIN user_actions ua ON ua.user_id = u.id
    LEFT JOIN groups g ON g.id = u.primary_group_id
    WHERE ua.action_type = 15
    GROUP BY user_id, g.id
),
    
tt_solution_by_month AS (
    SELECT 
        date_part('year', created_at) AS year, 
        date_part('month', created_at) AS month,
		COUNT(*) AS "total"
	FROM user_actions ua
	WHERE ua.action_type = 15
	GROUP BY date_part('year', created_at), date_part('month', created_at)
	ORDER BY date_part('year', created_at) ASC, date_part('month', created_at)
),
	
tt_solution_groups_by_month AS (
    SELECT 
        date_part('year', created_at) AS year, 
        date_part('month', created_at) AS month,
        ug.group_name,
    	COUNT(*) AS "tt_groups"
    FROM user_actions ua
    INNER JOIN users_groups ug ON ug.user_id = ua.user_id
    WHERE ua.action_type = 15
    GROUP BY ug.group_name, date_part('year', created_at), date_part('month', created_at)
    ORDER BY date_part('year', created_at) ASC, date_part('month', created_at), ug.group_name)
    
SELECT 
    ts.year,
    ts.month,
    COALESCE(tsg.group_name,'senza gruppo'),
    tt_groups,
    total,
    TRUNC((tt_groups::decimal/total::decimal) *100,1) AS "%"
FROM tt_solution_groups_by_month tsg  
INNER JOIN tt_solution_by_month ts 
    ON ts.year = tsg.year AND ts.month = tsg.month 


Fammi sapere se questo è il risultato che ti aspetti o se devo apportare delle modifiche.

Michelle

3 Mi Piace

Nessun problema! Sei il mio salvatore qui! :smiley:

Quasi perfetto! Non mi servono le colonne tt_groups e total, solo il numero percentuale. Per quanto riguarda la colonna group_name, la query sarà per un solo gruppo, quindi non serve nemmeno quella colonna. Nel codice della query specificherò semplicemente primary_group_id, cosÏ cercherà le soluzioni solo per questo gruppo esatto.

1 Mi Piace

Modificato. :slightly_smiling_face:

-- [params]
-- string :primary_group_id

WITH users_groups AS (
    SELECT 
        user_id, 
        g.id,
        g.name AS group_name 
    FROM users u
    INNER JOIN user_actions ua ON ua.user_id = u.id
    LEFT JOIN groups g ON g.id = u.primary_group_id
    WHERE ua.action_type = 15
    AND u.primary_group_id = :primary_group_id
    GROUP BY user_id, g.id
),
    
tt_solution_by_month AS (
    SELECT 
        date_part('year', created_at) AS year, 
        date_part('month', created_at) AS month,
		COUNT(*) AS "total"
	FROM user_actions ua
	WHERE ua.action_type = 15
	GROUP BY date_part('year', created_at), date_part('month', created_at)
	ORDER BY date_part('year', created_at) ASC, date_part('month', created_at)
),
	
tt_solution_groups_by_month AS (
    SELECT 
        date_part('year', created_at) AS year, 
        date_part('month', created_at) AS month,
        ug.group_name,
    	COUNT(*) AS "tt_groups"
    FROM user_actions ua
    INNER JOIN users_groups ug ON ug.user_id = ua.user_id
    WHERE ua.action_type = 15
    GROUP BY ug.group_name, date_part('year', created_at), date_part('month', created_at)
    ORDER BY date_part('year', created_at) ASC, date_part('month', created_at), ug.group_name)
    
SELECT 
    ts.year,
    ts.month,
    TRUNC((tt_groups::decimal/total::decimal) *100,1) AS "%"
FROM tt_solution_groups_by_month tsg  
INNER JOIN tt_solution_by_month ts 
    ON ts.year = tsg.year AND ts.month = tsg.month
4 Mi Piace

Perfetto! Era proprio quello che stavo cercando!

2 Mi Piace

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.