長いですが、1年ベースで実行されます...データが数年にわたる場合は、各select/unionに該当する「year」where句を追加するだけです。クエリの前提は、毎月事前に事前に宣言することです。これにより、特定の月の集計の「締め切り」日がどこにあるかがわかります(つまり、1月は2月未満の合計、2月は3月までの合計です。 、など)。それぞれが月ごとに独自のピボットロールアップを必要とします。これがこれを長くする理由ですが、結果を得るために従うのは非常に簡単です。特に大規模なセットで、このようなクエリをどのように計画するかではありません。予想される集計の月/年あたりの「ロールアップ」値を含む単一のテーブルが必要であり、該当する場合はトリガーがあります。必要に応じて、カウントやポイントを更新するだけです。
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