Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8222485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T14:17:57+00:00 2026-06-07T14:17:57+00:00

Do you have an idea about how to resolve this? This is an example

  • 0

Do you have an idea about how to resolve this?

This is an example of the DB I have: 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

Second Table: 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

The first table belongs to a group and this group can only spend points on items with ‘price_id’ = 14. In the spend_points table we have all items spend and there are some 14’s.
The query I’m trying to build should show data from all users who can spend on price_id = 14. The table should have the total of points they have, total of spend points and all points they have had so far. Sounds easy when you only want that data but the problem to me is when I try to make a history of this. Every new month it should show an update of the previos month and also the previous month and so on.

Status = 0 means Inactive and

So the final table should look like this.

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

Do you guys have any idea how to do it? :'(

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T14:17:58+00:00Added an answer on June 7, 2026 at 2:17 pm

    It’s long, but it will do it… based on a single year basis… if your data spans years, just add an applicable “year” where clause to each select/union. The premise of the query is to pre-declare up front each month so we know where the “cut-off” date is for a given month aggregation (ie: Jan is sum of all less than Feb, Feb is sum all up to Mar, etc).
    Each one does require it’s own pivot roll-up per month which is what makes this long, however, very simple to follow to get your results. Not how I would plan to have such a query, especially on a large set — where I would prefer to have a single table with “roll-up” values per month/year of the expected aggregations, and where applicable, have a trigger just update the counts, points, whatever as needed.

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Anyone have idea about how to bind Boolean in sqlite with objective - c.
I missed my workspace in which I was working. Any one have idea about
Anyone have any idea about CASlotProxy Class in Cocoa (iOS SDK)? I found it
Please let me know if you have any idea about it. Thanks EDIT What
I want to use a vertical UISlider . I have no idea about how,
I have a good idea about Agile development process but I have no ieda
Brief Idea about the flow : I have say minimum 1 and maximum 18
I have a very little idea about what database file system is. Can somebody
Anyone have any idea how I would go about converting a timestamp in milliseconds
i have a couple questions about an idea i have to manage a form

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.