0

你知道如何解决这个问题吗?

这是我拥有的数据库的一个示例:profile_table

user_id    status   points   created_at 
01          1        20       2011-01-01 01:03:35
02          1        50       2011-01-02 01:03:35
03          1        800      2011-01-04 01:03:35
04          1        152      2011-02-15 01:03:35
05          1        388      2011-02-20 01:03:35
06          0        40       2011-02-25 01:03:35
07          1        246      2011-02-26 01:03:35
08          1        335      2011-02-27 01:03:35
09          1        521      2011-03-08 01:03:35
10          1        5354     2011-03-14 01:03:35
11          0        22       2011-03-18 01:03:35
12          1        234      2011-04-03 01:03:35
13          1        222      2011-04-05 01:03:35
14          1        396      2011-04-24 01:03:35

第二个表:spend_points

price_id    spend_points   created_at
1           20             2011-01-01 01:03:35
2           10             2011-01-02 01:03:35
3           30             2011-01-05 01:03:35
8           40             2011-01-06 01:03:35
14          10             2011-02-03 01:03:35
7           50             2011-02-06 01:03:35
14          10             2011-02-07 01:03:35
2           10             2011-03-03 01:03:35
14          60             2011-03-12 01:03:35
14          10             2011-04-07 01:03:35
2           70             2011-04-12 01:03:35
14          80             2011-04-15 01:03:35
14          20             2011-04-21 01:03:35

第一个表属于一个组,该组只能在 'price_id' = 14 的项目上花费积分。在spend_points 表中,我们有所有项目花费,并且有一些 14。我正在尝试构建的查询应该显示可以在 price_id = 14 上花费的所有用户的数据。该表应该包含他们拥有的总点数、总花费点数和他们迄今为止拥有的所有点数。当您只想要这些数据时听起来很容易,但对我来说问题是当我试图记录这一点时。每个新月它都应该显示上个月和上个月的更新,依此类推。

状态 = 0 表示非活动和

所以决赛桌应该是这样的。

Detail                    January   February  March  April 
Total Active Users        3         7         9      12
Total Inactive Users      0         1         2      2
Total Point               807       2031      7928   8780      
Spend Points              0         20        80     190
Points So Far             807       2051      8008   8970

你们知道怎么做吗?:'(

4

2 回答 2

1

它很长,但它会做到……基于一年……如果您的数据跨越数年,只需在每个选择/联合中添加一个适用的“年份”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
于 2012-07-14T03:05:16.337 回答
-1

In the spreadsheet world, people call what you want to make a "pivot table". You're not going to get that out of MySQL without hand-building some massive ugly queries - it just doesn't fit in with what an RDBMS is meant to do. You just have to get the per-month values & build out your report at the application level. Similarly, those running totals are best just handled manually.

于 2012-07-14T00:00:26.683 回答