Given the following SQL query:
SELECT orders.id, orders.shop_id,
products.price * line_items.quantity as total_value,
line_items.description, line_items.id as lineitem_id,
line_items.order_id, products.price as price,
line_items.product_id, line_items.quantity
from orders
JOIN line_items
ON line_items.order_id = orders.id
JOIN products
ON line_items.product_id = products.id;
produces the following result:
I'm trying to use SUM()
to add together all price
columns for the same order_id
and have tried this new query:
SELECT orders.id, orders.shop_id, SUM(products.price),
line_items.description, line_items.id as lineitem_id,
line_items.order_id, products.price as price,
line_items.product_id, line_items.quantity
from orders
JOIN line_items
ON line_items.order_id = orders.id
LEFT JOIN products
ON line_items.product_id = products.id
GROUP BY orders.id;
which does its job correctly (see below).
However, I still want to retain each row as shown in the first image AND have the total_value
column summed.
In the first image, the total_value
columns for the first and second row (with an order_id
of 1
) should be 2199.98
- is this possible?