account Debit_Balance Long Short Date Daily_Change (Expected)
716-05 18981100 27946000 8964860 4/10/2018 0
716-06 -7526070 1676250 9202320 4/10/2018 0
716-07 6596930 26579600 19982700 4/10/2018 0
716-11 -1555190 3298790 4853980 4/10/2018 0
716-05 12861700 20754400 7892750 4/11/2018 -6119400
716-06 -8717010 1585470 10302500 4/11/2018 -1190940
716-07 7900390 28052300 20151900 4/11/2018 1303460
716-11 -1641360 3482290 5123650 4/11/2018 -86170
我需要在“Daily_Change”上方创建列,这将是每个帐户 Debit_Balance 的每日差异。因此,帐户 716-05 的 4/11/2018 的 Debit_Balance 减去 716-05 的 4/10/2018 的 Debit_Balance。在大约一个月的时间里,总共有大约三十个帐户。
我当前使用的查询:
select account, balance as Debit_Balance, int_balance as "Long",
short_mkt_value as Short, report_date as Date
from table
where group_name = "Carter"
and report_date in
(
select report_date
from table
group by report_date
order by account asc)
order by report_date asc, account asc
最佳答案
尝试以下操作
SELECT t1.*,t1.balance-t2.balance Daily_Change
FROM TestData t1
LEFT JOIN TestData t2
ON t2.report_date=DATE_SUB(t1.report_date, INTERVAL 1 DAY)
AND t1.account=t2.account
ORDER BY t1.report_date,t1.account
SQL fiddle - http://www.sqlfiddle.com/#!9/033d7b/10
关于mysql - 每个账户每个日期的运行差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49934510/