Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Firebird e Interbase (https://www.clubdelphi.com/foros/forumdisplay.php?f=19)
-   -   Consulta SQL algo compleja (https://www.clubdelphi.com/foros/showthread.php?t=36834)

subzero 26-10-2006 00:11:06

Consulta SQL algo compleja
 
Hola foro:

De antemano gracias por Interesarse en mi duda, Bueno, tengo lo siguiente, estoy generando un consulta en Firebird 2.0 en la cual estoy tomando tres tablas con este script.

Código PHP:

SELECT
  E
.EMP_NIT,
  
E.PRO_CODIGO,
  
PT.PRE_CODIGO,
  
MIN((E.EMP_PRO_COSTO - (E.EMP_PRO_COSTO E.EMP_PRO_DESC 100)) / E.EMP_PRO_EMBALAJE) AS TOTAL
FROM
  EMPRESA_PRODUCTO E
  INNER JOIN PRODUCTOS P ON 
(E.PRO_CODIGO P.PRO_CODIGO)
  
INNER JOIN PRESENTACION PT ON (P.PRO_PRE_CODIGO PT.PRE_CODIGO)
GROUP BY
  E
.EMP_NIT,
  
E.PRO_CODIGO,
  
PT.PRE_CODIGO
ORDER BY
  E
.PRO_CODIGO 


y me genera lo siguiente:

Código PHP:

    NIT           CODIGO        PRE      TOTAL 
800042169    0000000003     027      735 
800042169    0000000004     033      700 
8300108781   0000000004     033      450 
800042169    0000000005     064      20 
8300108781   0000000005     064      16.5 

Pués bien, como nos podemos dar cuenta existen códigos que se repiten pero pertenecen a "NIT's" diferentes y puede en ocaciones a "PRE" Presentaciones diferentes, por lo tanto la necesidad es poder escoger el registro con menor valor en el "TOTAL" para que me devuelva algo así:

Código PHP:

    NIT           CODIGO        PRE      TOTAL 
800042169    0000000003     027      735 
8300108781   0000000004     033      450 
8300108781   0000000005     064      16.5 

Como se pueden dar cuenta es escoger el producto mas barato y saber que empresa lo tiene... Agradezco cualquier idea.

jachguate 26-10-2006 16:51:24

Probá con esto:

Código SQL [-]
SELECT
  E.EMP_NIT,
  E.PRO_CODIGO,
  PT.PRE_CODIGO,
  MIN((E.EMP_PRO_COSTO - (E.EMP_PRO_COSTO * E.EMP_PRO_DESC / 100)) / E.EMP_PRO_EMBALAJE) AS TOTAL
FROM
  EMPRESA_PRODUCTO E
  INNER JOIN PRODUCTOS P ON (E.PRO_CODIGO = P.PRO_CODIGO)
  INNER JOIN PRESENTACION PT ON (P.PRO_PRE_CODIGO = PT.PRE_CODIGO)
GROUP BY
  E.EMP_NIT,
  E.PRO_CODIGO,
  PT.PRE_CODIGO
ORDER BY
  E.PRO_CODIGO
HAVING
  MIN((E.EMP_PRO_COSTO - (E.EMP_PRO_COSTO * E.EMP_PRO_DESC / 100)) / E.EMP_PRO_EMBALAJE) =
  (
    Select min((E2.EMP_PRO_COSTO - (E2.EMP_PRO_COSTO * E2.EMP_PRO_DESC / 100)) / E2.EMP_PRO_EMBALAJE)
      from empresa_producto e2
     where e2.pro_codigo = e.pro_codigo
  )

Si no funciona a la primera, al menos te acerca a la idea.

Hasta luego.

;)

subzero 26-10-2006 18:49:01

Gracias, era lo que estaba necesitando.


La franja horaria es GMT +2. Ahora son las 20:29:53.

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