SQL 计算行进距离
SQL to calulate distance travelled
我有一个 SQL 问题正在尝试解决,这样我就可以制作一份报告,显示在用户选择的日期范围内以公里为单位行驶了多少距离。
我有两个 table 包含我需要使用的信息,我已将这两个 table 链接在一起,目前,报告显示里程表上当前显示的内容。
使用此代码:
Select FLEET_ID, ALLOCATED_STATE, DRIVER, ODO_READING, B.Date_Added
From TABLE1 as A
left outer JOIN DATEBASE_NAME..TABLE2 as B
on A.Unique_ID = B.FLEET_UNIQUE_ID
WHERE B.Unique_ID is not NULL
AND B.Date_Added between @StartDate and @EndDate
每个月都会在 table 中更新里程表两个条目。
谢谢大家!
杰克
Results of March
Results of Apirl
您想使用 lag()
。你的问题信息量有点少,但是是这样的:
select *, odo_reading - prev_odo_reading
from (Select FLEET_ID, ALLOCATED_STATE, DRIVER, ODO_READING, B.Date_Added,
lag(odo_reading) over (partition by feel_ti, driver order by b.date_added) as prev_odo_reading
From TABLE1 A left outer JOIN
DATEBASE_NAME..TABLE2 B
on A.Unique_ID = B.FLEET_UNIQUE_ID
where B.Unique_ID is not NULL
) x
where B.Date_Added between @StartDate and @EndDate
我有一个 SQL 问题正在尝试解决,这样我就可以制作一份报告,显示在用户选择的日期范围内以公里为单位行驶了多少距离。
我有两个 table 包含我需要使用的信息,我已将这两个 table 链接在一起,目前,报告显示里程表上当前显示的内容。
使用此代码:
Select FLEET_ID, ALLOCATED_STATE, DRIVER, ODO_READING, B.Date_Added
From TABLE1 as A
left outer JOIN DATEBASE_NAME..TABLE2 as B
on A.Unique_ID = B.FLEET_UNIQUE_ID
WHERE B.Unique_ID is not NULL
AND B.Date_Added between @StartDate and @EndDate
每个月都会在 table 中更新里程表两个条目。
谢谢大家! 杰克
Results of March
Results of Apirl
您想使用 lag()
。你的问题信息量有点少,但是是这样的:
select *, odo_reading - prev_odo_reading
from (Select FLEET_ID, ALLOCATED_STATE, DRIVER, ODO_READING, B.Date_Added,
lag(odo_reading) over (partition by feel_ti, driver order by b.date_added) as prev_odo_reading
From TABLE1 A left outer JOIN
DATEBASE_NAME..TABLE2 B
on A.Unique_ID = B.FLEET_UNIQUE_ID
where B.Unique_ID is not NULL
) x
where B.Date_Added between @StartDate and @EndDate