【问题标题】:Delete everything except certain value and the next two rows删除除特定值和接下来的两行之外的所有内容
【发布时间】:2020-01-27 22:44:49
【问题描述】:

我需要帮助删除使用 SQL 语句,我可以删除除值包含“开始”之外的所有内容 Value1 字段和下面的下两行包含 Keep* 值。请记住,Keep* 值也可以是其他不同的值,我只是以 Keep 为例。

谢谢

**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

基本上我想要这些值

**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

【问题讨论】:

  • 如果没有两个“保留”行怎么办?同时标记您正在使用的数据库。
  • 请用您正在使用的数据库标记您的问题:mysql、sql-server、postgresql...?
  • 这是在 Mircrosoft SQL 2017 上。谢谢。
  • 您想要start 之后的任何2 行还是仅仅包含特定文本(在您的示例中为Keep*)

标签: sql sql-server sql-server-2017


【解决方案1】:

使用CTE 和lag() 窗口函数:

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, ''))

请参阅demo。
结果:

> 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 

【讨论】:

  • 谢谢你。你拥护它。
【解决方案2】:

如果ids 没有间隙,您可以这样做:

delete from t
    where (select t2.id
           from t t2
           where t2.documentname = t.documentname and
                 t2.value = 'Start'
          ) not in (t.id, t.id + 1, t.id + 2) and
          (t.value = 'Start' or t.value like 'Keep%')

重要的是,这假设每个文档都有一个起始行,并且id 中没有间隙。

【讨论】:

  • 嗨,戈丹,感谢您的帮助。您的语法中有 2 个错误。最后两行)不在 (t.id, t.id + 1, t.id + 2) 和 (t2.value = 'Start' or t2.value like 'Keep%') on t2.value 不是在子查询表中
  • 戈丹,感谢您的帮助。您的语法中有 2 个错误。你能建议吗?最后两行)不在 (t.id, t.id + 1, t.id + 2) 和 (t2.value = 'Start' or t2.value like 'Keep%') on t.id+1并且 t2.value 不在子查询表中。我收到错误消息 4104,级别 16,状态 1,第 53 行无法绑定多部分标识符“t.id”。消息 4104,级别 16,状态 1,第 54 行无法绑定多部分标识符“t2.Col1”。
  • 取回最后第二行是 on ) 不在 (t.id, t.id + 1, t.id + 2) 中,并且除了最后一行之外正在工作。另外我将最后一行 t2.value 更改为 t1.value 我遇到了另一个错误 Msg 512, Level 16, State 1, Line 45 子查询返回了超过 1 个值。当子查询跟随 =、!=、、>= 或子查询用作表达式时,这是不允许的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-07-27
  • 1970-01-01
  • 1970-01-01
  • 2016-01-19
  • 2019-08-01
  • 2015-03-27
  • 1970-01-01
相关资源
最近更新 更多