0

のテーブル

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..)
4

1 に答える 1

2
select user_name, id, min(status), sum (calls)
from yourtable
where status in  ('BadAD', 'Lead', 'DNC')
group by user_name, id
于 2012-10-12T14:49:45.890 に答える