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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:59:34+00:00 2026-06-10T15:59:34+00:00

I have a database table that stores user comments: comments(id, user_id, created_at) From that

  • 0

I have a database table that stores user comments:

comments(id, user_id, created_at)

From that table, I want to get the number of users that have commented for the first time in the past 7 days.


Here’s what I have so far:

SELECT COUNT(DISTINCT `user_id`) 
FROM `comments` 
WHERE `created_at` BETWEEN DATE_SUB(NOW(), INTERVAL 7 DAY) AND NOW()

This would give the number of users that have commented, but it would not take into consideration whether these comments are first for their users.

  • 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-10T15:59:36+00:00Added an answer on June 10, 2026 at 3:59 pm
    SELECT COUNT(DISTINCT user_id)
    FROM comments AS c1
    WHERE c1.created_at BETWEEN DATE_SUB(NOW(), INTERVAL 7 DAY) AND NOW()
    AND NOT EXISTS (SELECT 1 FROM comments AS c2 
                    WHERE c2.user_id = c1.user_id AND c2.created_at < c1.created_at)
    

    The NOT EXISTS clause checks whether the same user_id has a record with an earlier created_at time. If so, it means this is not the first time they are commenting, and thus we should discount this record.

    I have kept DISTINCT user_id because it is possible two comments are created at the same time. You could also try the following instead, which only gets the very first record for each user, so you can do away with the DISTINCT, but I don’t know which would be more optimal:

    SELECT COUNT(*)
    FROM comments AS c1
    WHERE c1.created_at BETWEEN DATE_SUB(NOW(), INTERVAL 7 DAY) AND NOW()
    AND NOT EXISTS (SELECT 1 FROM comments AS c2 
                    WHERE c2.user_id = c1.user_id 
                    AND (c2.created_at < c1.created_at 
                        OR (c2.created_at = c1.created_at AND c2.id < c1.id)))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok I have a table in my SQL Server database that stores comments. My
I have a database that has a table email_eml that stores 3 attributes name_eml,
I have a database table that stores document information (DocID, Name, Type, Owner, etc).
I am developing a CMS that stores user content in a database table like
I have a SQL Server 2005 database for a web site that stores user
I have a database table (called Info) that stores [Data], [fkID], [UserID], [Timestamp]. [Data]
I have a SQL Server 2005 database that stores data for multiple users. Each
I have an ecommerce MySQL database that stores order information in the table 'orders'
I have a mysql database that stores users that are online. Is there an
I have a table that stores user information in a table in a MySQL

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.