款项返回字符串,只有在PostgreSQL [英] sums return String, only with postgresql

查看:119
本文介绍了款项返回字符串,只有在PostgreSQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从MySQL迁移数据库到Postgres的。迁移本身是好的,下面的Postgres的文档。

现在,我正在修复我们的具体的MySQL查询。

在某一点上,我们现在有这样的事情:

 选择(%(
  SUM(CASE WHEN收入那么看重ELSE 0 END)由于租金,
  SUM(CASE WHEN不收那么看重ELSE 0 END)为费用
))
 

在MySQL中,这是一个 SUM(IF(收入,价值,0))等,并且它工作正常。

通过PG,它返回一个数字的字符串代替。

我已经检查了数据库和数据类型是正确的。

我能做些什么,除了投 to_d to_f


修改:完整的查询:

  SELECT
 SUM(CASE WHEN收入那么看重ELSE 0 END)由于租金,
 SUM(CASE WHEN不收那么看重ELSE 0 END)为费用
 从交易
 WHERE交易,类型IN(交易)
 与交易。user_ID的= 1
 与交易,有偿='T'
 AND(与2013年9月1日transactions.date和2013年9月30日)
 LIMIT 1
 

解决方案

据我所知,使用 .to_f .to_i 或什么就是答案 - Rails的Postgres的适配器似乎坚信这一切都是一个字符串,除非它是一个ActiveRecord模型

请参阅:<一href="http://stackoverflow.com/questions/12571215/connection-select-value-only-returns-strings-in-postgres-with-pg-gem">connection.select_value只返回字符串的Postgres与PG宝石

我不是特别赞同这一点,但它是,正如俗话所说,如预期。

I am migrating a database from mysql to postgres. The migration itself was ok, following the postgres documentation.

Right now, I'm fixing our specific mysql queries.

In some point, we have now something like this:

select(%(
  SUM(CASE WHEN income THEN value ELSE 0 END) AS rents,
  SUM(CASE WHEN not income THEN value ELSE 0 END) AS expenses
))

In mysql, it was a sum(if(incomes, value, 0)) etc, and it was working as expected.

With PG, it returns a string instead of a numeric.

I already checked the database and the data type is correct.

What can I do, besides cast to_d or to_f?


EDIT: the complete query:

SELECT 
 SUM(CASE WHEN income THEN value ELSE 0 END) AS rents,
 SUM(CASE WHEN not income THEN value ELSE 0 END) AS expenses
 FROM "transactions"
 WHERE "transactions"."type" IN ('Transaction')
 AND "transactions"."user_id" = 1
 AND "transactions"."paid" = 't'
 AND (transactions.date between '2013-09-01' and '2013-09-30')
 LIMIT 1

解决方案

As far as I know, using .to_f, .to_i or whatever is the answer - the Rails PostGres adapter seems adamant that everything is a String unless it's an ActiveRecord model.

See: connection.select_value only returns strings in postgres with pg gem

I don't particularly approve of this, but it is, as the saying goes, 'working as intended'.

这篇关于款项返回字符串,只有在PostgreSQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