I have a table like this:
client msg_type msg_body id ------ -------- -------- --- 123 typeA success abc 123 typeB success abc 456 typeA success abc 456 typeB failure abc 123 typeA success abc 123 typeA success abc 789 typeA success def 789 typeB success def
and etc.
I would like to conclude as follows:
client diff id ------ ---- --- 123 2 abc 456 1 abc 789 0 def
where diff is the number of messages typeA:success - typeB:success . I can get a Type A success score using something like:
select client, count(*) from mytable where msg_type="typeA" and msg_body="success"
However, I cannot figure out how to add another account there (for type B), and also subtract. I tried something like:
select client, count(*) from mytable where msg_type="typeA" and msg_body="success" - count(*) from mytable where msg_type="typeB" and msg_body="success"
But of course, this did not work, or I would not ask here. :) Any tips?
Edit: another column added. I tried the two suggested sentences, but this only returns results for one of the identifiers, not both.
Edit # 2: I tried to wrap a SELECT query with
select id, count(*) from (select ...) as anothertable where count_a_minus_count_b = 0;
I was hoping the result would be like this:
id count --- ----- abc 2 def 1
where count is the number of clients, where the difference between typeA: success and typeB: success is 0.