SQL Group By con un Orden Por


Tengo una tabla de etiquetas y quiero obtener las etiquetas de mayor recuento de la lista.

Los datos de muestra se ven así

id (1) tag ('night')
id (2) tag ('awesome')
id (3) tag ('night')

Usando

SELECT COUNT(*), `Tag` from `images-tags`
GROUP BY `Tag`

Me devuelve los datos que estoy buscando perfectamente. Sin embargo, me gustaría organizarlo, de modo que los recuentos de etiquetas más altos sean los primeros, y limitarlo a solo enviarme los primeros 20 o más.

Probé esto...

SELECT COUNT(id), `Tag` from `images-tags`
GROUP BY `Tag`
ORDER BY COUNT(id) DESC
LIMIT 20

Y sigo recibiendo un "Uso no válido de la función de grupo-ErrNr 1111"

¿Qué estoy haciendo mal?

Estoy usando MySQL 4.1.25-Debian

Author: OMG Ponies, 2008-08-26

6 answers

En todas las versiones de MySQL, simplemente ponga un alias al agregado en la lista de SELECCIÓN, y ordene por el alias:

SELECT COUNT(id) AS theCount, `Tag` from `images-tags`
GROUP BY `Tag`
ORDER BY theCount DESC
LIMIT 20
 174
Author: Scott Noyes,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2008-08-27 15:46:41

MySQL anterior a la versión 5 no permitía funciones agregadas en las cláusulas ORDER BY.

Puede sortear este límite con la sintaxis obsoleta:

SELECT COUNT(id), `Tag` from `images-tags`
GROUP BY `Tag`
ORDER BY 1 DESC
LIMIT 20

1, ya que es la primera columna en la que desea agrupar.

 50
Author: Lasse Vågsæther Karlsen,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2008-08-26 13:14:37

No conozco MySQL, pero en MS SQL, puede usar el índice de columna en la cláusula order by. He hecho esto antes al hacer cuentas con group by s, ya que tiende a ser más fácil trabajar con.

So

SELECT COUNT(id), `Tag` from `images-tags`
GROUP BY `Tag`
ORDER BY COUNT(id) DESC
LIMIT 20

Se convierte en

SELECT COUNT(id), `Tag` from `images-tags`
GROUP BY `Tag`
ORDER 1 DESC
LIMIT 20
 7
Author: jerhinesmith,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2008-08-26 17:18:21

En Oracle, algo como esto funciona muy bien para separar su conteo y ordenar un poco mejor. No estoy seguro si funcionará en MySQL 4.

select 'Tag', counts.cnt
from
  (
  select count(*) as cnt, 'Tag'
  from 'images-tags'
  group by 'tag'
  ) counts
order by counts.cnt desc
 4
Author: JosephStyons,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2008-08-26 17:34:48

Puede sortear este límite con la sintaxis obsoleta: ORDENAR POR 1 DESC

Esta sintaxis no está en desuso en absoluto, es E121-03 de SQL99.

 2
Author: Damien B,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2008-08-26 17:41:11

Pruebe esta consulta

 SELECT  data_collector_id , count (data_collector_id ) as frequency 
    from rent_flats 
    where is_contact_person_landlord = 'True' 
    GROUP BY data_collector_id 
    ORDER BY count(data_collector_id) DESC
 0
Author: Ashutosh Gupta,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-07-13 13:15:49