1

Tabla de servicios

Tabla de ajustes

Tabla datos

Resultado esperado

Al ejecutar el siguiente código, me muestra la información a excepción de los servicios 16 y 17 porque tienen volumen 0 y debería mostrarme todos los servicios cuando su Volumen sea <> 0.

SELECT datos_cbr.servicio AS Num, nombreServicioSigitel AS Servicio, COUNT(importe) AS Volumen, IF((SELECT SUM(importeMas) FROM ajustes WHERE (numeroServicio = ajustes.servicio) != 0 AND fechaAjuste = '2020-06-28'), (SELECT COUNT(importeMas) FROM ajustes WHERE (numeroServicio = ajustes.servicio AND importeMas !=0 AND fechaAjuste = '2020-06-28')),0) AS 'Volumen(+)', IF((SELECT SUM(importeMenos) FROM ajustes WHERE (numeroServicio = ajustes.servicio) != 0 AND fechaAjuste = '2020-06-28'), (SELECT COUNT(importeMenos) FROM ajustes WHERE (numeroServicio = ajustes.servicio AND importeMenos !=0 AND fechaAjuste = '2020-06-28')),0) AS 'Volumen(-)',

(SELECT COUNT(importe) FROM datos_cbr WHERE (numeroServicio = servicio AND importe BETWEEN 0000.01 AND 2000.00 AND fechaOperacion = '2020-06-28')) AS '1 a 2,000' ,
(SELECT COUNT(importe) FROM datos_cbr WHERE (numeroServicio = servicio AND importe BETWEEN 2000.01 AND 3000.00 AND fechaOperacion = '2020-06-28')) AS '2,001 a 3,000' ,
(SELECT COUNT(importe) FROM datos_cbr WHERE (numeroServicio = servicio AND importe BETWEEN 3000.01 AND 4000.00 AND fechaOperacion = '2020-06-28')) AS '3,001 a 4,000' ,
(SELECT COUNT(importe) FROM datos_cbr WHERE (numeroServicio = servicio AND importe BETWEEN 4000.01 AND 5000.00 AND fechaOperacion = '2020-06-28')) AS '4,001 a 5,000' ,
(SELECT COUNT(importe) FROM datos_cbr WHERE (numeroServicio = servicio AND importe BETWEEN 5000.01 AND 7500.00 AND fechaOperacion = '2020-06-28')) AS '5,001 a 7,500' ,
(SELECT COUNT(importe) FROM datos_cbr WHERE (numeroServicio = servicio AND importe BETWEEN 7500.01 AND 10000.00 AND fechaOperacion = '2020-06-28')) AS '7,501 a 10,000' ,
(SELECT COUNT(importe) FROM datos_cbr WHERE (numeroServicio = servicio AND importe BETWEEN 10000.01 AND 15000.00 AND fechaOperacion = '2020-06-28')) AS '10,001 a 15,000',
(SELECT COUNT(importe) FROM datos_cbr WHERE (numeroServicio = servicio AND importe > 15000.01 AND fechaOperacion = '2020-06-28')) AS 'Mayor a 15,000',

(SELECT COUNT(importe) FROM datos_cbr WHERE (numeroServicio = servicio AND fechaOperacion = '2020-06-28')) 

+(SELECT COUNT(importeMas) FROM ajustes WHERE (numeroServicio = ajustes.servicio AND importeMas !=0 and fechaAjuste = '2020-06-28')) -(SELECT COUNT(importeMenos) FROM ajustes WHERE (numeroServicio = ajustes.servicio AND importeMenos !=0 and fechaAjuste = '2020-06-28')) AS 'Volumen Total',

IF(ISNULL( (SELECT FORMAT(ROUND(SUM(importe),2),2) FROM datos_cbr WHERE (numeroServicio = servicio AND fechaOperacion = '2020-06-28'))), 0.00, (SELECT FORMAT(ROUND(SUM(importe),2),2) FROM datos_cbr WHERE (numeroServicio = servicio AND fechaOperacion = '2020-06-28'))) AS Importe,

