[英]Show records only if a previous record has a certain value
I'm trouble shooting a certain business proces and i'm able to follow the flow in a database. 我在拍摄某些业务流程时遇到麻烦,并且能够跟踪数据库中的流程。 I've found the error message I want to look into, but only in case of a certain flow.
我已经找到了我想研究的错误消息,但是仅在一定流量的情况下。
My problem is how to achieve that in SQL. 我的问题是如何在SQL中实现。
For example 例如
Source Statement -------- ------------ PORT Cannot create APP No active visit GIK OIK APP No active visit
Now I only want to have the record with statement 'No active visit', if the previous record has a Source value "GIK". 现在,如果以前的记录的源值为“ GIK”,那么我只想使用语句为“无活动访问”的记录。
How do I achieve that in SQL? 如何在SQL中实现? I'm working in SSMS btw.
我在SSMS工作。
greets, Stoney 打招呼,斯托尼
You can use lag()
: 您可以使用
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';
If lag()
will not support then you can use apply
: 如果
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
tables represent unordered sets. SQL
表表示无序集。 If you want a specific ordering in the result set, you need one ordering column. 如果要在结果集中进行特定的排序,则需要一个排序列。 So, use column name instead of
?
因此,请使用列名代替
?
that specify your column ordering. 指定您的列顺序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.