I need help to delete using SQL statement that I can delete everything except value contain 'Start' on Value1 field and the next two rows below that contain Keep* values. Bear in mind Keep* value can be other different values as well, I just use Keep as an example.
Thanks
**ID DocumentName Value1 Value2**
__ ____________ ______ _____
1 Doc1 AB 123rt
2 Doc1 CD 4543
3 Doc1 CE fgf
4 Doc1 Csafsaf fghfdg
5 Doc1 sdsaff sdsa
6 Doc1 Start From
7 Doc1 Keep2 Stay2
8 Doc1 Keep3 Stay3
9 Doc1 Stop end
10 Doc1 hjkhjkiu iuoiuio
11 Doc1 zxzfd bxgx
12 Doc2 AB 123rt
13 Doc2 CD 4543
14 Doc2 CE fgf
15 Doc2 Csafsaf fghfdg
16 Doc2 sdsaff sdsa
17 Doc2 Start From
18 Doc2 Keep5 Stay5
19 Doc2 Keep6 Stay6
20 Doc2 Stop end
21 Doc2 hjkhjkiu iuoiuio
22 Doc2 zxzfd bxgx
23 Doc3 dds gdgd
24 Doc3 jhkjh jhjkh
25 Doc3 jkkjh fgf
26 Doc3 hnmnbjkhjkiu mnbmn
27 Doc3 nmb nbmn
28 Doc3 Start From
29 Doc3 Keep7 Stay7
30 Doc3 Keep8 Stay8
31 Doc3 Stop end
Basically I want to these values
**ID DocumentName Value1 Value2**
__ ____________ ______ _____
6 Doc1 Start From
7 Doc1 Keep2 Stay2
8 Doc1 Keep3 Stay3
17 Doc2 Start From
18 Doc2 Keep5 Stay5
19 Doc2 Keep6 Stay6
28 Doc3 Start From
29 Doc3 Keep7 Stay7
30 Doc3 Keep8 Stay8
Use a CTE
and lag()
window function:
with cte as (
select *,
lag(Value1, 1) over (partition by DocumentName order by ID) prev1,
lag(Value1, 2) over (partition by DocumentName order by ID) prev2
from tablename
)
delete from cte
where 'Start' not in (Value1, coalesce(prev1, ''), coalesce(prev2, ''))
See the demo.
Results:
> ID | DocumentName | Value1 | Value2
> -: | :----------- | :----- | :-----
> 6 | Doc1 | Start | From
> 7 | Doc1 | Keep2 | Stay2
> 8 | Doc1 | Keep3 | Stay3
> 17 | Doc2 | Start | From
> 18 | Doc2 | Keep5 | Stay5
> 19 | Doc2 | Keep6 | Stay6
> 28 | Doc3 | Start | From
> 29 | Doc3 | Keep7 | Stay7
> 30 | Doc3 | Keep8 | Stay8