user_name id status calls
===========================================
apple 21 BadAd 2
apple 21 Lead 5
apple 21 DNC 6
apple 21 pajs 2
orange 34 Lead 9
orange 34 disks 3
orange 34 BadAd 8
orange 34 pajs 5
私の試み
SELECT sum(calls) FROM (SELECT y.calls, y.user_name, y.status FROM stats
INNER JOIN
(select user_name, status, sum(calls) as calls
from stats WHERE status = ('BadAD', 'Lead', 'DNC')
group by user_name, status) y
ON y.user_name = stats.user_name
GROUP BY y.status) w WHERE w.user_name = y.username
私の問題:calls
特定のstatus
フィールド値 ( WHERE status = ('BadAD', 'Lead', 'DNC')
) のみを含み、同じ user_name を持つ行のフィールドから値を合計しようとしています。したがって、次のようになります。
user_name id status calls
===========================================
apple 21 BadAd 13 (BadAd 2 + Lead 5 + DNC 6)
orange 34 Lead 17 (etc..)