這很長,但適合於RDBMS的意圖。它會這樣做......基於單一年的基礎......如果你的數據跨越了數年,只需在每個選擇/聯盟中添加適用的「年」where子句,查詢的前提是預先聲明每個月,所以我們知道「截止日期」在某個月的聚合情況下的位置(即:Jan是所有小於2月的總和,2月是總和到Mar等)。 每個人都需要每個月自己的樞軸彙總,這是什麼讓這個漫長的,但是,很容易得到你的結果。不是我如何計劃進行這樣的查詢,尤其是在一個大集合上 - 我希望每個月/每年的預期聚合具有「彙總」值的單個表,並且在適用的情況下,具有觸發器只需根據需要更新計數,點數。
set @jan := date("2011-01-01");
set @feb := date("2011-02-01");
set @mar := date("2011-03-01");
set @apr := date("2011-04-01");
set @may := date("2011-05-01");
set @jun := date("2011-06-01");
set @jul := date("2011-07-01");
set @aug := date("2011-08-01");
set @sep := date("2011-09-01");
set @oct := date("2011-10-01");
set @nov := date("2011-11-01");
set @decem := date("2011-12-01");
set @nextYr := date("2012-01-01");
select
'Active Users' as Detail,
sum(if(pt.status = 1 and pt.created_at < @feb, 1, 0)) January,
sum(if(pt.status = 1 and pt.created_at < @mar, 1, 0)) February,
sum(if(pt.status = 1 and pt.created_at < @apr, 1, 0)) March,
sum(if(pt.status = 1 and pt.created_at < @may, 1, 0)) April,
sum(if(pt.status = 1 and pt.created_at < @jun, 1, 0)) May,
sum(if(pt.status = 1 and pt.created_at < @jul, 1, 0)) June,
sum(if(pt.status = 1 and pt.created_at < @aug, 1, 0)) July,
sum(if(pt.status = 1 and pt.created_at < @sep, 1, 0)) August,
sum(if(pt.status = 1 and pt.created_at < @oct, 1, 0)) September,
sum(if(pt.status = 1 and pt.created_at < @nov, 1, 0)) October,
sum(if(pt.status = 1 and pt.created_at < @decem, 1, 0)) November,
sum(if(pt.status = 1 and pt.created_at < @nextYr, 1, 0)) December
from
profile_table pt
union
select
'Inactive Users' as Detail,
sum(if(pt.status = 0 and pt.created_at < @feb, 1, 0)) January,
sum(if(pt.status = 0 and pt.created_at < @mar, 1, 0)) February,
sum(if(pt.status = 0 and pt.created_at < @apr, 1, 0)) March,
sum(if(pt.status = 0 and pt.created_at < @may, 1, 0)) April,
sum(if(pt.status = 0 and pt.created_at < @jun, 1, 0)) May,
sum(if(pt.status = 0 and pt.created_at < @jul, 1, 0)) June,
sum(if(pt.status = 0 and pt.created_at < @aug, 1, 0)) July,
sum(if(pt.status = 0 and pt.created_at < @sep, 1, 0)) August,
sum(if(pt.status = 0 and pt.created_at < @oct, 1, 0)) September,
sum(if(pt.status = 0 and pt.created_at < @nov, 1, 0)) October,
sum(if(pt.status = 0 and pt.created_at < @decem, 1, 0)) November,
sum(if(pt.status = 0 and pt.created_at < @nextYr, 1, 0)) December
from
profile_table pt
union
select
'Total Points' as Detail,
sum(pt.points * if(pt.status = 1 and pt.created_at < @feb, 1, 0)) January,
sum(pt.points * if(pt.status = 1 and pt.created_at < @mar, 1, 0)) February,
sum(pt.points * if(pt.status = 1 and pt.created_at < @apr, 1, 0)) March,
sum(pt.points * if(pt.status = 1 and pt.created_at < @may, 1, 0)) April,
sum(pt.points * if(pt.status = 1 and pt.created_at < @jun, 1, 0)) May,
sum(pt.points * if(pt.status = 1 and pt.created_at < @jul, 1, 0)) June,
sum(pt.points * if(pt.status = 1 and pt.created_at < @aug, 1, 0)) July,
sum(pt.points * if(pt.status = 1 and pt.created_at < @sep, 1, 0)) August,
sum(pt.points * if(pt.status = 1 and pt.created_at < @oct, 1, 0)) September,
sum(pt.points * if(pt.status = 1 and pt.created_at < @nov, 1, 0)) October,
sum(pt.points * if(pt.status = 1 and pt.created_at < @decem, 1, 0)) November,
sum(pt.points * if(pt.status = 1 and pt.created_at < @nextYr, 1, 0)) December
from
profile_table pt
union
select
'Spend Points' as Detail,
sum(sp.spend_points * if(sp.price_id = 14 and sp.created_at < @feb, 1, 0)) January,
sum(sp.spend_points * if(sp.price_id = 14 and sp.created_at < @mar, 1, 0)) February,
sum(sp.spend_points * if(sp.price_id = 14 and sp.created_at < @apr, 1, 0)) March,
sum(sp.spend_points * if(sp.price_id = 14 and sp.created_at < @may, 1, 0)) April,
sum(sp.spend_points * if(sp.price_id = 14 and sp.created_at < @jun, 1, 0)) May,
sum(sp.spend_points * if(sp.price_id = 14 and sp.created_at < @jul, 1, 0)) June,
sum(sp.spend_points * if(sp.price_id = 14 and sp.created_at < @aug, 1, 0)) July,
sum(sp.spend_points * if(sp.price_id = 14 and sp.created_at < @sep, 1, 0)) August,
sum(sp.spend_points * if(sp.price_id = 14 and sp.created_at < @oct, 1, 0)) September,
sum(sp.spend_points * if(sp.price_id = 14 and sp.created_at < @nov, 1, 0)) October,
sum(sp.spend_points * if(sp.price_id = 14 and sp.created_at < @decem, 1, 0)) November,
sum(sp.spend_points * if(sp.price_id = 14 and sp.created_at < @nextYr, 1, 0)) December
from
spend_points sp
union
select
'Points So Far' as Detail,
PointsPerMonth.January + SpendPerMonth.January January,
PointsPerMonth.February + SpendPerMonth.February February,
PointsPerMonth.March + SpendPerMonth.March March,
PointsPerMonth.April + SpendPerMonth.April April,
PointsPerMonth.May + SpendPerMonth.May May,
PointsPerMonth.June + SpendPerMonth.June June,
PointsPerMonth.July + SpendPerMonth.July July,
PointsPerMonth.August + SpendPerMonth.August August,
PointsPerMonth.September + SpendPerMonth.September September,
PointsPerMonth.October + SpendPerMonth.October October,
PointsPerMonth.November + SpendPerMonth.November November,
PointsPerMonth.December + SpendPerMonth.December December
from
(select
sum(pt.points * if(pt.status = 1 and pt.created_at < @feb, 1, 0)) January,
sum(pt.points * if(pt.status = 1 and pt.created_at < @mar, 1, 0)) February,
sum(pt.points * if(pt.status = 1 and pt.created_at < @apr, 1, 0)) March,
sum(pt.points * if(pt.status = 1 and pt.created_at < @may, 1, 0)) April,
sum(pt.points * if(pt.status = 1 and pt.created_at < @jun, 1, 0)) May,
sum(pt.points * if(pt.status = 1 and pt.created_at < @jul, 1, 0)) June,
sum(pt.points * if(pt.status = 1 and pt.created_at < @aug, 1, 0)) July,
sum(pt.points * if(pt.status = 1 and pt.created_at < @sep, 1, 0)) August,
sum(pt.points * if(pt.status = 1 and pt.created_at < @oct, 1, 0)) September,
sum(pt.points * if(pt.status = 1 and pt.created_at < @nov, 1, 0)) October,
sum(pt.points * if(pt.status = 1 and pt.created_at < @decem, 1, 0)) November,
sum(pt.points * if(pt.status = 1 and pt.created_at < @nextYr, 1, 0)) December
from
profile_table pt) PointsPerMonth,
(select
sum(sp.spend_points * if(sp.price_id = 14 and sp.created_at < @feb, 1, 0)) January,
sum(sp.spend_points * if(sp.price_id = 14 and sp.created_at < @mar, 1, 0)) February,
sum(sp.spend_points * if(sp.price_id = 14 and sp.created_at < @apr, 1, 0)) March,
sum(sp.spend_points * if(sp.price_id = 14 and sp.created_at < @may, 1, 0)) April,
sum(sp.spend_points * if(sp.price_id = 14 and sp.created_at < @jun, 1, 0)) May,
sum(sp.spend_points * if(sp.price_id = 14 and sp.created_at < @jul, 1, 0)) June,
sum(sp.spend_points * if(sp.price_id = 14 and sp.created_at < @aug, 1, 0)) July,
sum(sp.spend_points * if(sp.price_id = 14 and sp.created_at < @sep, 1, 0)) August,
sum(sp.spend_points * if(sp.price_id = 14 and sp.created_at < @oct, 1, 0)) September,
sum(sp.spend_points * if(sp.price_id = 14 and sp.created_at < @nov, 1, 0)) October,
sum(sp.spend_points * if(sp.price_id = 14 and sp.created_at < @decem, 1, 0)) November,
sum(sp.spend_points * if(sp.price_id = 14 and sp.created_at < @nextYr, 1, 0)) December
from
spend_points sp) SpendPerMonth
Ty非常DRapp :)我會試試看! – Nathre 2012-07-15 05:36:44