在GROUP BY和HAVING COUNT之后执行COUNT的总和> 1

时间:2014-07-24 19:47:01

标签: mysql

我有以下查询,我想对任何大于1的计数的计数表进行计算。我不确定如何执行此操作:

SELECT  count(id) as count, 
        id, 
        location_id, 
        time, 
        weekofyear(time) 


from table where weekofyear(time) = '28' 
group by location_id, id
having count > 1
order by count desc

结果:

count   id  location_id time    weekofyear(time)
5   32265   409 7/12/14 3:58    28
5   32266   409 7/12/14 3:59    28
5   27532   399 7/12/14 4:54    28
4   31124   41  7/7/14 4:41 28

所以我希望总和显示19

1 个答案:

答案 0 :(得分:0)

您自己的查询中的一个简单子选择应该这样做:

SELECT
   SUM(t.count)
FROM (
    SELECT  count(id) as count, 
            id, 
            location_id, 
            time, 
            weekofyear(time) 
    from table where weekofyear(time) = '28' 
    group by location_id, id
    having count > 1
) as t