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

The Archive Base Latest Questions

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

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

  • 0

I have the following table with messages:

+---------+---------+------------+----------+
| msg_id  | user_id | _date      | _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     | 3       | 2011-01-22 | 13:45:00 |
|   7     | 3       | 2011-01-23 | 13:22:34 |
|   8     | 3       | 2011-01-23 | 18:22:34 |
+---------+---------+------------+----------+

What I want is for each day, to see how many messages each user has sent BEFORE and AFTER 16:00. I now do this in two steps:

SELECT user_id, _date, COUNT(msg_id) AS cnt 
FROM messages WHERE _time <= '16:00'
GROUP BY user_id, _date ORDER BY user_id, _date ASC

user_id _date       cnt
-----------------------------
1       2011-01-22  1
1       2011-01-23  0
2       2011-01-22  0
2       2011-01-23  0
3       2011-01-22  1
3       2011-01-23  1

SELECT user_id, _date, COUNT(msg_id) AS cnt 
FROM messages WHERE _time > '16:00'
GROUP BY user_id, _date ORDER BY user_id, _date ASC

user_id _date       cnt
-----------------------------
1       2011-01-22  0
1       2011-01-23  2
2       2011-01-22  1
2       2011-01-23  1
3       2011-01-22  0
3       2011-01-23  1

(In reality, btw, the rows with “0” value aren’t given in the resultset. I just added them for clarification)

I would like to combine these two outputs into one:

user_id _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
3       2011-01-22  1         0
3       2011-01-23  1         1

However, I have no idea on how to write this query. If you do, your help would be appreciated 🙂

  • 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:38:55+00:00Added an answer on June 1, 2026 at 10:38 pm

    Try this:

    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
    
    • 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 | m_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
I have following table structure: Table: Plant PlantID: Primary Key PlantName: String Table: Party

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.