IF((SELECT sum(importeMas) FROM ajustes WHERE (numeroServicio = ajustes.servicio) <> 0.00 AND fechaOperacion = '2020-06-28'), 
(SELECT ROUND(SUM(importeMas),2) FROM ajustes WHERE (numeroServicio = ajustes.servicio AND fechaAjuste = '2020-06-28')),0.00) AS 'Importe(+)', 
IF((SELECT sum(importeMenos) FROM ajustes WHERE (numeroServicio = ajustes.servicio) <> 0.00 AND fechaOperacion = '2020-06-28'), 
(SELECT ROUND(SUM(importeMenos),2) FROM ajustes WHERE (numeroServicio = ajustes.servicio AND fechaAjuste = '2020-06-28')),0.00) AS 'Importe(-)',

IF(ISNULL(
(SELECT FORMAT(ROUND(SUM(importe),2),2) FROM datos_cbr WHERE (numeroServicio = servicio AND importe BETWEEN 0000.01 AND 2000.00 AND fechaOperacion = '2020-06-28'))), 0.00,
(SELECT FORMAT(ROUND(SUM(importe),2),2) FROM datos_cbr WHERE (numeroServicio = servicio AND importe BETWEEN 0000.01 AND 2000.00 AND fechaOperacion = '2020-06-28'))) AS '1 a 2,000' ,
 IF(ISNULL(
(SELECT FORMAT(ROUND(SUM(importe),2),2) FROM datos_cbr WHERE (numeroServicio = servicio AND importe BETWEEN 2000.01 AND 3000.00 AND fechaOperacion = '2020-06-28'))), 0.00,
(SELECT FORMAT(ROUND(SUM(importe),2),2) FROM datos_cbr WHERE (numeroServicio = servicio AND importe BETWEEN 2000.01 AND 3000.00 AND fechaOperacion = '2020-06-28'))) AS '2,001 a 3,000' ,
 IF(ISNULL(
(SELECT FORMAT(ROUND(SUM(importe),2),2) FROM datos_cbr WHERE (numeroServicio = servicio AND importe BETWEEN 3000.01 AND 4000.00 AND fechaOperacion = '2020-06-28'))), 0.00,
(SELECT FORMAT(ROUND(SUM(importe),2),2) FROM datos_cbr WHERE (numeroServicio = servicio AND importe BETWEEN 3000.01 AND 4000.00 AND fechaOperacion = '2020-06-28'))) AS '3,001 a 4,000' ,
IF(ISNULL(
(SELECT FORMAT(ROUND(SUM(importe),2),2) FROM datos_cbr WHERE (numeroServicio = servicio AND importe BETWEEN 4000.01 AND 5000.00 AND fechaOperacion = '2020-06-28'))), 0.00,
(SELECT FORMAT(ROUND(SUM(importe),2),2) FROM datos_cbr WHERE (numeroServicio = servicio AND importe BETWEEN 4000.01 AND 5000.00 AND fechaOperacion = '2020-06-28'))) AS '4,001 a 5,000' ,
IF(ISNULL(
(SELECT FORMAT(ROUND(SUM(importe),2),2) FROM datos_cbr WHERE (numeroServicio = servicio AND importe BETWEEN 5000.01 AND 7500.00 AND fechaOperacion = '2020-06-28'))), 0.00,
(SELECT FORMAT(ROUND(SUM(importe),2),2) FROM datos_cbr WHERE (numeroServicio = servicio AND importe BETWEEN 5000.01 AND 7500.00 AND fechaOperacion = '2020-06-28'))) AS '5,001 a 7,500' ,
IF(ISNULL(
(SELECT FORMAT(ROUND(SUM(importe),2),2) FROM datos_cbr WHERE (numeroServicio = servicio AND importe BETWEEN 7500.01 AND 10000.00 AND fechaOperacion = '2020-06-28'))), 0.00,
(SELECT FORMAT(ROUND(SUM(importe),2),2) FROM datos_cbr WHERE (numeroServicio = servicio AND importe BETWEEN 7500.01 AND 10000.00 AND fechaOperacion = '2020-06-28'))) AS '7,501 a 10,000' ,
IF(ISNULL(
(SELECT FORMAT(ROUND(SUM(importe),2),2) FROM datos_cbr WHERE (numeroServicio = servicio AND importe BETWEEN 10000.01 AND 15000.00 AND fechaOperacion = '2020-06-28'))), 0.00,
(SELECT FORMAT(ROUND(SUM(importe),2),2) FROM datos_cbr WHERE (numeroServicio = servicio AND importe BETWEEN 10000.01 AND 15000.00 AND fechaOperacion = '2020-06-28'))) AS '10,001 a 15,000' ,
IF(ISNULL(
(SELECT FORMAT(ROUND(SUM(importe),2),2) FROM datos_cbr WHERE (numeroServicio = servicio AND importe > 15000.01 AND fechaOperacion = '2020-06-28'))), 0.00,
(SELECT FORMAT(ROUND(SUM(importe),2),2) FROM datos_cbr WHERE (numeroServicio = servicio AND importe > 15000.01 AND fechaOperacion = '2020-06-28'))) AS 'Mayor a 15,000',

