Mysql 每个账户每个日期的运行差异
我需要在“每日变动”上面创建一列,这将是每个账户的借方余额的每日差额。因此,账户716-05 2018年11月4日的借方余额减去716-05 2018年10月4日的借方余额。在大约一个月的时间里,总共有大约三十个帐户 我当前使用的查询:Mysql 每个账户每个日期的运行差异,mysql,Mysql,我需要在“每日变动”上面创建一列,这将是每个账户的借方余额的每日差额。因此,账户716-05 2018年11月4日的借方余额减去716-05 2018年10月4日的借方余额。在大约一个月的时间里,总共有大约三十个帐户 我当前使用的查询: account Debit_Balance Long Short Date Daily_Change (Expected) 716-05 18981100 27946000 8964860
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
试试下面的方法
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
SQL Fiddle-很抱歉,这是正确的,我的错误。再次感谢。好的祝你好运对于这个答案,您唯一需要做的就是在join上添加一个额外的条件。
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