简体   繁体   English

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

[英]SQL Query to return the difference between records of two most recent dates

I have the following table: 我有下表:

**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

Now what i'm trying to do is to query the two most recent transactions and arrive at this data: 现在,我正在尝试查询两个最近的事务并获得以下数据:

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

Then using the data above I would like to compare the balances to show the difference: 然后使用上面的数据,我想比较余额以显示差异:

UserID      UserName       Difference 
---------------------------------------------------------------
10001       John Doe       -11410.00

This just shows the difference between the two previous balances (the latest and the balance before the latest) 这仅显示了之前两个余额(最新余额和最新余额之前的余额)之间的差异

Now I have the following query below. 现在,我在下面有以下查询。 This works okay to show the two most recent transactions. 可以正常显示两个最近的事务。

SELECT  
 TOP 2  *
  FROM  Table1
 WHERE  UserID  = '1001'
 ORDER
    BY  TranDate DESC

Now my issues are: 现在我的问题是:

  1. Is the sql above safe to use? 上面的sql安全使用吗? I am just relying on the sorting of the TranDate by the ORDER BY DESC keyword and I am not so sure if this is very much reliable or not. 我只是依靠ORDER BY DESC关键字对TranDate进行排序,因此我不确定这是否非常可靠。

  2. How do I select the difference between the two Balances (Row 2 - Row 1 )? 如何选择两个天平之间的差异(第2行-第1行)? I was looking for some answers online and I find stuff about self-joining. 我在网上寻找一些答案,并且发现一些有关自我加入的信息。 I tried it but it doesn't show me my desired output. 我尝试了一下,但没有显示我想要的输出。

EDIT: 编辑:

This is the closest I can get to my desired result. 这是我能得到的最接近期望结果的结果。 Can someone help me out on this please? 有人可以帮我这个忙吗? Thanks! 谢谢!

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    

Thanks a lot! 非常感谢!

You should be able to use something like the following assuming SQL Server as the RDBMS: 假定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;

See SQL Fiddle with demo . 请参阅带有演示的SQL Fiddle

Or this could be done using an INNER JOIN on the row_number value: 或者可以通过在row_number值上使用INNER JOIN来完成此操作:

;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

See SQL Fiddle with Demo 参见带有演示的SQL Fiddle

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

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