我知道有幾個關於累計總數的答案。我已經嘗試過,並沒有找到解決我的問題的方法。獲取計數()其中created_date是累積和日期爲基礎
Here是一個sqlfiddle。
我們有兩個字段,EID和CREATE_TIME聯繫人表:
eid create_time
991772 April, 21 2016 11:34:21
989628 April, 17 2016 02:19:57
985557 April, 04 2016 09:56:39
981920 March, 30 2016 11:03:12
981111 March, 30 2016 09:36:48
我想我們的聯繫人數據庫的大小一起在每個月底在每個月選擇新的聯繫人數量月。按年份和月份的新聯繫人很簡單。 Contacts表在每個月底的大小我做了一些研究和found what looked to be a straight forwards method:
set @csum = 0;
select
year(c.create_time) as yr,
month(c.create_time) as mth,
count(c.eid) as new_contacts,
(@csum + count(c.eid)) as cumulative_contacts
from
contacts c
group by
yr,
mth
運行,但讓我意外的結果。
如果我運行:
select count(*) from contacts where date(create_time) < current_date
我得到的總記錄數表中的146
因此我使用@csum有146 2016年4月預計,最終排在我的查詢。它只有3?
我的目標是什麼字段cumulative_contacts: 對於記錄與例如2016年一月
select count(*) from contacts where date(create_time) < '2016-02-01';
而2月的記錄將有:
select count(*) from contacts where date(create_time) < '2016-03-01';
等等
sqlfiddle鏈接不工作 – KaeL