它很长,但它会做到……基于一年……如果您的数据跨越数年,只需在每个选择/联合中添加一个适用的“年份”where 子句。查询的前提是每个月预先声明,这样我们就知道给定月份聚合的“截止”日期在哪里(即:一月是所有小于二月的总和,二月是所有三月的总和, ETC)。每个人都需要每个月都有自己的枢轴汇总,这就是为什么这么长的原因,但是,很容易遵循以获得您的结果。不是我打算如何进行这样的查询,尤其是在一个大集合上——我希望有一个表,其中包含预期聚合的每月/每年的“汇总”值,并且在适用的情况下,有一个触发器只需根据需要更新计数,点数。
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