次のようなテーブルがあります。
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
等
次のような出力が必要です。
client    diff   id
------    ----   ---
 123      2      abc
 456      1      abc
 789      0      def
diffメッセージの数はどこtypeA:successですか -typeB:successメッセージ。次のようなものを使用して、typeA の成功数を取得できます。
select client, count(*) from mytable
where msg_type="typeA" and msg_body="success"
ただし、そこに別のカウントを入れて(typeBの場合)、減算する方法がわかりません。私は次のようなものを試しました:
select client, count(*) from mytable
where msg_type="typeA" and msg_body="success" - count(*)
from mytable where msg_type="typeB" and msg_body="success"
しかし、もちろんうまくいきませんでした。:) 何かアドバイス?
編集:別の列を追加しました。与えられた 2 つの提案を試しましたが、両方ではなく、一方の ID の結果のみが返されるようです。
編集#2:SELECTクエリを次のようにラップしてみました:
select id, count(*) from (select ...) as anothertable where count_a_minus_count_b = 0;
出力が次のようになることを望んでいました。
id    count
---   -----
abc   2
def   1
count は、typeA:success と typeB:success の差が 0 であるクライアントの数です。