我收到了一個2列表,其中存儲了更改時間(unix_time)和更改值的事務。獲取每月的交易餘額
create table transactions (
changed int(11),
points int(11)
);
insert into transactions values (UNIX_TIMESTAMP('2014-03-27 03:00:00'), +100);
insert into transactions values (UNIX_TIMESTAMP('2014-05-02 03:00:00'), +100);
insert into transactions values (UNIX_TIMESTAMP('2015-01-01 03:00:00'), -100);
insert into transactions values (UNIX_TIMESTAMP('2015-05-01 03:00:00'), +150);
爲了得到當前的餘額,你需要總結的所有值,並從你需要總結,如果更換時間這個值是過去取得平衡小於要求,如:
select
sum(case when changed < unix_timestamp('2013-12-01') then
points
else
0
end) as cash_balance_2013_11,
...
因此每個月需要有一個單獨的SQL代碼。我希望有SQL代碼可以爲我提供所有月份的餘額。 (例如,從固定日期至今)
編輯:
你可以分成這裏的時間間隔 http://stackoverflow.com/questions/4342370/grouping-into-interval-of-5-minutes-within-a-time-range – 2014-11-20 18:28:53
@ Horst Walter:據我所知,這會在一個月內給我帶來改變。爲了獲得平衡,您不僅需要從給定的月份中總結所有以前的值。 – Pueblo 2014-11-20 18:44:13