Support Questions

Find answers, ask questions, and share your expertise

Incorrect math being returned on rows when aggregating values together

avatar
Contributor

So I have 2 values from different tables that i'm trying to join and aggregate. The problem is that i get results where the math doesn't add up at times- and have tried changing the join\inner join, group by, select distinct, cast- it doesn't seem to work. I need coalesce, as there are null values.

Dataset:

Table Sales:

CurrencyColAColBColCDateField
AUD208,883{null}{null}'2024-06-30'
CAD215,913{null}{null}'2024-06-30'
MZN00{null}'2024-06-30'
YER0{null}10'2024-06-30'

Table OTHER_Sales:

CurrencyCol1Col2Col3DateField
AUD{null}208,88320'2024-06-30'
CAD{null}215,9130'2024-06-30'
MZN{null}013'2024-06-30'
YER{null}00'2024-06-30'

Query:

SELECT a.currency as currency, SUM((coalesce(a.ColA,0)) + (coalesce(b.Col2))) as sales_check

FROM

db.sales a inner join db.OTHER_sales b

ON a.currency = b.currency

WHERE a.DateField = '2024-06-30'

GROUP BY a.currency

The incorrect results that I currently get:

currencysales_check

AUD

-1645761638.790000
CAD5855379123.770950
MZN0
YER0

The correct results that I need to get:

currencysales_check
AUD0
CAD0
MZN0
YER0

Thanks!

1 REPLY 1

avatar
Master Collaborator

Hi @Supernova 

Can you try this?

SELECT
a.currency as currency,
SUM(coalesce(a.ColA, 0) + coalesce(a.ColB, 0) + coalesce(a.ColC, 0) + coalesce(b.Col1, 0) + coalesce(b.Col2, 0) + coalesce(b.Col3, 0)) as sales_check
FROM
db.sales a
INNER JOIN
db.OTHER_sales b
ON
a.currency = b.currency
WHERE
a.DateField = '2024-06-30'
AND b.DateField = '2024-06-30'
GROUP BY
a.currency;

 

Regards,

Chethan YM