我需要总结每辆车每月的里程 运行
I need to sum the mileage ran each month per vehicle
我有一系列的车辆每天都加油,输入总油量和里程表读数。我需要总结每辆车每月的里程 运行。
我的 table 是燃料,车辆是公共汽车,日期是 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
不确定我是否理解您的所有具体要求,但我将通过以下方式编写查询来报告公交车队的里程数。
首先,我会使用 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
不确定这是否完全符合您的要求,但这是我根据您提供的信息报告里程的方式。
我有一系列的车辆每天都加油,输入总油量和里程表读数。我需要总结每辆车每月的里程 运行。 我的 table 是燃料,车辆是公共汽车,日期是 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
不确定我是否理解您的所有具体要求,但我将通过以下方式编写查询来报告公交车队的里程数。
首先,我会使用 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
不确定这是否完全符合您的要求,但这是我根据您提供的信息报告里程的方式。