您对如何解决此问题有所了解吗?
这是我拥有的DB的一个例子: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上的用户的数据。该表应该包含他们拥有的总点数,支出总数以及他们迄今为止所拥有的所有点数。当你只想要那些数据时听起来很简单,但问题在于我试图创建这个数据的历史。每个新月都应显示previos月和上个月的更新,依此类推。
状态= 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
你们有任何想法怎么做吗? :'(
答案 0 :(得分:1)
这很长,但它会做到......基于一年......如果您的数据跨越多年,只需在每个选择/联合中添加适用的“年份”where子句。查询的前提是每个月预先申报,因此我们知道给定月份聚合的“截止”日期(即:Jan是所有小于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
答案 1 :(得分:-1)
在电子表格世界中,人们会将您想要的内容称为“数据透视表”。如果没有手工构建一些大规模的丑陋查询,你就不会从MySQL那里得到它 - 它只是不适合RDBMS的意图。你只需得到每月的价值和&amp;在应用程序级别构建您的报告。同样,那些运行总计最好只需手动处理。