簡體   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