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 6747681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:28:22+00:00 2026-05-26T12:28:22+00:00

On a previous question a table could be created and populated with the days

  • 0

On a previous question a table could be created and populated with the days of the month, but I’d like that table to be populated slightly different: each day of the month should have three different hour intervals.

According to that question, this code by Tom Mac:

create table all_date 
(id int unsigned not null primary key auto_increment, 
a_date date not null,
last_modified timestamp not null default current_timestamp on update current_timestamp,
unique key `all_date_uidx1` (a_date));

And then,

DELIMITER //


CREATE PROCEDURE populate_all_dates(IN from_date DATE, IN days_into_future INT)

BEGIN

 DECLARE v_date DATE;
 DECLARE ix int;


 SET ix := 0;
 SET v_date := from_date;


 WHILE v_date <= (from_date + interval days_into_future day) DO

  insert into all_date (a_date) values (v_date) 
  on duplicate key update last_modified = now();

  set ix := ix +1;

  set v_date := from_date + interval ix day;

 END WHILE;

END//

DELIMITER ;

And then you can run:

call populate_all_dates('2011-10-01',30);

To populate all dates for October (or whatever month, change the values of the function)

With that I could run the following query

select day(a.a_date) as 'October',
IFNULL(t.a1,0) as 'Auth1',
IFNULL(t.a2,0) as 'Auth2',
IFNULL(t.a50,0) as 'Auth50'
from all_date a
LEFT OUTER JOIN
(
SELECT date(wp.post_date) as post_date,
sum(case when wp.post_author = '1' then 1 else 0 end) as a1,
sum(case when wp.post_author = '2' then 1 else 0 end) as a2,
sum(case when wp.post_author = '50' then 1 else 0 end) as a50,
count(*) as 'All Auths'
FROM wp_posts wp
WHERE  wp.post_type = 'post'
AND wp.post_date  between '2011-10-01' and '2011-10-31 23:59:59'
GROUP BY date(wp.post_date)
) t
ON a.a_date = t.post_date
where a.a_date between '2011-10-01' and '2011-10-31'
group by day(a.a_date);

And I would get a table with the number of posts in my WordPress blog by author and day, similar to this:

+---------+---------+-------+------+---------+
| October | Auth1   | Auth2 | Auth3|  Auth4  |
+---------+---------+-------+------+---------+
|       1 |       0 |     0 |    0 |       0 |
|       2 |       0 |     0 |    1 |       0 |
|       3 |       4 |     4 |    6 |       2 |
|       4 |       4 |     3 |    5 |       2 |
|       5 |       7 |     0 |    5 |       2 |
|       6 |       4 |     4 |    0 |       2 |
|       7 |       0 |     2 |    1 |       2 |
|       8 |       0 |     0 |    7 |       0 |
.....
etc

But what I’dlike to have is each day divided in three different rows, each one corresponding to the following time ranges:

00:00-14:30
14:31-18:15
18:16-23:59

So the table should show something like (for example, I don’t know how each of the time ranges could be shown, so a good way should be day 1, time range 1 (1-1), etc).

+---------+---------+-------+------+---------+
| October | Auth1   | Auth2 | Auth3|  Auth4  |
+---------+---------+-------+------+---------+
|    1-1  |       0 |     0 |    0 |       0 |
|    1-2  |       0 |     0 |    0 |       0 |
|    1-3  |       0 |     0 |    0 |       0 |
|    2-1  |       0 |     0 |    1 |       0 |
|    2-2  |       0 |     0 |    0 |       0 |
|    2-3  |       0 |     0 |    0 |       0 |
|    3-1  |       1 |     2 |    3 |       0 |
|    3-2  |       1 |     2 |    2 |       2 |
|    3-3  |       2 |     0 |    1 |       0 |
etc...

As you can see, the three rows sum is equivalent to each of the previous unique row for the day.

Is that possible?

  • 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-26T12:28:22+00:00Added an answer on May 26, 2026 at 12:28 pm

    use (UPDATE #2)

    SELECT 
    a.a_datetm as 'October',
    IFNULL(p.a1,0) as 'Auth1',
    IFNULL(p.a2,0) as 'Auth2',
    IFNULL(p.a50,0) as 'Auth50'
    FROM
    (
    SELECT CONCAT (day(X.a_date), '-1') AS a_datetm
    FROM all_date X
    WHERE X.a_date between '2011-10-01' and '2011-10-31'
    UNION ALL
    SELECT CONCAT (day(Y.a_date), '-2') AS a_datetm
    FROM all_date Y
    WHERE Y.a_date between '2011-10-01' and '2011-10-31'
    UNION ALL
    SELECT CONCAT (day(Z.a_date), '-3') AS a_datetm
    FROM all_date Z
    WHERE Z.a_date between '2011-10-01' and '2011-10-31'
    ) a
    LEFT OUTER JOIN
    (
    SELECT 
    CONCAT (day(wp.post_date), (CASE WHEN (TIME(wp.post_date) < '14:31:00') THEN '-1' WHEN (TIME(wp.post_date) BETWEEN '14:31:00' AND '18:15:59') THEN '-2' ELSE '-3' END )) AS a_datetm,
    sum(case when wp.post_author = '1' then 1 else 0 end) as a1,
    sum(case when wp.post_author = '2' then 1 else 0 end) as a2,
    sum(case when wp.post_author = '50' then 1 else 0 end) as a50,
    count(*) as 'All Auths'
    FROM wp_posts wp
    WHERE  wp.post_type = 'post'
    AND wp.post_date  between '2011-10-01' and '2011-10-31 23:59:59'
    GROUP BY CONCAT (day(wp.post_date), (CASE WHEN (TIME(wp.post_date) < '14:31:00') THEN '-1' WHEN (TIME(wp.post_date) BETWEEN '14:31:00' AND '18:15:59') THEN '-2' ELSE '-3' END ))
    ) p
    ON a.a_datetm = p.a_datetm
    ORDER BY a.a_datetm ASC;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In a previous question @Morawski was saying that a table with 1,000 columns and
In previous question of mine, someone had meantioned that using Semaphores were expensive in
This question is related to a previous question of mine That's my current code
This question follows on from a previous question, that has raised a further issue.
In reference to my previous question: One table, two column MYSQL query challenge The
We're using gwt-presenter, but not really a question specific to that... I've got a
I'm implementing a table per subclass design I discussed in a previous question .
Okay, so if you would like a back story, look at my previous question
This code is taken from a previous question, but my question directly relates to
I asked a previous question about mapping an enumerated value on a table using

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.