La tabla Suscription tiene una columna llamada serviceId cuyo valor repetido (2 veces) representa el ID de la tabla que estamos usando la función SUM
... por lo tanto en la consulta me trae 2 veces el valor de sum(p.amount)
...
¿Cómo configuro la consulta para que solo me utilice la función SUM una única vez...?
Intenté meter un DISTINCT
dentro de un subselect para ver si de esta forma genero una condición que me impida traerme valores repetidos de una tabla relacionada, pero nada... No me funcionó.
select 'Vuelto' as 'srvType', SUM(p.amount) as 'totalAmount', cc.name as 'currency'
from p2p p, p2p_response t, currency cc, company c, user u, subscription sub
where sub.serviceid IN
(SELECT distinct sub.serviceid
FROM subscription sub
where sub.companyid=1 and sub.serviceid=2)
and t.p2pid = p.p2pid
AND u.userid = p.userid
AND p.currencyid=cc.currencyid
AND u.companyid = c.companyid
AND sub.companyid = u.companyid
AND sub.companyid = c.companyid
AND c.companyid=1
AND sub.statusid=3
and STR_TO_DATE(p.trndate,'%d-%m-%Y') >= STR_TO_DATE('23-01-2023','%d-%m-%Y')
AND STR_TO_DATE(p.trndate,'%d-%m-%Y') <= STR_TO_DATE('23-01-2023','%d-%m-%Y')
and (t.referenceNumber != 'null' and t.referenceNumber != '') HAVING SUM(p.amount) IS NOT NULL;
Me solucioné usando la siguiente query, sin embargo no me funciona cuando distintos serviceID se repiten...
select 'Vuelto' as 'srvType', SUM(p.amount) as 'totalAmount', cc.name as 'currency' from p2p p, p2p_response t, currency cc, company c, user u, subscription sub where sub.serviceid IN (SELECT distinct sub.serviceid=2 FROM subscription sub where sub.statusid=3) and t.p2pid = p.p2pid AND u.userid = p.userid AND p.currencyid=cc.currencyid AND u.companyid = c.companyid AND sub.companyid = u.companyid AND sub.companyid = c.companyid AND c.companyid=1 and STR_TO_DATE(p.trndate,'%d-%m-%Y') >= STR_TO_DATE('24-01-2023','%d-%m-%Y')
AND STR_TO_DATE(p.trndate,'%d-%m-%Y') <= STR_TO_DATE('24-01-2023','%d-%m-%Y') and (t.referenceNumber != 'null' and t.referenceNumber != '') HAVING SUM(p.amount) IS NOT NULL
union select 'Pago con tarjeta' as 'srvType', SUM(cp.amount) as 'totalAmount', cc.name as 'currency' from card_payment cp, card_payment_response cpr, currency cc, company c, user u, subscription sub where sub.serviceid IN (SELECT distinct sub.serviceid=1 FROM subscription sub where sub.statusid=3) and cpr.idcardpayment = cp.idcardpayment and u.userid = cp.userid AND cp.currencyid=cc.currencyid AND u.companyid = c.companyid AND sub.companyid = u.companyid AND sub.companyid = c.companyid and c.companyid=1 and STR_TO_DATE(cpr.datetime,'%d-%m-%Y') >= STR_TO_DATE('24-01-2023','%d-%m-%Y')
AND STR_TO_DATE(cpr.datetime,'%d-%m-%Y') <= STR_TO_DATE('24-01-2023','%d-%m-%Y') and (cpr.reference != 'null' and cpr.reference != '') HAVING SUM(cp.amount) IS NOT NULL
union select 'Pago movil' as 'srvType', SUM(pm.amount) as 'totalAmount', cc.name as 'currency' from pago_movil pm, currency cc, company c, user u, subscription sub where sub.serviceid IN (SELECT distinct sub.serviceid=3 FROM subscription sub where sub.statusid=3) and u.userid = pm.userid and pm.currencyid=cc.currencyid and u.companyid = c.companyid AND sub.companyid = u.companyid AND sub.companyid = c.companyid and c.companyid=1 and sub.statusid=3 and STR_TO_DATE(pm.createdat,'%d-%m-%Y') >= STR_TO_DATE('24-01-2023','%d-%m-%Y')
AND STR_TO_DATE(pm.createdat,'%d-%m-%Y') <= STR_TO_DATE('24-01-2023','%d-%m-%Y') and (pm.reference != 'null' and pm.reference != '') HAVING SUM(pm.amount) IS NOT NULL
union select 'Transferencia' as 'srvType', SUM(tt.amount) as 'totalAmount', cc.name as 'currency' from transfer tt, currency cc, company c, user u, subscription sub where sub.serviceid IN (SELECT distinct sub.serviceid=3 FROM subscription sub where sub.statusid=3) and u.userid = tt.userid and tt.currencyid=cc.currencyid and u.companyid = c.companyid AND sub.companyid = u.companyid AND sub.companyid = c.companyid and c.companyid=1 and sub.statusid=3 and STR_TO_DATE(tt.createdat,'%d-%m-%Y') >= STR_TO_DATE('24-01-2023','%d-%m-%Y')
AND STR_TO_DATE(tt.createdat,'%d-%m-%Y') <= STR_TO_DATE('24-01-2023','%d-%m-%Y') and (tt.reference != 'null' and tt.reference != '') HAVING SUM(tt.amount) IS NOT NULL;