简体   繁体   English

SQL Server:查找大于 5 的最近连续记录

[英]SQL Server : find recent consecutive records that are greater than 5

I need to write a query that shows the result broken down by FormID that have a value greater than 5 based on the most recent LogDate .我需要编写一个查询,显示结果通过分解FormID有基于最新的值大于5 LogDate

Based on the most recent LogDate , if there was a value that was less than 5, it should display values from that point that are greater than 5 as the values under 5 is a 'reset' if you will.根据最新的LogDate ,如果有一个小于 5 的值,它应该显示从那个点开始的大于 5 的值,因为如果您愿意,小于 5 的值是“重置”。

I am essentially looking at recent consecutive LogDate records that are greater than 5.我基本上是在查看最近连续的大于 5 的LogDate记录。

Say we have the following record set:假设我们有以下记录集:

FormID   Value  LogDate    
--------------------------
Form2    6      10/12/19   
Form2    7      10/13/19   
Form1    8      10/12/19
Form1    12     10/12/19
Form1    3      10/14/19
Form1    8      10/15/19
Form1    6      10/21/19  

The following would return the following (please note I like to show the row_num as well:以下将返回以下内容(请注意,我也喜欢显示 row_num:

 FormID   Value  LogDate   row_num
 ----------------------------------
 Form2    6      10/12/19  1
 Form2    7      10/13/19  2
 Form1    8      10/15/19  1
 Form1    6      10/21/19  2

Note in the example above, since the following record had a recent value under 5 (value of 3), we need to get the records that are above 5.请注意,在上面的示例中,由于以下记录的最近值低于 5(值为 3),因此我们需要获取高于 5 的记录。

Another example:另一个例子:

FormID   Value  LogDate     
Form1    8      10/15/19
Form1    3      10/21/19  

RESULT: No result would be shown as there are in recent record that is greater than 5结果:不会显示任何结果,因为在最近的记录中有大于 5

Another example:另一个例子:

FormID   Value  LogDate    
Form2    4      10/12/19   
Form2    3      10/13/19   
Form1    16     10/12/19
Form1    3      10/12/19
Form1    3      10/14/19
Form1    8      10/15/19
Form1    12     10/21/19 

Result here would be:这里的结果是:

FormID   Value  LogDate   row_num
Form1    8      10/15/19  1
Form1    12     10/21/19  2

Another example:另一个例子:

FormID   Value  LogDate    
Form1    12      10/12/19   
Form2    13      10/13/19  

Result:结果:

FormID   Value  LogDate    row_num
Form1    12      10/12/19  1 
Form2    13      10/13/19  2

From my understanding, this can be done with the LAG function but not sure how to put it altogether.根据我的理解,这可以通过 LAG 函数来完成,但不确定如何完全放置。

We can do something like the following:我们可以执行以下操作:

   DECLARE @mytable TABLE
   (
     FormID VARCHAR(50), 
     [Value] INT, 
     LogDate DATETIME
    )

    select t.*, 
        lag(value) over(partition by formid order by logdate) lag_value
    from @mytablet

But not sure how to pull it all together.但不知道如何把它拉在一起。

If I follow you correctly, you can do this with window functions like this:如果我正确地跟随你,你可以用这样的窗口函数来做到这一点:

select 
from (
    select t.*, 
        row_number() over(partition by formid order by logdate desc) rn,
        sum(case when value > 5 then 1 else 0 end) over(partition by formid order by logdate desc) grp
    from mytable t
) t
where rn = grp

The idea is to compare the number of values above 5 to a row number, counting from the most recent value.这个想法是将大于5的值与行号进行比较,从最近的值开始计数。 Rows where the two values are equal can be retained.可以保留两个值相等的行。

One method is:一种方法是:

select t.*,
       row_number() over (partition by formid order by logdate)
from t
where t.logdate > (select coalesce(max(t.logdate), '2000-01-01')
                   from t t2
                   where t2.formid = t.formid and t.value <= 5
                  );

You can also use window functions:您还可以使用窗口函数:

select t.*,
       row_number() over (partition by formid order by logdate)
from (select t.*,
             max(case when value <= 5 then logdate end) over (partition by formid) as logdate_5
      from t
     ) t
where logdate_5 is null or
      date > logdate_5
order by formid, logdate;

Find an indicative answer in fiddle .fiddle 中找到指示性答案。

The reset_calendar is the dates that a reset happened and is used to filter out the data. reset_calendar是重置发生的日期,用于过滤数据。

SELECT temp.*,
       ROW_NUMBER() OVER (PARTITION BY temp.FormID ORDER BY temp.LogDate) AS Sequence
FROM (
  SELECT t.*
  FROM t
  LEFT JOIN (
    SELECT FormID, MAX(LogDate) AS recent_reset 
    FROM t
    WHERE Value<6
    GROUP BY FormID) AS reset_calendar
  ON t.FormID = reset_calendar.FormID
  WHERE t.LogDate > reset_calendar.recent_reset OR reset_calendar.recent_reset IS NULL)temp

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

相关问题 SQL:检查n个连续记录是否大于某个值 - SQL: Check if n consecutive records are greater than some value SQL Server查找连续失败记录 - SQL Server find consecutive failure records SQL 查询 - 查找具有基数大于 1 的字段的记录 - SQL Query - Find records with field that has cardinality greater than one 查找3个或更多连续交易记录,其中交易金额大于100且属于同一类别的记录 - Find 3 or more consecutive transaction record where the transaction amount greater than 100 and the records belong to the same category SQL Server Delete记录大于去年的这个时间 - SQL Server Delete records greater than this time last year SQL Server查询以查找间隔为12个月或更长时间的记录 - SQL Server query to find records with gap or 12 months or greater SQL 服务器查询查找最后出现的大于 0 的数字。如果没有大于 0 的数字,则查询应返回 0 - SQL Server query to find last occurred number greater than 0. If there is no number greater than 0 then query should return 0 SQL查询以查找最新记录组 - SQL Query to find the most recent group of records SQL Server-日期,大于和小于 - SQL Server - Dates, Greater than and Less than 在SQL Server中从子表中获取计数大于0的记录 - get records from child table having count greater than 0 in sql server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM