简体   繁体   English

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

[英]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.

相关问题 计算子记录具有特定值的每一天的记录 - Counting records for each day where a child record has a certain value 有没有办法返回所有记录,其中特定列上的下一条记录的值来自上一条记录 + 1? - Is there a way to return all records where the next record on a specific column has a value from previous record + 1? sqlite-如果两个记录在某些列中存在不同的值,则仅选择某些记录 - sqlite - where two records exist with a different value in certain column, only select certain record SQL查询返回多列中具有特定条件的记录,并且仅存在具有特定值的一条记录 - SQL Query to return records with certain conditions in several columns, and where only one record with a certain value exists 仅显示在下一条记录中具有相同值的第一列 - Show only the first column which has the same value on next record 如何选择字段具有特定值的所有记录,直到显示具有不同值的记录? - How to select all records where a field is of a certain value until a record shows up that has a different value? 记录具有多种状态,需要重新检查。 只想要符合特定条件的记录 - Record has multiple statuses that gets rechecked. Only want records that meet a certain criteria 连接没有记录时的SQL显示记录 - SQL show record when join has no records 加载更多记录脚本,如果添加了新记录,则显示上一组记录中的最后一条记录 - load more records script show the same last record from a previous set of records if a new record is added MYSQL - 仅选择先前记录列数据不同的记录 - MYSQL - Select Only Records Where Previous Record Column Data Differs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM