Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   SQL (https://www.clubdelphi.com/foros/forumdisplay.php?f=6)
-   -   Que aparezca la fila con un SUM en cero (https://www.clubdelphi.com/foros/showthread.php?t=83356)

santiago14 07-06-2013 19:19:25

Que aparezca la fila con un SUM en cero
 
Buenas, tengo un dilema, he aquí la consulta

Código SQL [-]
Select s.forma_envio, COALESCE(SUM(s.CANT_EJEMPLARES), 0) AS cant_reserva
From SUSCRIPCIONES s
Where (s.TIPO_SCIONES = 'PAPEL' or s.TIPO_SCIONES = 'PAPEL E INTERNET')
and s.ESTADO_SCIONES = 'A'
and '2013/06/08' BETWEEN s.f_inic_sciones AND s.f_fin_sciones
and s.cod_sucursal = 1
Group by s.forma_envio
Order by s.forma_envio asc

Las formas de envío pueden ser de tres tipos posibles: CORREO, ESTAFETA, RETIRA
La consulta me dice cuantos ejemplares van por cada uno de los tipos, el resultado: tres filas

CORREO 10
ESTAFETA 13
RETIRA 20

El drama se me presenta cuando uno de los tipos no tiene ejemplares en el rango de fechas, supongamos que ESTAFETA no tiene ejemplares en el rango de fechas, lo que yo quiero que presente es:

CORREO 10
ESTAFETA 0
RETIRA 20

Sin embargo, la sql me devuelve

CORREO 10
RETIRA 20

¿Cómo puedo hacer para lograr que ponga ESTAFETA 0?

Gracias, Santiago.

santiago14 07-06-2013 19:41:27

Ah, el motor es Firebird 2.1. Lo había olvidado.

Gracias.:D

maeyanes 07-06-2013 20:28:03

Hola...

Te había propuesto que uses CASE, pero veo que la función COALESCE ya debería hacer lo que necesitas...


Saludos...

aposi 07-06-2013 20:42:24

hola,
tienes alguna tabla con todas las formas de envio??

TOPX 07-06-2013 20:45:50

Buenas,

Aparentemente los distintos valores de FORMA_ENVIO solamente están en esa tabla SUSCRIPCIONES, así que tal vez se pueda haciendo un JOIN de la misma tabla. Algo así:
Código SQL [-]
SELECT S2.FORMA_ENVIO, COALESCE(SUM(S1.CANT_EJEMPLARES), 0) AS CANT_RESERVA
FROM SUSCRIPCIONES S1
LEFT OUTER JOIN (SELECT DISTINCT FORMA_ENVIO FROM SUSCRIPCIONES) AS S2
  ON S1.FORMA_ENVIO = S2.FORMA_ENVIO
WHERE S1.TIPO_SCIONES IN ('PAPEL', 'PAPEL E INTERNET')
  AND S1.ESTADO_SCIONES = 'A'
  AND '2013/06/08' BETWEEN S1.F_INIC_SCIONES AND S1.F_FIN_SCIONES
  AND S1.COD_SUCURSAL = 1
GROUP BY S2.FORMA_ENVIO
ORDER BY S2.FORMA_ENVIO
-

aposi 07-06-2013 20:52:50

Código SQL [-]

Select distinct a.forma_envio, COALESCE((select sum(s.CANT_EJEMPLARES)
From SUSCRIPCIONES s 
Where (s.TIPO_SCIONES = 'PAPEL' or s.TIPO_SCIONES = 'PAPEL E INTERNET') and s.ESTADO_SCIONES = 'A' and '2013/06/08' BETWEEN s.f_inic_sciones AND s.f_fin_sciones and s.cod_sucursal = 1
and s.forma_envio = a.forma_envio), 0) AS cant_reserva
from SUSCRIPCIONES a 
order by a.forma_envio

Puedes provar este sql a ver como va

santiago14 08-06-2013 02:39:24

Tienen razón, las formas de envío están en la tabla SUSCRIPCIONES. Esto fue un error que cometí hace años :D, quedó así y hoy es muy pero muy difícil modificar la BD. Es grande y de un gran uso...

Algo que me funcionó fue esto:
Código SQL [-]
Select COALESCE(SUM(s.CANT_EJEMPLARES), 0) AS CORREO,
  (Select COALESCE(SUM(s.CANT_EJEMPLARES), 0) AS cant_reserva
  From SUSCRIPCIONES s
  Where (s.TIPO_SCIONES = 'PAPEL' or s.TIPO_SCIONES = 'PAPEL E INTERNET')
  and s.ESTADO_SCIONES = 'A'
  and '2013/06/08' BETWEEN s.f_inic_sciones AND s.f_fin_sciones
  and s.cod_sucursal = 1
  and s.forma_envio = 'RETIRA') AS RETIRA,

  (Select COALESCE(SUM(s.CANT_EJEMPLARES), 0) AS cant_reserva
  From SUSCRIPCIONES s
  Where (s.TIPO_SCIONES = 'PAPEL' or s.TIPO_SCIONES = 'PAPEL E INTERNET')
  and s.ESTADO_SCIONES = 'A'
  and '2013/06/08' BETWEEN s.f_inic_sciones AND s.f_fin_sciones
  and s.cod_sucursal = 1
  and s.forma_envio = 'ESTAFETA') AS ESTAFETA

From SUSCRIPCIONES s
Where (s.TIPO_SCIONES = 'PAPEL' or s.TIPO_SCIONES = 'PAPEL E INTERNET')
and s.ESTADO_SCIONES = 'A'
and '2013/06/08' BETWEEN s.f_inic_sciones AND s.f_fin_sciones
and s.cod_sucursal = 1
and s.forma_envio = 'CORREO'
Group by s.forma_envio

Está medio rebuscado, pero cuando alguna de las formas de envío no tiene ejemplares muestra un null, que lo puedo interpretar como "cero"
En lugar de hacer una fila por cada forma de envío, hice una columna (esto no es tan dramático, porque las formas de envío no han cambiado en años)


La franja horaria es GMT +2. Ahora son las 17:18:04.

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