我有一个表,该表在帐户1、2、3、4等中具有数值,并带有日期(例如:今天和昨天) 我如何计算今天和昨天的账户1、2、3、4之间的差额? 谢谢!
这是数据
+--------------+-------------+------------+
| GL | Amount | Date |
+--------------+-------------+------------+
| Bank account | 77000000 | 10/05/2019 |
| Bank account | 30373448 | 9/05/2019 |
| Cost | 49552128 | 10/05/2019 |
| Cost | 3363508.25 | 9/05/2019 |
| Div | 3050 | 10/05/2019 |
| Div | 609177.8125 | 9/05/2019 |
+--------------+-------------+------------+
答案 0 :(得分:0)
select y.account_value as yesterdays_value
, b.account_value as todays_value
, b.account_value - y.account_value as dif_in_account_value
from (select * from your_table where date_time = yesterday) y
join (select * from your_table where date_time = today) t
on y.account_id = t.account_id
答案 1 :(得分:0)
假设您使用的是Oracle,并且“日期”列为DATE
,且时间始终等于00:00:00,则可以尝试以下操作:
SELECT tday.gl AS account,
tday.amount AS todays_amount,
yday.amount AS yesterdays_amount,
tday.amount - yday.amount AS difference
FROM my_table tday
JOIN my_table yday
ON tday.gl = yday.gl
AND tday.date = yday.date + 1
WHERE tday.date = TO_DATE('20190510', 'YYYYMMDD');
当然,仅当每个帐户今天有1条记录且昨天有1条记录时,这才返回正确的结果。