在同一个表中查找记录之间的Datediff

时间:2016-10-17 13:47:53

标签: sql-server tsql

IP             QID  ScanDate                Rank
101.110.32.80   6   2016-09-28 18:33:21.000 3
101.110.32.80   6   2016-08-28 18:33:21.000 2
101.110.32.80   6   2016-05-30 00:30:33.000 1

我有一个包含某些记录的表,按Ipaddress和QID分组。我的要求是找出哪个记录​​错过了日期列中的序列,或者其他词语的日期差异超过30天。在上表中,排名1和排名2之间的日期差异超过30天。所以,我应该标记排名2记录。

3 个答案:

答案 0 :(得分:1)

虽然Window Functions可以在这里使用,但我认为自我加入可能更直接且更容易理解:

SELECT
t1.IP,
    t1.QID,
    t1.Rank,
    t1.ScanDate as endScanDate,
    t2.ScanDate as beginScanDate,
    datediff(day, t2.scandate, t1.scandate) as scanDateDays     
FROM
    table as t1
    INNER JOIN table as t2 ON
        t1.ip = t2.ip
        t1.rank - 1 = t2.rank --get the record from t2 and is one less in rank
WHERE datediff(day, t2.scandate, t1.scandate) > 30 --only records greater than 30 days

这是不言自明的。我们正在加入这个表并加入排名,其中排名2加入排名1,排名3加入排名2,依此类推。然后我们使用datediff函数测试大于30天的记录。

答案 1 :(得分:1)

您可以在Sql 2012 +

中使用LAG
declare @Tbl Table (Ip VARCHAR(50), QID INT, ScanDate DATETIME,[Rank] INT)

INSERT INTO @Tbl        
VALUES  
('101.110.32.80', 6,   '2016-09-28 18:33:21.000', 3),
('101.110.32.80', 6,   '2016-08-28 18:33:21.000', 2),
('101.110.32.80', 6,   '2016-05-30 00:30:33.000', 1)

;WITH Result
AS
(
    SELECT
        T.Ip ,
        T.QID ,
        T.ScanDate ,
        T.[Rank],
        LAG(T.[Rank]) OVER (ORDER BY T.[Rank]) PrivSRank,
        LAG(T.ScanDate) OVER (ORDER BY T.[Rank]) PrivScanDate
    FROM
        @Tbl T
) 

SELECT
    R.Ip ,
    R.QID ,
    R.ScanDate ,
    R.Rank ,
    R.PrivScanDate,
    IIF(DATEDIFF(DAY, R.PrivScanDate, R.ScanDate) > 30, 'This is greater than 30 day. Rank ' + CAST(R.PrivSRank AS VARCHAR(10)), '') CFlag
FROM
    Result R

结果:

Ip                       QID         ScanDate                Rank        CFlag
------------------------ ----------- ----------------------- ----------- --------------------------------------------
101.110.32.80            6           2016-05-30 00:30:33.000 1           
101.110.32.80            6           2016-08-28 18:33:21.000 2           This is greater than 30 day. Rank 1
101.110.32.80            6           2016-09-28 18:33:21.000 3           This is greater than 30 day. Rank 2

答案 2 :(得分:0)

我会使用窗口函数来避免自联接,这在很多情况下会表现得更好。

<!DOCTYPE html>
<html>
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<style>
.dropdown-submenu {
    position: relative;
}

.dropdown-submenu .dropdown-menu {
    top: 0;
    left: 100%;
    margin-top: -1px;
}
</style>
</head>
<body>
   
<div class="container">
  <h2>Multi-Level Dropdowns</h2>
  <p>In this example, we have created a .dropdown-submenu class for multi-level dropdowns (see style section above).</p>
  <p>Note that we have added jQuery to open the multi-level dropdown on click (see script section below).</p>
  <div class="dropdown">
    <button class="btn btn-default dropdown-toggle" type="button" data-toggle="dropdown">Tutorials
    <span class="caret"></span></button>
    <ul class="dropdown-menu">
      <li><a tabindex="-1" href="#">HTML</a></li>
      <li><a tabindex="-1" href="#">CSS</a></li>
      <li class="dropdown-submenu">
        <a class="test" tabindex="-1" href="#">New dropdown <span class="caret"></span></a>
        <ul class="dropdown-menu">
          <li><a tabindex="-1" href="#">2nd level dropdown</a></li>
          <li><a tabindex="-1" href="#">2nd level dropdown</a></li>
          <li class="dropdown-submenu">
            <a class="test" href="#">Another dropdown <span class="caret"></span></a>
            <ul class="dropdown-menu">
              <li><a href="#">3rd level dropdown</a></li>
              <li><a href="#">3rd level dropdown</a></li>
            </ul>
          </li>
        </ul>
      </li>
    </ul>
  </div>
</div>


<script>
$(document).ready(function(){
  $('.dropdown-submenu a.test').on("click", function(e){
    $(this).next('ul').toggle();
    e.stopPropagation();
    e.preventDefault();
  });
});
</script>

</body>
</html>