0

My table

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

My attempt

 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

My problem: I am trying to add up the values from the calls field of rows that only contain a certain status field value (WHERE status = ('BadAD', 'Lead', 'DNC')) and has the same user_name. So it would look like this.

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 回答