5
我可以对hstore列中的值执行聚合查询吗?例如,我想确定商店中的count
字段的SUM
。我可以在PostgreSQL HStore值上使用聚合函数吗?
我可以对hstore列中的值执行聚合查询吗?例如,我想确定商店中的count
字段的SUM
。我可以在PostgreSQL HStore值上使用聚合函数吗?
是的。但值被存储为文本,因此您必须先将它们转换为适当的数据类型。因此,以英寸为单位的高度总和存储在“其他”列中的hstore中
CREATE TABLE my_table (
id integer primary key,
other hstore
);
insert into my_table values
(1, hstore('height_in', '72') || hstore('weight_lbs', '180')),
(2, hstore('height_in', '65') || hstore('girth_in', '42'));
select sum((other->'height_in')::integer) sum_of_height
from my_table;
sum_of_height
--
137
如果任何一行包含无法用整数转换的值,该怎么办?它会给'错误:无效输入语法为整数:'所以如何获得所需的结果跳过该行? – RockStar
@RockStar:使用WHERE子句和正则表达式:'where(other - >'height_in')〜'^ \ d + $''。尽管如此,需要做到这一点指向应用程序代码中的错误。修复。 –