我使用Rails 4.1,Ruby 2.1和groupdate gem 2.4
我有一个包含以下列的记录表:
:value(json):day(datetime)
current_user.time_zone为用户提供了'时区。
value给出一个像这样的json对象:
{"430"=>1079.27, "400"=>10.23 }
我试图为每周添加所有价值[' 430']字段
这会产生错误
Record.where(:user_id => current_user.id).group_by_week(:day, time_zone: current_user.time_zone).sum("value#>>'{430}'")
(40.3ms) SELECT SUM(value#>>'{430}') AS sum_value_430, (DATE_TRUNC('week', (day::timestamptz - INTERVAL '6 day' - INTERVAL '0' hour) AT TIME ZONE 'Asia/Shanghai') + INTERVAL '6 day' + INTERVAL '0' hour) AT TIME ZONE 'Asia/Shanghai' AS week FROM "eats" WHERE "eats"."user_id" = 53 AND (day IS NOT NULL) GROUP BY (DATE_TRUNC('week', (day::timestamptz - INTERVAL '6 day' - INTERVAL '0' hour) AT TIME ZONE 'Asia/Shanghai') + INTERVAL '6 day' + INTERVAL '0' hour) AT TIME ZONE 'Asia/Shanghai'
PG::UndefinedFunction: ERROR: function sum(text) does not exist
LINE 1: SELECT SUM(value#>>'{430}') AS sum_total_nutr...
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
: SELECT SUM(value#>>'{430}') AS sum_value_430, (DATE_TRUNC('week', (day::timestamptz - INTERVAL '6 day' - INTERVAL '0' hour) AT TIME ZONE 'Asia/Shanghai') + INTERVAL '6 day' + INTERVAL '0' hour) AT TIME ZONE 'Asia/Shanghai' AS week FROM "eats" WHERE "eats"."user_id" = 53 AND (day IS NOT NULL) GROUP BY (DATE_TRUNC('week', (day::timestamptz - INTERVAL '6 day' - INTERVAL '0' hour) AT TIME ZONE 'Asia/Shanghai') + INTERVAL '6 day' + INTERVAL '0' hour) AT TIME ZONE 'Asia/Shanghai'
ActiveRecord::StatementInvalid: PG::UndefinedFunction: ERROR: function sum(text) does not exist
LINE 1: SELECT SUM(value#>>'{430}') AS sum_total_nutr...
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
: SELECT SUM(value#>>'{430}') AS sum_value_430, (DATE_TRUNC('week', (day::timestamptz - INTERVAL '6 day' - INTERVAL '0' hour) AT TIME ZONE 'Asia/Shanghai') + INTERVAL '6 day' + INTERVAL '0' hour) AT TIME ZONE 'Asia/Shanghai' AS week FROM "eats" WHERE "eats"."user_id" = 53 AND (day IS NOT NULL) GROUP BY (DATE_TRUNC('week', (day::timestamptz - INTERVAL '6 day' - INTERVAL '0' hour) AT TIME ZONE 'Asia/Shanghai') + INTERVAL '6 day' + INTERVAL '0' hour) AT TIME ZONE 'Asia/Shanghai'
尝试后
Record.where(:user_id => current_user.id).group_by_week(:day, time_zone: current_user.time_zone).sum("CAST(value->>'430'AS integer)")
我得到了
ActiveRecord::StatementInvalid at /records
PG::InvalidTextRepresentation: ERROR: invalid input syntax for integer: "1063.32"
: SELECT SUM(CAST(value->>'430'AS integer)) AS sum_cast_value_430_as_integer, (DATE_TRUNC('week', (day::timestamptz - INTERVAL '6 day' - INTERVAL '0' hour) AT TIME ZONE 'Asia/Shanghai') + INTERVAL '6 day' + INTERVAL '0' hour) AT TIME ZONE 'Asia/Shanghai' AS week FROM "records" WHERE "records"."user_id" = 53 AND (day IS NOT NULL) GROUP BY (DATE_TRUNC('week', (day::timestamptz - INTERVAL '6 day' - INTERVAL '0' hour) AT TIME ZONE 'Asia/Shanghai') + INTERVAL '6 day' + INTERVAL '0' hour) AT TIME ZONE 'Asia/Shanghai'
答案 0 :(得分:2)
您可能需要将JSON类型字段强制转换为整数(为清楚起见,我已删除了其他查询方法)。
Record.sum("CAST(value->>'430'AS integer)")
答案 1 :(得分:1)
你可以试试这个:
value = Record.where(:user_id => current_user.id).group_by_week(:day, time_zone: current_user.time_zone).pluck(:value)
sum = value.reduce(:+)