I am trying to group a example table bellow by item, with .25 price increments and the sum of qty.
| item_id | qoute | qty | --output table--> | item_id | qoute | qty | |---------|-------|-----| |---------|-------|-----| | 10 | 10.99 | 10 | | 10 | 10.00 | 20 | | 10 | 10.00 | 20 | | 10 | 10.75 | 10 | | 10 | 11.00 | 1 | | 10 | 11.00 | 1 | | 10 | 11.27 | 5 | | 10 | 11.25 | 10 | | 10 | 11.33 | 5 | | 10 | 11.50 | 10 | | 10 | 11.50 | 10 | * Notice how the two quotes of 11.27 and 11.33 are grouped together into 11.25 in the output table
I don't know if it is possible to do this with SQL or not. This will ultimately be implemented into Doctrine2 DQL, but I should be able to port any SQL to that easily.
I would really like to do this with SQL. But if it is not possible, I can probably come up with a way to do it with a algorithm.
Here is one way, using the syntax from SQL Server:
select item_id, floor(quote * 4.0)/4.0 as roundquote, sum(qty) as qty
from t
group by item_id, floor(quote * 4.0)
order by 1, 2
This multiplies the value by 4, truncates to the lower integer, and then divides by 4. This should round to the nearest $0.25.