Search code examples
sqlpostgresqlsqldatatypes

Postgres: how to excecute the query for Sum as it is giving error?


I am using sum function to find the total but getting error. Here is the query:

select sum(col1) 
from table_name 
where col2="abc"
Error: function sum(text) does not exist
Hint: No function matches the given name and argument types. You might need to add explicit type casts

Solution

  • Assuming the text column contains text numbers, not actual integers, then it would explain the error you are seeing. You might get around this by first casting text to integer, then summing:

    SELECT SUM(text::int)
    FROM yourTable;