简体   繁体   English

如何在SQL中检查下一个值是否接近上一个值

[英]How to check if next value is close to previous value in sql

I have a table which has columns 我有一个有列的表

ID int,
NUM int,
FK_POS int,
TRANS_DATE datetime

First of all, I get the rows of this table, then i want to get the next rows's values (by ID), then i want to check if next row's NUM,FK_POS and TRANS_dATE values are the same with the previous rows's value.. 首先,我获得该表的行,然后我想获得下一行的值(按ID),然后我要检查下一行的NUM,FK_POS和TRANS_dATE值是否与上一行的值相同。

For example i have; 例如我有;

"ID" "NUM" "FK_POS" "TRANS_DATE"
"1"  "429" "2043" "2015-01-11 23:40:00.000"

I want to check NUM,FK_POS and TRANS_DATE values of the second row. 我想检查第二行的NUM,FK_POS和TRANS_DATE值。

And if there is a row like this; 如果有这样的行;

"2"  "429" "2043" "2015-01-11 23:40:00.000"

That's what I want; 这就是我想要的; is this possible? 这可能吗?

If you are using SQL Server 2012+, then you can use lead() : 如果您使用的是SQL Server 2012+,则可以使用lead()

select t.*,
       (case when num = lead(num) over (order by id) and
                  fk_pos = lead(fk_pos) over (order by id) and
                  trans_date = lead(trans_date) over (order by id)
             then 1 else 0
        end) as SameAsNext
from table t;

If you are using an earlier version, you can do something very similar with cross apply : 如果您使用的是早期版本,则可以使用cross apply做一些非常相似的事情:

select t.*,
       (case when t.num = tnext.num and
                  t.fk_pos = tnext.fk_pos and
                  t.trans_date = tnext.rans_date
             then 1 else 0
        end) as SameAsNext
 from table t cross apply
     (select top 1 t2.*
      from table t2
      where t2.id > t.id
      order by t2.id asc
     ) tnext

You can join the table with itself: 您可以将表与自身连接:

SELECT ID, NUM, FK_POS, TRANS_DATE,
       NextRowHasSameValues = CASE 
          WHEN t2.ID IS NOT NULL
           AND t1.NUM = t2.NUM
           AND t1.FK_POS = t2.FK_POS 
           AND t1.TRANS_DATE = t2.TRANS_DATE 
          THEN 'Yes' ELSE 'No' END
FROM dbo.TableName t1
LEFT OUTER JOIN dbo.TablName t2
    ON t1.ID = t2.ID - 1

Something like this? 像这样吗

SELECT *
FROM table a JOIN table b on b.id = a.id + 1
WHERE a.NUM = b.NUM AND etc...

The first way I can think of doing this (there might be a better way) is using a where clause and EXISTS 我想到的第一种方法(可能会有更好的方法)是使用where子句和EXISTS

SELECT ID, NUM, FK_POS, TRANS_DATE
    FROM tblTable pt
        WHERE EXISTS(SELECT 1 FROM tblTable ct WHERE pt.NUM = ct.NUM AND pt.FK_POS = ct.FK_POS AND pt.TRANS_DATE = ct.TRANS_DATE AND pt.ID + 1 = ct.ID)
        /*This is assuming your ID is an Identity field*/

If you don't care if it's the previous row, you can use several better methods: 如果您不在乎上一行,则可以使用几种更好的方法:

The first, and probably best, is by using GROUP BY 第一个,也许也是最好的,是使用GROUP BY

SELECT MIN(ID)/*or MAX(ID)*/, NUM, FK_POS, TRANS_DATE
    FROM tblTable
        GROUP BY NUM, FK_POS, TRANS_DATE
            HAVING COUNT(*) > 1

Or, you can use DISTINCT with subqueries 或者,您可以对子查询使用DISTINCT

SELECT ID, NUM, FK_POS, TRANS_DATE
    FROM tblTable
        WHERE ID NOT IN (SELECT DISTINCT NUM, FK_POS, TRANS_DATE FROM tblTable)

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

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