1

I have two tables (lets call them cases and users) structured something like this: Cases

ID|status      |user_id
__|____________|_______  
1 |Open        |6
2 |Closed      |9
3 |Incomplete  |9

Users:

ID| 
__|_____
 6|
 9|

I would like to select a count for the id as well as a count for each one of the status fields.

Currently I have tried:

SELECT COUNT(c.id), COUNT(
(SELECT status from cases where status = 'Open')),
COUNT(
(SELECT status from cases where status = 'Closed')),
COUNT(
(SELECT status from cases where status = 'Incomplete'))
FROM cases as c
JOIN users as u on u.id = c.user_id

I am getting an error of query returning more than one row.

This is what I am trying to achieve

ID| Open | Closed | Incomplete |user_id
__|______|________|____________|__________
 2|0     |1       |1           |9

RECAP: A count for each user id that counts how many id's, and a count for each status.

1 Answer 1

3

You can try this with sum

SELECT COUNT(c.id), 
SUM(c.status = 'Open') Open ,
SUM(c.status = 'Closed') Closed ,
SUM(c.status = 'Incomplete') Incomplete,
u.id user_id
FROM cases as c
JOIN users as u on u.id = c.user_id
GROUP BY u.id
Sign up to request clarification or add additional context in comments.

1 Comment

This seems like such a reasonable answer. It is just really hard to tell if this actually answers the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.