查询返回的总金额

时间:2009-11-20 23:09:55

标签: sql sql-server

我在下面有一个查询,它返回单个销售记录,其中包含针对特定产品SKU的每个订单的金额。我如何总计总金额?该列是“extprice”,我需要总结。任何帮助将不胜感激,谢谢......

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

2 个答案:

答案 0 :(得分:4)

由于这是一个简单的问题,我在这里假设一个简单的答案:

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

答案 1 :(得分:0)

您可以使用SUM聚合函数:

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