FORMAT((SELECT ROUND(SUM(importe),2) FROM datos_cbr WHERE (numeroServicio = servicio AND fechaOperacion = '2020-06-28')) 

+IF((SELECT SUM(importeMas) FROM ajustes WHERE (numeroServicio = ajustes.servicio) <> 0.00 AND fechaAjuste = '2020-06-28'), (SELECT ROUND(SUM(importeMas),2) FROM ajustes WHERE (numeroServicio = ajustes.servicio)AND fechaAjuste = '2020-06-28'), 0.00) -IF((SELECT SUM(importeMenos) FROM ajustes WHERE (numeroServicio = ajustes.servicio) <> 0.00 AND fechaAjuste = '2020-06-28'), (SELECT ROUND(SUM(importeMenos),2) FROM ajustes WHERE (numeroServicio = ajustes.servicio)AND fechaAjuste = '2020-06-28'), 0.00),2) AS 'Importe Total'

FROM datos_cbr INNER JOIN servicios WHERE numeroServicio = datos_cbr.servicio AND fechaOperacion = '2020-06-28' or numeroServicio = ANY (SELECT servicio FROM ajustes WHERE importeMas > 0 OR importeMenos > 0)

GROUP BY servicio ORDER BY tipoComision, numeroServicio;

1
  • Saludos. Corrige los nombre de las tablas en la imagen así mismo en lugar de aplicar INNER JOIN a quien es tabla1 usa LEFT JOIN para que no elimine el dato que quieres. el 30 jun. 2020 a las 1:03

1 respuesta 1

0

en la parte donde tienes: FROM datos_cbr INNER JOIN servicios WHERE numeroServicio = datos usa LEFT JOIN

el LEFT JOIN indica que tendrás todos los datos del lado izquierdo del JOIN a pesar de no tener una contraparte al lado derecho.

Recuerda que al tener un LEFT JOIN, debes especificar sobre que columnas se hace el JOIN, p.e. FROM datos_cbr LEFT JOIN servicios ON datos_cbr.columna1 = servicios.columna2

2
  • 1
    Estimado William, al cambiar a LEFT JOIN me muestra lo siguente : Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE numeroServicio = datos_cbr.servicio AND fechaOperacion = '2020-06-28' or n' at line 56 el 30 jun. 2020 a las 15:46
  • Creo que el problema radica en que debes especificar sobre que columnas está haciendo el join .... LEFT JOIN tabla2 ON tabla1.columna1 = tabla2.columna2, el WHERE sería una condición adicional al JOIN si es que la tienes. mysqltutorial.org/mysql-left-join.aspx el 30 jun. 2020 a las 16:04

Tu Respuesta

Al pulsar en “Publica tu respuesta”, muestras tu consentimiento a nuestros términos de servicio, política de privacidad y política de cookies

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.