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

  • Home
  • SEARCH
  • 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 323703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:03:20+00:00 2026-05-12T09:03:20+00:00

I have a table with records that look like this: CREATE TABLE sample (

  • 0

I have a table with records that look like this:

CREATE TABLE sample (
  ix int unsigned auto_increment primary key,
  start_active datetime,
  last_active datetime
);

I need to know how many records were active on each of the last 30 days. The days should also be sorted incrementing so they are returned oldest to newest.

I’m using MySQL and the query will be run from PHP but I don’t really need the PHP code, just the query.

Here’s my start:

SELECT COUNT(1) cnt, DATE(?each of last 30 days?) adate
FROM sample
WHERE adate BETWEEN start_active AND last_active
GROUP BY adate;
  • 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-05-12T09:03:20+00:00Added an answer on May 12, 2026 at 9:03 am

    Do an outer join.

    No table? Make a table. I always keep a dummy table around just for this.

    create table artificial_range( 
      id int not null primary key auto_increment, 
      name varchar( 20 ) null ) ;
    
    -- or whatever your database requires for an auto increment column
    
    insert into artificial_range( name ) values ( null )
    -- create one row.
    
    insert into artificial_range( name ) select name from artificial_range;
    -- you now have two rows
    
    insert into artificial_range( name ) select name from artificial_range;
    -- you now have four rows
    
    insert into artificial_range( name ) select name from artificial_range;
    -- you now have eight rows
    
    --etc.
    
    insert into artificial_range( name ) select name from artificial_range;
    -- you now have 1024 rows, with ids 1-1024
    

    Now make it convenient to use, and limit it to 30 days, with a view:

    Edit: JR Lawhorne notes:

    You need to change “date_add” to “date_sub” to get the previous 30 days in the created view.

    Thanks JR!

    create view each_of_the_last_30_days as
    select date_sub( now(), interval (id - 1) day ) as adate
    from artificial_range where id < 32;
    

    Now use this in your query (I haven’t actually tested your query, I’m just assuming it works correctly):

    Edit: I should be joining the other way:

    SELECT COUNT(*) cnt, b.adate
    FROM  each_of_the_last_30_days b
    left outer join sample a 
     on ( b.adate BETWEEN a.start_active AND a.last_active)
    GROUP BY b.adate;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 288k
  • Answers 288k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer SELECT AVG(period) FROM ( SELECT TIME_TO_SEC(TIMEDIFF(@date_posted, date_posted)) AS period, @date_posted… May 13, 2026 at 5:19 pm
  • Editorial Team
    Editorial Team added an answer property[.//programs/program=1] Selects the property elements that contain descendant programs elements… May 13, 2026 at 5:19 pm
  • Editorial Team
    Editorial Team added an answer http://api.jquery.com/prev/ This should do it: $('a').click(function() { $(this).parent().prev().addClass('previous'); }); May 13, 2026 at 5:19 pm

Related Questions

I want to do a data update using Oracle. I have a situation where
I have a table that records a sequence of actions with a field that
I have a MySQL table holding lots of records that i want to give
I have a 3 tables that look like this: (source: InsomniacGeek.com ) On the

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.