tags:

views:

71

answers:

2

I have a query below that is returning individual sales records with amounts for each order placed for a specific product SKU. How would I go about summing the total amount? The column is "extprice" that I need to sum. Any help would be appreciated, thanks...

select       partno4pt,orders.orderdate,orders.processdate,orderdetails.qty,orderdetails.extprice
from orderdetails
inner join  orders
on orderdetails.order_id = orders.order_id
where orderdate > '2009.01.17 09:00:00' 
and partnumber like '%m9150%' 
and orders.processdate is not null
A: 

You can just use the SUM aggregate function:

select partno4pt,orders.orderdate,orders.processdate,orderdetails.qty,orderdetails.extprice, 
    SUM(orderdetails.extprice) AS sumprice from orderdetails 
    inner join orders on orderdetails.order_id = orders.order_id 
    where orderdate > '2009.01.17 09:00:00' 
    and partnumber like '%m9150%' and orders.processdate is not null 
        GROUP BY partno4pt,orders.orderdate,orders.processdate,orderdetails.qty,orderdetails.extprice
Matthew Jones
The SUM will based on the `GROUP BY` - you'd get the SUM between identical `ORDERDATE`, `PROCESSDATE`, and `QTY`. The OP didn't provide details, but it's unlikely your query will return the expected value.
OMG Ponies
+4  A: 

I'm assuming a simple answer here due to it being a simple question:

select SUM(orderdetails.extprice)
from orderdetails inner join orders on orderdetails.order_id = orders.order_id
where orderdate > '2009.01.17 09:00:00' and
partnumber like '%m9150%' and orders.processdate is not null
jlech