如何获得特定日期的特定列的值与前一个日期的同一列的值之间的差异?
Current Table:
Date Qty
8-Jan-17 100
9-Jan-17 120
10-Jan-17 180
Desired Output:
Date Diff Qty
9-Jan-17 20
10-Jan-17 60
最佳答案
假设日期不重复并且日期之间也没有间隙,这将起作用。
--Sample data as provided. This script works in SQL Server 2005+
CREATE TABLE #Table1
([Date] datetime, [Qty] int)
;
INSERT INTO #Table1
([Date], [Qty])
VALUES
('2017-01-08 00:00:00', 100),
('2017-01-09 00:00:00', 120),
('2017-01-10 00:00:00', 180)
;
--This script is plain SQL for any DMBS
select y.Date, y.Qty-x.Qty as 'Diff Qty'
from #table1 x inner join #Table1 y
on x.Date+1=y.Date
结果
+-------------------------+----------+
| Date | Diff Qty |
+-------------------------+----------+
| 2017-01-09 00:00:00.000 | 20 |
| 2017-01-10 00:00:00.000 | 60 |
+-------------------------+----------+
关于mysql - SQL获取同一列但同一表中不同日期的值差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42404814/