我想統計每user
已關閉多少個conversations
,以及每個user
已編寫多少個messages
。協會計數
一個user
有很多conversations
一個conversations
有許多messages
一個message
可以屬於user
這是查詢我有些遠
select a.id, u.display_name, count(c.id) as closed, count(m.id) as replied
from apps a
left join app_users au on au.app_id = a.id
left join users u on u.id = au.user_id
left join conversations c on c.app_id = a.id and c.closed_by_id = u.id
left join messages m on m.conversation_id = c.id and m.user_id = u.id
group by a.id, u.id
order by closed desc
當我沒有加入消息時,它工作正常只是計算封閉的談話。加入郵件時,closed
和replied
列是完全相同的數字(對於兩者也是不正確的)
任何想法?
是'計數(不同c.id)'? – Mike
是否有一列表示消息具有「關閉」或「已回覆」狀態? –
不是,但是有一個'closed_at',並且用戶回覆了一條消息,如果該消息的user_id匹配我們計算的用戶 – Tarlen