I'm trying to write a query that extracts the total number of undeleted messages sent to friends. Here is a screenshot of the table structure:
The output I want to achieve is as follows:
id messages_count --------------------------- 1 4562 2 7255
Here are the queries I have tried so far:
SELECT u.id, count( CASE WHEN m.is_deleted = false THEN m.is_deleted END ) as messages_count FROM users u inner join messages m on u.id = m.to_id GROUP BY u.id ORDER BY u.id;
Have you tried anything similar?