SQL SUM仅返回值> = 1

时间:2015-07-30 08:45:17

标签: mysql sql sum

我有一个简单的查询,我只想返回值

SELECT advisor AS advisor,
SUM(`outcome` LIKE '%TPS_Approved%') AS approved
FROM shift_report 
WHERE `team`!=6 AND date>20150720 AND date<20150728
GROUP BY `advisor` 
ORDER BY `advisor`

如果计数为&gt; = 1

,我只希望它返回顾问和批准的计数

3 个答案:

答案 0 :(得分:1)

这会做你想要的吗?

SELECT advisor AS advisor,
count(*) AS approved
FROM shift_report 
WHERE `team`!=6 AND date>20150720 AND date<20150728 and `outcome` LIKE '%TPS_Approved%'
GROUP BY `advisor` 
ORDER BY `advisor`

答案 1 :(得分:0)

其中一个可行的选择

SELECT A.advisor, A.approved
FROM (
    SELECT advisor,
    SUM(`outcome` LIKE '%TPS_Approved%') AS approved
    FROM shift_report WHERE `team`!=6 and date>20150720 and date<20150728
    group by `advisor` order by `advisor`
) A
WHERE
    A.approved >= 1

答案 2 :(得分:0)

这应该可以满足您的需求,请注意having根据小组总数限制结果后的group by条件。

SELECT advisor,
count(*) AS approved
FROM shift_report 
WHERE `team`!=6 
AND date between 20150720 AND 20150728 
and `outcome` LIKE '%TPS_Approved%'
GROUP BY `advisor`
HAVING count(*) >=1