Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   SQL (https://www.clubdelphi.com/foros/forumdisplay.php?f=6)
-   -   Seleccionar los diez registros con mas cantidad (https://www.clubdelphi.com/foros/showthread.php?t=59867)

juangabriel1786 11-09-2008 14:38:37

Seleccionar los diez registros con mas cantidad
 
Estoy relizando un select que me haga un count en una tabla y metraiga los registros que mas se repitan como explica el siguiente codigo,por cierto uso Mysql 5.1 y Delphi 7

Código SQL [-]SELECT c.CIE10, COUNT( i.COD_INGRESO ) FROM ingreso i, cie10 c WHERE (c.COD_CIE10 = i.COD_CIE10) and (i.FECHA >= :rd1) and (i.FECHA <= :rd2 ) GROUP BY c.CIE10 ORDER BY c.CIE10


pero a la hora de traer los registros quiero que me traigan los 10 registros que mas tengan. ejemplo
Registro 1=300
Registro 2=400
Registro 3=500
Registro....n=9000

Yo quiero que me traigan los 10 registros que mas cantidades tengan

maeyanes 11-09-2008 14:46:00

Hola...

Checa la ayuda de LIMIT

Tu SQL podría quedar así:

Código SQL [-]
SELECT c.CIE10, COUNT( i.COD_INGRESO ) FROM ingreso i, cie10 c WHERE (c.COD_CIE10 = i.COD_CIE10) and
  (i.FECHA >= :rd1) and (i.FECHA <= :rd2) GROUP BY c.CIE10 ORDER BY c.CIE10 LIMIT 10


Saludos...

Caro 11-09-2008 15:10:08

Hola, si quieres traer los que mas se repitan tendras que ordenar por la cantidad que te devuelve el count de mayor a menor "desc" y aplicar el Limit como te explica mayanes

Código SQL [-]
SELECT c.CIE10, COUNT( i.COD_INGRESO ) AS CANTIDAD
FROM ingreso i, cie10 c 
WHERE (c.COD_CIE10 = i.COD_CIE10) and (i.FECHA >= :rd1) and (i.FECHA <= :rd2) 
GROUP BY c.CIE10 ORDER BY CANTIDAD DESC LIMIT 10

Saluditos


La franja horaria es GMT +2. Ahora son las 16:32:10.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi