繁体   English   中英

仅在先前记录具有特定值时显示记录

[英]Show records only if a previous record has a certain value

我在拍摄某些业务流程时遇到麻烦,并且能够跟踪数据库中的流程。 我已经找到了我想研究的错误消息,但是仅在一定流量的情况下。

我的问题是如何在SQL中实现。

例如

Source      Statement
--------    ------------
PORT        Cannot create
APP         No active visit
GIK         OIK
APP         No active visit

现在,如果以前的记录的源值为“ GIK”,那么我只想使用语句为“无活动访问”的记录。

如何在SQL中实现? 我在SSMS工作。

打招呼,斯托尼

您可以使用lag()

select t.*
from (select t.*,
             lag(Source) over (order by ?) as prev_source
      from table t
     ) t
where Statement = 'No active visit' and prev_source = 'GIK';

如果lag()不支持,则可以使用apply

select t.*
from table t cross apply
     (select top (1) t1.*
      from table t1 
      where t1.<identity_col> < t.<identity_col>
      order by t1.<identity_col> desc
     ) t1
where t.Statement = 'No active visit' and t1.source = 'GIK';

SQL表表示无序集。 如果要在结果集中进行特定的排序,则需要一个排序列。 因此,请使用列名代替? 指定您的列顺序。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM