在SQL中如何获取整年的数据总和

时间:2018-03-31 01:07:01

标签: sql

我的代码看起来像这样

SELECT FORMAT (sum (DISTINCT(b.Amount_Total)),'c' ) 
FROM Customer a
INNER JOIN
OrderHead b ON a.Customer_Id = b.Customer_Id
INNER JOIN
Order_ c ON b.Order_Id = c.Order_Id
WHERE Name = 'John Smith' AND b.Order_date between '2013/01/01' and '2013/12/31'

但是我不知道我是否得到了约翰在2013年所花费的金额

1 个答案:

答案 0 :(得分:1)

脱掉DISTINCT。如果他有2个100.00的订单,那么每个不是200.00美元?

SELECT FORMAT (sum(b.Amount_Total),'c' ) 
FROM Customer a
INNER JOIN
OrderHead b ON a.Customer_Id = b.Customer_Id
INNER JOIN
Order_ c ON b.Order_Id = c.Order_Id
WHERE Name = 'John Smith' AND b.Order_date between '2013/01/01' and '2013/12/31'

如果b.Order_date是日期时间并且包含时间,您将使用'2013/12/31 23:59:59'