选择每天的总销售额

问题描述 投票:-1回答:2

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

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

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

sql sql-server tsql datetime select
2个回答
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)

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)
© www.soinside.com 2019 - 2024. All rights reserved.