I am probably missing something obvious and asking a silly thing, but I am unable to do a simple sum.
My data was imported with the character '€' character so I had to import the data as text:
original data sample:
"€31.51"
"€0.10"
"€24.23"
I tried to use a string function to remove the €. I was then hoping to convert to numeric and sum.
SELECT sum(coalesce(CAST(split_part(revenue_eur, '€', 2) as NUMERIC),'0'))
FROM revenue_test;
the only piece that runs is
SELECT coalesce(split_part(revenue_eur, '€', 2),'0')
FROM revenue_test;
What I need is just a sum. Could someone please kindly help me figure it out? I tried doing a subquery but failed in misery..
Maybe there is a way to import the data without the € and into numeric?
Thank you!!!!!!
EDIT: I imported via CSV to pgAdmin4 and using postgres 12 (The file counts to 85k rows in sql) To import the data I tried COPY with the pgAdmin4 query tool but I got error 'permission denied'. I checked all the permissions to my file but I clearly was missing something, the most likely solution I found was that I needed to connect to postgres via the terminal on my mac and use \COPY. But I didn't manage to do that. So I ended up using the right click feature 'import' via pgAdmin.
EDIT 2: I found the problem, when importing a ',' was inserted to one value (mark thousands) so I am unable to cast without taking it away. I found regular expression help on removing a character from a specific order but the , appears randomly. Code works!
WITH test AS
(SELECT translate(revenue_eur, '€,', '')::float as eur
FROM revenue_test)
SELECT sum(coalesce(eur, '0'))
from test;