Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

use of the count function in sql

I have a small doubt, can I show the counted total of the rows instead of what it appears below ,separately. Without using subqueries?

SELECT  COUNT(TEMA),tema
FROM LIBRERIAS 
WHERE TRIM(TEMA )LIKE 'GEO%IA'  AND EJEMPLARES BETWEEN 5 AND 20
GROUP BY tema;

enter image description here

I would like to get the total counted showed only one time. It is possible? Can i achieve this?
enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use SUM OVER to get the total:

SELECT SUM(COUNT(tema)) OVER () AS total, tema
FROM librerias 
WHERE TRIM(tema) LIKE 'GEO%IA'
AND ejemplares BETWEEN 5 AND 20
GROUP BY tema
ORDER BY tema;

A more typical query result would be the result you already got along with a total row (using ROLLUP). Yet another, one single result row showing the count and the temas:

SELECT
  COUNT(tema) AS total,
  LISTAGG(tema, ', ') WITHIN GROUP (ORDER BY tema) as temas
FROM librerias 
WHERE TRIM(tema) LIKE 'GEO%IA'
AND ejemplares BETWEEN 5 AND 20;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading