简体   繁体   中英

mysql having clause on sum of count

I have a following query giving following result

SELECT ttable.* 
FROM  ( SELECT 
            `STATUS`, 
            `TELCOID`,                
            COUNT(*) smsCount                 
        FROM `smsout` 
        WHERE `STATUS`  = 'Send'        
            AND (RECEIVEDTIME BETWEEN DATE_SUB(NOW(), INTERVAL 1000000 MINUTE) AND NOW())
        GROUP BY  `STATUS` , `TELCOID`                                 
   ) ttable 
#having Sum(smscount) > 2500 ;

STATUS  TELCOID smsCount
send    -3      2
send    -1      2487
send    158     233
send    162     16

what I needed is to add a where/having clause which will check sum(smscount) >2500 then return the result set other wise nothing What can i change in this query to achieve it. In the current result sum of 2 + 2487 + 233 + 16 is greater then 2500 so it will not return any thing. if instead of 2500 i place 3000 then resultset will return

You either need to run two queries or use variables. I think this might work:

SELECT status, telcoid, smscount
FROM  (SELECT `STATUS`, `TELCOID`, COUNT(*) as smsCount,
              (@s := @s + COUNT(*)) as cumesum              
        FROM `smsout` CROSS JOIN (SELECT @s := 0) vars
        WHERE `STATUS`  = 'Send' AND       
              RECEIVEDTIME BETWEEN DATE_SUB(NOW(), INTERVAL 1000000 MINUTEAND NOW())
        GROUP BY `STATUS` , `TELCOID`                                 
     ) ttable 
WHERE @s > 2500;

Sometimes variables don't work quite right with aggregation, so you might need an extra layer of subqueries for this to work.

EDIT:

Try this version:

SELECT status, telcoid, smscount
FROM  (SELECT t.*, (@s := @s + COUNT(*)) as cumesum              
       FROM (SELECT `STATUS`, `TELCOID`, COUNT(*) as smsCount
              FROM `smsout`
              WHERE `STATUS`  = 'Send' AND       
                    RECEIVEDTIME BETWEEN DATE_SUB(NOW(), INTERVAL 1000000 MINUTEAND NOW())
              GROUP BY `STATUS` , `TELCOID`                                 
            ) t CROSS JOIN
            (SELECT @s := 0) vars
       ) t
WHERE @s > 2500;

smscount is a sum because you wrote count(*) smscount

can you try

SELECT ttable.* 
FROM  ( SELECT 
        `STATUS`, 
        `TELCOID`,                
        COUNT(*) smsCount                 
        FROM `smsout` 
        WHERE `STATUS`  = 'Send'        
            AND (RECEIVEDTIME BETWEEN DATE_SUB(NOW(), INTERVAL 1000000     MINUTE) AND NOW())
        GROUP BY  `STATUS` , `TELCOID`                                 
   ) ttable 
having smscount > 2500 ;
SELECT `STATUS`, `TELCOID`, COUNT(*) smsCount
FROM `smsout` 
WHERE `STATUS`  = 'Send'        
AND (RECEIVEDTIME BETWEEN DATE_SUB(NOW(), INTERVAL 1000000 MINUTE) AND NOW())
GROUP BY  `STATUS` , `TELCOID` 
HAVING COUNT(*) > 2500;

The subquery shouldn't be necessary.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM