我有一系列每天加油的车辆,输入总燃料和里程表读数。我需要总结一下每辆车每个月的行驶里程。 我的表是燃料,车辆是公共汽车,日期是service_date,里程表读数是里程,输入的燃料是数量。
因此,我将从每个月的第一天开始计算里程,然后从上个月的第一天开始减去每辆车的里程数,在这种情况下为3237。更糟糕的是,如果第一天没有加油,我会想要从上个月加油的最后一天开始的里程。
service_date bus mileage quantity
7/1/2018 202 149654 34
7/3/2018 202 150256 40.5
7/4/2018 202 150562 42
7/6/2018 202 150853 41
7/7/2018 202 151191 37
7/8/2018 202 151323 23.6
7/15/2018 202 151502 39
7/13/2018 202 151806 45
8/1/2018 202 152891 37 3237
答案 0 :(得分:1)
不确定我是否理解您的所有特定要求,但是这是我编写查询以报告公交车队里程的方法。
首先,我将使用GROUP BY
和MAX
这样来获得每个月的结束里程:
SELECT bus_id
, MAX(bus_mileage) AS 'ending_mileage'
, DATEPART(YEAR, service_date) AS 'year'
, DATEPART(MONTH, service_date) AS 'month'
, MAX(service_date) AS 'last_service_date'
FROM @mileage
GROUP BY bus_id
, DATEPART(YEAR, service_date)
, DATEPART(MONTH, service_date)
然后,我将其用作子查询来获取上个月的结束里程。这将为您提供当月的起始里程。
SELECT bus_id
, ending_mileage
, (
SELECT MAX(bus_mileage)
FROM @mileage m
WHERE DATEPART(year, m.service_date) = DATEPART(YEAR, DATEADD(MONTH, -1, bus_mileage.last_service_date))
AND DATEPART(MONTH, m.service_date) = DATEPART(MONTH, DATEADD(MONTH, -1, bus_mileage.last_service_date))
AND m.bus_id = bus_mileage.bus_id
) AS 'starting_mileage'
, mileage_year
, mileage_month
FROM (
SELECT bus_id
, MAX(bus_mileage) AS 'ending_mileage'
, DATEPART(YEAR, service_date) AS 'mileage_year'
, DATEPART(MONTH, service_date) AS 'mileage_month'
, MAX(service_date) AS 'last_service_date'
FROM @mileage
GROUP BY bus_id
, DATEPART(YEAR, service_date)
, DATEPART(MONTH, service_date)
) bus_mileage
然后,我将整个内容包装在一个外部查询中,该查询从结束里程中减去开始里程。
SELECT bus_id
, ending_mileage
, starting_mileage
, ending_mileage - starting_mileage AS 'mileage_for_the_month'
, mileage_year
, mileage_month
FROM (
SELECT bus_id
, ending_mileage
, (
SELECT MAX(bus_mileage)
FROM @mileage m
WHERE DATEPART(year, m.service_date) = DATEPART(YEAR, DATEADD(MONTH, -1, bus_mileage.last_service_date))
AND DATEPART(MONTH, m.service_date) = DATEPART(MONTH, DATEADD(MONTH, -1, bus_mileage.last_service_date))
AND m.bus_id = bus_mileage.bus_id
) AS 'starting_mileage'
, mileage_year
, mileage_month
FROM (
SELECT bus_id
, MAX(bus_mileage) AS 'ending_mileage'
, DATEPART(YEAR, service_date) AS 'mileage_year'
, DATEPART(MONTH, service_date) AS 'mileage_month'
, MAX(service_date) AS 'last_service_date'
FROM @mileage
GROUP BY bus_id
, DATEPART(YEAR, service_date)
, DATEPART(MONTH, service_date)
) bus_mileage
) monthly_mileage
这是演示数据的全部内容,因此您可以看到它们如何协同工作。
DECLARE @mileage TABLE(
service_date DATE NOT NULL
, bus_id INT NOT NULL
, bus_mileage INT NOT NULL
)
INSERT INTO @mileage (service_date, bus_id, bus_mileage)
VALUES ('7/1/2018', 202, 149654)
, ('7/15/2018', 202, 151502)
, ('8/1/2018', 202, 152891)
, ('8/15/2018', 202, 153502)
, ('9/3/2018', 202, 154891)
, ('10/15/2018', 202, 155502)
, ('11/3/2018', 202, 157891)
, ('7/5/2018', 302, 155502)
, ('8/3/2018', 302, 157691)
SELECT bus_id
, ending_mileage
, starting_mileage
, ending_mileage - starting_mileage AS 'mileage_for_the_month'
, mileage_year
, mileage_month
FROM (
SELECT bus_id
, ending_mileage
, (
SELECT MAX(bus_mileage)
FROM @mileage m
WHERE DATEPART(year, m.service_date) = DATEPART(YEAR, DATEADD(MONTH, -1, bus_mileage.last_service_date))
AND DATEPART(MONTH, m.service_date) = DATEPART(MONTH, DATEADD(MONTH, -1, bus_mileage.last_service_date))
AND m.bus_id = bus_mileage.bus_id
) AS 'starting_mileage'
, mileage_year
, mileage_month
FROM (
SELECT bus_id
, MAX(bus_mileage) AS 'ending_mileage'
, DATEPART(YEAR, service_date) AS 'mileage_year'
, DATEPART(MONTH, service_date) AS 'mileage_month'
, MAX(service_date) AS 'last_service_date'
FROM @mileage
GROUP BY bus_id
, DATEPART(YEAR, service_date)
, DATEPART(MONTH, service_date)
) bus_mileage
) monthly_mileage
不确定这是否完全符合您的要求,但这是我根据您提供的信息报告里程的方式。