我想合并内部和位置基础上的行。我想查看一辆车停在特定位置的时间。在 1 里面,车辆进入特定位置, 0 表示离开特定位置。如果车辆在特定日期进入并离开相同位置,则计算 TimeLast 。例如 HD 和 BP 是车辆进入和离开的位置。
oId Number Id Location TimeFirst Inside
1639 0664 1 Hd 2016-05-29 05:48 0
1639 0664 2 Kp 2016-05-29 18:46 1
1640 0359 1 Hd 2016-05-29 13:12 0
1640 0359 1 Hd 2016-05-29 13:42 1
1640 0359 1 Hd 2016-05-29 13:46 0
1643 0812 3 Lr 2016-05-29 11:41 0
1644 0806 4 Mn 2016-05-29 06:56 0
1644 0806 5 Bp 2016-05-29 09:54 1
1644 0806 5 Bp 2016-05-29 17:43 0
输出
oId Number Id Location TimeFirst TimeLast
1639 0664 1 Hd 2016-05-29 05:48
1639 0664 2 Kp 2016-05-29 18:46
1640 0359 1 Hd 2016-05-29 13:12
1640 0359 1 Hd 2016-05-29 13:42 2016-05-29 13:46
1643 0812 3 Lr 2016-05-29 11:41
1644 0806 4 Mn 2016-05-29 06:56
1644 0806 5 Bp 2016-05-29 09:54 2016-05-29 17:43
预期输出
{{1}}
答案 0 :(得分:0)
这对您有所帮助:
private string _color;
public string Color
{
get { return _color; }
set { _color = value; }
}
答案 1 :(得分:0)
正如您在Sql Server 2012上一样,您可以使用窗口聚合函数lag
和lead
来执行此操作,这样您可以将数据与上一个或下一个记录中的数据进行比较:
select oId, Number, Id, Location, TimeFirst,
CASE WHEN inside = 1 THEN DepartureTime END DepartureTime
from ( select zs.oId, o.Number, z.Id, z.Location, zs.TimeFirst,
zs.inside,
lag(zs.inside) over (partition by o.oId, z.Id
order by zs.TimeFirst) as prev_inside,
lead(zs.inside) over (partition by o.oId, z.Id
order by zs.TimeFirst) as next_inside,
lead(zs.TimeFirst) over (partition by o.oId, z.Id
order by zs.TimeFirst) as DepartureTime
from zs
join z on zs.Id = z.Id
join o on zs.oId=o.oId
where z.GroupId in (1094,1095,1096)
and CONVERT(Date,TimeFirst)='2016/5/29'
) as base
where (inside = 1 or coalesce(prev_inside,0) = 0)
order by oId, TimeFirst