我有一个消息日志表,按日期排序,如下所示:
Id Name Date Type Text
--------------------------------------------------------------
1 Vasya 2018/05/01 08:00 In NULL
2 Vasya 2018/05/01 09:00 Message Hello
3 Vasya 2018/05/01 18:00 Out NULL
4 Petya 2018/05/02 08:00 In NULL
----- missing part (with message of type In)
5 Vasya 2018/05/02 15:00 Message Hello
6 Vasya 2018/05/02 18:00 Out NULL
7 Petya 2018/05/02 18:10 Message Good bye
8 Petya 2018/05/02 19:00 Out NULL
----- missing part (with message of type In)
9 Masha 2018/05/03 09:00 Out NULL
----- missing part (with message of type In)
10 Ivan 2018/05/03 10:00 Hi NULL
----- missing part (with message of type Out)
他们可以发送这样一个损坏的日志,我需要通过在缺少每个用户时为每个用户添加相应的In / Out消息来进行修复。
从下往上看:
我可以通过使用每个用户的NOT EXISTS()来查找任何地方没有In消息的Out消息来做到这一点。
在这个地方,我现在意识到我需要将JOIN Out消息加入到相应的OUT消息中,从而变得更早...
谢谢
斯拉瓦
答案 0 :(得分:0)
要查找缺少的Ins for Outs,例如:
;with Ins (Id, Name, Date, Type, Text)
as
(
select l.Id, l.Name, plm.Date, plm.Type, l.Text
from Log as l
outer apply (select top 1 pl.Date, pl.Type
from Log as pl
where pl.Type in ('In', 'Out')
and pl.User = l.User
and pl.Date < l.Date
order by pl.Date desc) as plm
where l.Type = 'Out'
)
insert into Log (Id, Name, Date, Type, Text)
select Id + 1, Name,
case
when Date is null
then '2000/01/01'
else dateadd(second, -1, Date) -- make In a second earlier then Out
end as NewDate,
'In',
Text
from Ins
where Type <> 'In'
看起来像只为消息找到丢失的Ins一样