ROLLUP and HAVING in the same consultation

Asked

Viewed 61 times

0

The query below returns the sum of 24 accounts, using the ROLLUP and without the clause HAVING, so far so good, but when I use the clause HAVING shortly after the ROLLUP should return me the sum of 19 accounts, but continues to return me the sum of 24 accounts. How do I make the ROLLUP bring me the right sum, regardless of any condition HAVING?

Consultation without the clause HAVING

SELECT 
 ifnull(cust_id, 'Total') as ID_cliente,
 count(account_id) as QTDE_de_contas
 FROM account
 GROUP BY
 cust_id WITH ROLLUP

Consultation with the clause HAVING

SELECT 
 ifnull(cust_id, 'Total') as ID_cliente,
 count(account_id) as QTDE_de_contas
 FROM account
 GROUP BY
 cust_id WITH ROLLUP
 HAVING
 count(account_id) >= 2
  • "How do I make the ROLLUP bring me the right sum regardless of any condition" It doesn’t make sense, the rollup is based on group by, which is filtered by having, are totally dependent things, if you do not want the having need to remove from query... would be like to select and want the results not to depend on where

  • Hello Ricardo, I believe I did not make myself understood, when I run the query without the HAVING, query brings me a value, but when I use the filter HAVING, the query keeps bringing me the same value and shouldn’t. That’s what I meant.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.