Postgres:如何执行 Sum 查询,因为它给出了错误?

Abh*_*ngh 5 postgresql

我正在使用 sum 函数来查找总数,但出现错误。这是查询:

select sum(col1) 
from table_name 
where col2="abc"
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)

Tim*_*sen 10

假设该text列包含文本数字,而不是实际整数,那么它将解释您所看到的错误。您可以通过首先转换text为整数,然后求和来解决这个问题:

SELECT SUM(text::int)
FROM yourTable;
Run Code Online (Sandbox Code Playgroud)