Please I'm trying to find the percentage difference between two values sum of imls2016.visits and sum of imls2010.visits in each row. Which was previously working fine before I added a third table in my joint statement (the table being imls_regions). But anytime I run the new code it throws a numeric overflow error. I have adjusted the precision and scale but I haven't been able to rectify this error. Please what should I change in my code: (PostgreSQL 12)
- Error Message: > "ERROR: numeric field overflow DETAIL: A field with precision 10, scale 1 must round to an absolute value less than 10^9. SQL state: 22003"
My code:
SELECT imlsreg.obereg,
imlsreg.obereg_name,
SUM(imls2016.visits) AS total_visits_2016,
SUM(imls2010.visits) AS total_visits_2010,
ROUND((CAST(SUM(imls2016.visits) AS decimal(10, 1)) - SUM(imls2010.visits)) / SUM(imls2010.visits) * 100, 1) AS pct_change_in_visits
FROM imls_regions AS imlsreg INNER JOIN imls_library_survey_2016 AS imls2016
ON imlsreg.obereg = imls2016.obereg
INNER JOIN imls_library_survey_2010 AS imls2010
ON imlsreg.obereg = imls2010.obereg
WHERE imls2016.visits >= 0 AND imls2010.visits >= 0
GROUP BY imlsreg.obereg, imlsreg.obereg_name
ORDER BY pct_change_in_visits ASC;