Here is my sample table:
owner file_type status
John doc_1 1
John doc_2 1
John doc_3 0
Mike doc_1 1
Mike doc_2 0
Mike doc_3 1
Dona doc_1 0
Dona doc_2 0
Dona doc_3 0
Jane doc_1 1
Jane doc_2 1
Jane doc_3 1
Result I need to see from my query is this:
owner uploaded remaining
John 2 1
Mike 2 1
Dona 0 3
Jane 3 0
Here is the code I tried.
SELECT owner AS 'Name', COUNT(file_type) AS 'uploaded'
FROM sampleTable
WHERE [status] = 1
GROUP BY owner
Based on my code I get the count of uploaded documents,but how do I also get the remaining count in the same query the other condition is [status] = 0
?
3条答案
按热度按时间qojgxg4l1#
We can use conditional aggregation here:
Each of the above
CASE
expressions conditionally counts the number of times the status be 0 or 1.dauxcl2d2#
Just use a
CASE
expression:vs3odd8k3#
Assuming that
status
is always0
or1
, I would recommend arithmetics rather thancase
expressions. I would assume this solution to be a little more efficient than thecase
approach (and it also neater):