I am trying to do some calculations in my query and i think postgresql is handling the math differently than mysql is but i’m not sure where the difference is. This is my query from my controller:
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")
In my development environment i am using mysql and in my production environment i am using postgresql. These are the outputs that i am getting. For reference, the discount_percentage is 20.
MySQL:
| id | subtotal | total |
----------------------------------------
| 21 | 570.0000 | 456.00000000 |
Postgresql:
| id | subtotal | total |
----------------------------------------
| 9 | 570.0000 | 570.00000000 |
It looks like it’s something to do with the percentage and total. Anyone have any ideas? By the way, the MySQL result is what i am wanting.
Postgres is doing integer division.
So, change the
x / 100tox / 100.0to get them both to behave the same.