返回两个最近日期的记录之间的差异的SQL查询

返回两个最近日期的记录之间的差异的SQL查询,sql,sql-order-by,self-join,Sql,Sql Order By,Self Join,我有下表: **TABLE1** RecordID UserID UserName Balance TranDate --------------------------------------------------------------- 100 10001 John Doe 10213.00 2013-02-12 00:00:00.000 101 10001 John Doe

我有下表:

**TABLE1**

RecordID    UserID      UserName       Balance     TranDate
---------------------------------------------------------------
100         10001       John Doe       10213.00    2013-02-12 00:00:00.000
101         10001       John Doe        1932.00    2013-04-30 00:00:00.000
102         10001       John Doe       10213.00    2013-03-25 00:00:00.000
103         10001       John Doe       14514.00    2013-04-12 00:00:00.000
104         10001       John Doe        5430.00    2013-02-19 00:00:00.000
105         10001       John Doe       21242.00    2010-02-11 00:00:00.000
106         10001       John Doe       13342.00    2013-05-22 00:00:00.000
现在我要做的是查询最近的两个事务,并得出以下数据:

RecordID    UserID      UserName       Balance     TranDate
---------------------------------------------------------------
106         10001       John Doe       13342.00    2013-05-22 00:00:00.000
101         10001       John Doe        1932.00    2013-04-30 00:00:00.000
然后,使用上述数据,我想比较余额以显示差异:

UserID      UserName       Difference 
---------------------------------------------------------------
10001       John Doe       -11410.00
这只是显示了前两个余额(最新余额和最新余额之前的余额)之间的差异

现在我有下面的问题。这样可以显示最近的两个事务

SELECT  
 TOP 2  *
  FROM  Table1
 WHERE  UserID  = '1001'
 ORDER
    BY  TranDate DESC
现在我的问题是:

  • 上面的sql是否可以安全使用?我只是依靠ORDER by DESC关键字对TranDate进行排序,我不确定这是否非常可靠

  • 如何选择两个余额之间的差额(第2行-第1行)?我在网上寻找一些答案,我找到了一些关于自我加入的东西。我试过了,但它没有显示我想要的输出

  • 编辑:

    这是我能得到的最接近我想要的结果。有人能帮我一下吗?谢谢

    DECLARE @SampleTable TABLE
    (
     UserID       INT,  
     UserName     VARCHAR(20),   
     Balance      DECIMAL(9,2) DEFAULT 0
    )
    
    INSERT 
      INTO  @SampleTable
           (UserID, UserName, Balance)
    SELECT  
     TOP 2  UserID,
            UserName,
            Balance
      FROM  Table1
     WHERE  UserID  = '1001'
     ORDER
        BY  TranDate DESC
    
    
     SELECT  A.UserID,
             A.UserName,
             B.Balance - A.Balance AS Difference
       FROM  @SampleTable A
       JOIN  @SampleTable B
         ON  A.UserID  = B.UserID    
    

    非常感谢

    假设SQL Server作为RDBMS,您应该能够使用如下内容:

    ;with cte as
    (
      select recordid, userid, username, balance, trandate,
        row_number() over(partition by userid order by trandate desc) rn
      from table1 
    ) 
    select c1.userid, c1.username,
      c1.balance - c2.balance diff
    from cte c1
    cross apply cte c2
    where c1.rn = 1
      and c2.rn = 2;
    

    或者,也可以使用行\编号值上的内部联接来完成此操作:

    ;with cte as
    (
      select recordid, userid, username, balance, trandate,
        row_number() over(partition by userid order by trandate desc) rn
      from table1 
    ) 
    select c1.userid, c1.username,
      c1.balance - c2.balance diff
    from cte c1
    inner join cte c2
      on c1.rn + 1 = c2.rn
    where c1.rn = 1
    

    请参见

    假设SQL Server作为RDBMS,您应该能够使用如下内容:

    ;with cte as
    (
      select recordid, userid, username, balance, trandate,
        row_number() over(partition by userid order by trandate desc) rn
      from table1 
    ) 
    select c1.userid, c1.username,
      c1.balance - c2.balance diff
    from cte c1
    cross apply cte c2
    where c1.rn = 1
      and c2.rn = 2;
    

    或者,也可以使用行\编号值上的内部联接来完成此操作:

    ;with cte as
    (
      select recordid, userid, username, balance, trandate,
        row_number() over(partition by userid order by trandate desc) rn
      from table1 
    ) 
    select c1.userid, c1.username,
      c1.balance - c2.balance diff
    from cte c1
    inner join cte c2
      on c1.rn + 1 = c2.rn
    where c1.rn = 1
    

    谢谢!这正是我需要的!我只是做了一些调整,缩小了结果范围,效果很好!谢谢这正是我需要的!我只是做了一些调整,缩小了结果范围,效果很好!