1

クエリでいくつかの計算を実行しようとしていますが、postgresql は mysql とは異なる方法で数学を処理していると思いますが、違いがどこにあるのかわかりません。これは私のコントローラーからの私のクエリです:

Invoice.find(params[:id], :joins => :invoice_line_items, :select => "invoices.id, SUM(invoice_line_items.hours * invoice_line_items.rate) as subtotal, SUM(invoice_line_items.hours * invoice_line_items.rate) - (SUM(invoice_line_items.hours * invoice_line_items.rate) * (invoices.discount_percentage / 100)) as total", :group => "invoices.id")

私の開発環境では mysql を使用しており、実稼働環境では postgresql を使用しています。これらは私が得ている出力です。参考までに、discount_percentage は 20 です。

MySQL:

 |  id  |  subtotal  |  total         |
----------------------------------------
 |  21  |  570.0000  |  456.00000000  |

Postgresql:

 |  id  |  subtotal  |  total         |
----------------------------------------
 |  9   |  570.0000  |  570.00000000  |

パーセンテージと合計に何か関係があるようです。誰にもアイデアはありますか?ちなみに、MySQLの結果は私が望んでいるものです。

4

1 に答える 1

2

Postgres は整数除算を行っています。

Select                    MySQL   Postgres
-----------------------   -----   --------
select 999 / 100 as a     9.99    9
select 999 / 100.0 as a   9.99    9.99

したがって、を変更しx / 100x / 100.0、両方が同じように動作するようにします。

于 2012-11-21T03:44:32.173 に答える