选择每日总销售额

时间:2018-03-05 10:10:03

标签: sql sql-server tsql datetime select

我有这样的查询,我希望每天的销售总额

SELECT DATEPART(day,deduction_timestamp) as [day],
[fare_deduction]
FROM [dbfastsprocess].[dbo].[vClosingTransitLog]
WHERE bus_id in ('JEAST', 'MKV004', 'NWTN01')
and YEAR(deduction_timestamp) = ISNULL(2016, YEAR(deduction_timestamp))
and MONTH(deduction_timestamp) = ISNULL(10, MONTH(deduction_timestamp))
GROUP BY DATEPART(day,deduction_timestamp), fare_deduction 

with result :

day              fare_deduction
--------------------------------
1                   10.00
3                   15.00
3                   2.00
4                   10.00
10                  20.00
31                  12.00

我希望结果像这样..

 day              fare_deduction
--------------------------------
1                   10.00
3                   17.00
4                   10.00
10                  20.00
31                  12.00

请注意,并非所有日期都有值,而且只显示受影响的值   只有一天。可以帮我这些吗?谢谢!

2 个答案:

答案 0 :(得分:0)

SELECT DATEPART(day,deduction_timestamp) as [day],
sum(fare_deduction) as fare_deduction
FROM [dbfastsprocess].[dbo].[vClosingTransitLog]
WHERE bus_id in ('JEAST', 'MKV004', 'NWTN01')
and YEAR(deduction_timestamp) = ISNULL(2016, YEAR(deduction_timestamp))
and MONTH(deduction_timestamp) = ISNULL(10, MONTH(deduction_timestamp))
GROUP BY DATEPART(day,deduction_timestamp)

答案 1 :(得分:0)

只需使用sum,即可获得所需的结果

SELECT DATEPART(day,deduction_timestamp) as [day],
SUM([fare_deduction]) [fare_deduction]
FROM [dbfastsprocess].[dbo].[vClosingTransitLog]
WHERE bus_id in ('JEAST', 'MKV004', 'NWTN01')
and YEAR(deduction_timestamp) = ISNULL(2016, YEAR(deduction_timestamp))
and MONTH(deduction_timestamp) = ISNULL(10, MONTH(deduction_timestamp))
GROUP BY DATEPART(day,deduction_timestamp)