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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T22:53:51+00:00 2026-06-01T22:53:51+00:00

I have the following table with messages: +———+———+————+———-+ | msg_id | user_id | m_date

  • 0

I have the following table with messages:

+---------+---------+------------+----------+
| msg_id  | user_id | m_date     |  m_time  |
+-------------------+------------+----------+
|   1     | 1       | 2011-01-22 | 06:23:11 |
|   2     | 1       | 2011-01-23 | 16:17:03 |
|   3     | 1       | 2011-01-23 | 17:05:45 |
|   4     | 2       | 2011-01-22 | 23:58:13 |
|   5     | 2       | 2011-01-23 | 23:59:32 |
|   6     | 2       | 2011-01-24 | 21:02:41 |
|   7     | 3       | 2011-01-22 | 13:45:00 |
|   8     | 3       | 2011-01-23 | 13:22:34 |
|   9     | 3       | 2011-01-23 | 18:22:34 |
|  10     | 3       | 2011-01-24 | 02:22:22 |
|  11     | 3       | 2011-01-24 | 13:12:00 |
+---------+---------+------------+----------+

What I want is for each day, to see how many messages each user has sent BEFORE and AFTER 16:00:

SELECT 
    user_id, 
    m_date, 
    SUM(m_time <= '16:00') AS before16, 
    SUM(m_time > '16:00') AS after16 
FROM messages 
GROUP BY user_id, m_date
ORDER BY user_id, m_date ASC

This produces:

user_id m_date      before16  after16
-------------------------------------
1       2011-01-22  1         0
1       2011-01-23  0         2
2       2011-01-22  0         1
2       2011-01-23  0         1
2       2011-01-24  0         1
3       2011-01-22  1         0
3       2011-01-23  1         1
3       2011-01-24  2         0

Because user 1 has written no messages on 2011-01-24, this date is not in the resultset. However, this is undesirable. I have a second table in my database, called “date_range”:

+---------+------------+
| date_id | d_date     |
+---------+------------+
| 1       | 2011-01-21 |
| 1       | 2011-01-22 |
| 1       | 2011-01-23 |
| 1       | 2011-01-24 |
+---------+------------+

I want to check the “messages” against this table. For each user, all these dates have to be in the resultset. As you can see, none of the users have written messages on 2011-01-21, and as said, user 1 has no messages on 2011-01-24. The desired output of the query would be:

user_id d_date      before16  after16
-------------------------------------
1       2011-01-21  0         0
1       2011-01-22  1         0
1       2011-01-23  0         2
1       2011-01-24  0         0
2       2011-01-21  0         0
2       2011-01-22  0         1
2       2011-01-23  0         1
2       2011-01-24  0         1
3       2011-01-21  0         0
3       2011-01-22  1         0
3       2011-01-23  1         1
3       2011-01-24  2         0

How can I link the two tables so that the query result also holds rows with zero values for before16 and after16?

Edit: yes, I have a “users” table:

+---------+------------+
| user_id | user_date  |
+---------+------------+
| 1       | foo        |
| 2       | bar        |
| 3       | foobar     |
+---------+------------+
  • 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-01T22:53:54+00:00Added an answer on June 1, 2026 at 10:53 pm

    Test bed:

    create table messages (msg_id integer, user_id integer, _date date, _time time);
    create table date_range (date_id integer, _date date);
    insert into messages values
           (1,1,'2011-01-22','06:23:11'),
           (2,1,'2011-01-23','16:17:03'),
           (3,1,'2011-01-23','17:05:05');
    insert into date_range values
           (1, '2011-01-21'),
           (1, '2011-01-22'),
           (1, '2011-01-23'),
           (1, '2011-01-24');
    

    Query:

    SELECT p._date, p.user_id,
           coalesce(m.before16, 0) b16, coalesce(m.after16, 0) a16
      FROM
          (SELECT DISTINCT user_id, dr._date FROM messages m, date_range dr) p
      LEFT JOIN
          (SELECT user_id, _date,
                  SUM(_time <= '16:00') AS before16,
                  SUM(_time > '16:00') AS after16 
             FROM messages 
            GROUP BY user_id, _date
            ORDER BY user_id, _date ASC) m
        ON p.user_id = m.user_id AND p._date = m._date;
    

    EDIT:

    1. Your initial query is left as is, I hope it doesn’t requires any explanations;

    2. SELECT DISTINCT user_id, dr._date FROM messages m, date_range dr will return a cartesian or CROSS JOIN of two tables, which will give me all required date range for each user in subject. As I’m interested in each pair only once, I use DISTINCT clause. Try this query with and without it;

    3. Then I use LEFT JOIN on two sub-selects.

      This join means: first, INNER join is performed, i.e. all rows with matching fields in the ON condition are returned. Then, for each row in the left-side relation of the join that has no matches on the right side, return NULLs (thus the name, LEFT JOIN, i.e. left relation is always there and right is expected to have NULLs). This join will do what you expect — return user_id + date combinations even if there were no messages in the given date for a given user. Note that I use user_id + date sub-select first (on the left) and messages query second (on the right);

    4. coalesce() is used to replace NULL with zero.

    I hope this clarifies how this query works.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following table with messages: +---------+---------+------------+----------+ | msg_id | user_id | _date
Imagine I have the following data in a table called messages: message_id | language_id
I have a messages table with the following columns: id (primary Key) username timestamp
Consider the following table with messages. Some of them have one or more labels
I have the following table in a MYSQL database: Messages MessageId (PK) int(10) -
I have a simple mysql table called messages with the following fields: ID(int) MESSAGE(varchar)
I have the following table: Messages ID(PK) int auto_inc Message varchar(100) I would like
I have a table called inbox_messages with following rows: user_id message_id ======= ========== 4
For a messages table with the following structure id, sender_id, receiver_id, text I want
Consider the following table: +---------+---------+------------+----------+------------+ | msg_id | user_id | _date | _time |

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.