I have data that records movements, such as
ID Name DateDone Moved_To By
01 John Smith 2023-08-03T15:30:23 Left JP --EG1
01 John Smith 2023-08-03T17:41:45 Right KS --EG2
01 John Smith 2023-08-03T18:58:42 Still KS
01 John Smith 2023-08-03T20:59:55 Still KS --EG2
02 Tina Platt 2023-08-04T03:11:34 Left PK
02 Tina Platt 2023-08-04T03:19:10 Left MO --EG3
02 Tina Platt 2023-08-04T06:19:49 Right MO
02 Tina Platt 2023-08-04T08:57:14 Left MO --EG4
02 Tina Platt 2023-08-04T14:05:12 Right PK
02 Tina Platt 2023-08-04T14:11:52 Right MO --EG5
02 Tina Platt 2023-08-04T16:43:02 Right MO
02 Tina Platt 2023-08-04T16:45:49 Right PK --EG6
02 Tina Platt 2023-08-05T13:21:10 - SO --EG7
Customer John Smith (01) needs one person to move his item, whereas Tina Platt (02) needs two. This is derived from the [Customers] table, and the field [num_of_ppl].
I want to highlight certain rows based on the following rules:
In the data example, the results SHOULD filter as follows:
My code is still at its early stage at the moment, but essentially:
select
case when ID = lag(ID) over(order by DateDone)
and moved_to = lag(moved_to) over(order by DateDone)
and moved_to != 'Still'
or
moved_to = lead(ID) over(order by DateDone)
and moved_to = lean(moved_to) over(order by DateDone)
and moved_to != 'Still'
then 'Repeated Move_To' end as "Flag", *
from [Movements]
Any help offered would be appreciated. Thanks.
The expected results should be:
ID Name DateDone Moved_To By Issue
02 Tina Platt 2023-08-04T14:15:11 Right MO No 2nd
02 Tina Platt 2023-08-04T14:11:52 Right MO >4hrs
02 Tina Platt 2023-08-04T16:45:49 Right PK Repeat
02 Tina Platt 2023-08-05T13:21:10 - SO No Pos
In the end I did it myself, and it does seem to work so far. This is my complete code, not just the simplified version I posted above...
DROP TABLE IF EXISTS #Results999
CREATE TABLE #Results999
(
Name nvarchar(100),
id nvarchar(10),
DateDone nvarchar(20),
[Moved To] nvarchar(100),
[By] nvarchar(100)
)
INSERT #Results999 EXEC sp_get_movementslast60d @home_name = @home_name
select
*
from
(
select
case
when
r99.id = lag(r99.id) over(order by r99.id, datedone)
and
(
[moved to] = lag([moved to]) over(order by r99.id, datedone)
)
and [moved to] not in ('S', 'I') --barring those actions
then 'Repeated Reposition'
when
r99.id = lag(r99.id) over(order by r99.id, datedone)
and datedone > dateadd(hour,4, lag(datedone) over(order by r99.id, datedone))
then '> 4hrs Since Last'
when
[moved to] is null
then 'No position specified'
when
[Outcome] = 'Declined' and [repositioned to?] =''
then 'Staff Refused' --I added this one later
when
ras.textanswer = '1|2;'
and (r99.id = lag(r99.id) over(order by r99.id, datedone)
and datedone > dateadd(minute, 30, lag(datedone) over(order by r99.id, datedone)))
then 'No 2nd'
end as "Flag", r99.*
from
#Results999 r99
left join
[customers] r on r.id = r99.id
inner join
[ras] on r.personid = ras.personid and ras.typename = 'OrdersInfo' and questionid = '11'
where
action = 'Movement'
and
datedone >= dateadd(day,-2, getdate())
) subRP
where subRP.Flag != ''
order by subRP.lastname, subRP.datedone desc
Probably far from perfect coding, but I've checked around 1000 entries now and it seems to pick up each of the issues in turn.