Sql postgres中的Sum列
我有一个varchar类型的列,值可以是任何字符串,是否可以找到可以转换为数值的所有行的总和Sql postgres中的Sum列,sql,postgresql,Sql,Postgresql,我有一个varchar类型的列,值可以是任何字符串,是否可以找到可以转换为数值的所有行的总和 value ---- | 3 | | 2 2| | as | | a1 | | !2@| | 0.5| ---- 3.5 您可以使用大小写表达式并转换: select (case when value ~ '^[0-9]+[.]?[0-9]*$' then value::numeric else 0 end) from t;
value
----
| 3 |
| 2 2|
| as |
| a1 |
| !2@|
| 0.5|
----
3.5
您可以使用大小写表达式并转换:
select (case when value ~ '^[0-9]+[.]?[0-9]*$'
then value::numeric
else 0
end)
from t;
是一个dbfiddle。您可以使用大小写表达式并转换:
select (case when value ~ '^[0-9]+[.]?[0-9]*$'
then value::numeric
else 0
end)
from t;
是一把小提琴。可能是:
with tb as(
select '3' as v union all
select '2 2' as v union all
select 'as' as v union all
select 'a1' as v union all
select '!2' as v union all
select '0.5'
)
select sum(case when v ~ '^[0-9\.]+$'
then v::numeric
else 0
end) as result
from tb;
可能是:
with tb as(
select '3' as v union all
select '2 2' as v union all
select 'as' as v union all
select 'a1' as v union all
select '!2' as v union all
select '0.5'
)
select sum(case when v ~ '^[0-9\.]+$'
then v::numeric
else 0
end) as result
from tb;
你错过了总数。@TheImpler。我也忽略了0.5不是整数;你错过了总数。@TheImpler。我也忽略了0.5不是整数;