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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:29:21+00:00 2026-06-15T17:29:21+00:00

I have one table with user posts. I need to show from 1 to

  • 0

I have one table with user posts. I need to show from 1 to maximum n post from each user per each day.

Example:

post_id|user_id|post_datetime|post_text
1      |100    |2012-12-01 01:00:00|lorem ipsum 1
2      |100    |2012-12-01 02:00:00|lorem ipsum 2
3      |101    |2012-12-01 03:00:00|lorem ipsum 3
4      |100    |2012-12-01 04:00:00|lorem ipsum 4
5      |102    |2012-12-01 05:00:00|lorem ipsum 5
6      |100    |2012-12-02 03:00:00|lorem ipsum 6
7      |102    |2012-12-02 04:00:00|lorem ipsum 7
8      |101    |2012-12-02 05:00:00|lorem ipsum 8
9      |101    |2012-12-02 06:00:00|lorem ipsum 9
10     |101    |2012-12-02 07:00:00|lorem ipsum 10

I need a query that returns, for instance, a maximumf of 2 posts per day for each user:

post_id|user_id|post_datetime|post_text
2      |100    |2012-12-01 02:00:00|lorem ipsum 2
4      |100    |2012-12-01 04:00:00|lorem ipsum 4
3      |101    |2012-12-01 03:00:00|lorem ipsum 3
5      |102    |2012-12-01 05:00:00|lorem ipsum 5
6      |100    |2012-12-02 03:00:00|lorem ipsum 6
7      |102    |2012-12-02 04:00:00|lorem ipsum 7
9      |101    |2012-12-02 06:00:00|lorem ipsum 9
10     |101    |2012-12-02 07:00:00|lorem ipsum 10

I tried with GROUP and HAVING but I only get the top n records, not the top n per day for each user:

SELECT a.* FROM posts AS a
   JOIN posts AS a2 
   ON a.user_id = a2.user_id AND a.post_datetime <= a2.post_datetime
GROUP BY a.post_id
HAVING COUNT(*) <= 3
ORDER BY a.post_id, a.post_datetime DESC
  • 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-15T17:29:21+00:00Added an answer on June 15, 2026 at 5:29 pm

    Try this awful SQL code 🙂

    select post_id, user_id, post_datetime, post_text from (
      select posts.*,
        if (user_id = @prev_user and date(post_datetime) = date(@prev_day),
          @row := @row + 1, @row := 1) idx,
        @prev_user := user_id,
        @prev_day := post_datetime
      from posts, (select @row := 1, @prev_user := null, @prev_day := null) init
      order by date(post_datetime), user_id, post_datetime desc
    ) s
    where s.idx <= 2
    

    Result:

    +---------+---------+---------------------------------+----------------+
    | POST_ID | USER_ID |          POST_DATETIME          |   POST_TEXT    |
    +---------+---------+---------------------------------+----------------+
    |       4 |     100 | December, 01 2012 04:00:00+0000 | lorem ipsum 4  |
    |       2 |     100 | December, 01 2012 02:00:00+0000 | lorem ipsum 2  |
    |       3 |     101 | December, 01 2012 03:00:00+0000 | lorem ipsum 3  |
    |       5 |     102 | December, 01 2012 05:00:00+0000 | lorem ipsum 5  |
    |       6 |     100 | December, 02 2012 03:00:00+0000 | lorem ipsum 6  |
    |      10 |     101 | December, 02 2012 07:00:00+0000 | lorem ipsum 10 |
    |       9 |     101 | December, 02 2012 06:00:00+0000 | lorem ipsum 9  |
    |       7 |     102 | December, 02 2012 04:00:00+0000 | lorem ipsum 7  |
    +---------+---------+---------------------------------+----------------+
    

    Fiddle here.

    I thought the ordering would be more suitable if it was descending by date, as you’re actually getting the top 2 closest to the current date.

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

Sidebar

Related Questions

I have one table containing user sessions and another to indicate violations in the
I have two tables. One user table and one table in which results are
I currently have a user's table which contains a one-to-one relationship for Youtube OAuth
I have a user and a message table. User to Message are one-to-many relationships
I have two table views, when user clicks one cell of the first, second
I have two tables. One table contains comments on posts, another contains commenter information
I have the table users with information about every user. Then the table posts
Ok, I have a database with with a table for storing classified posts, each
I have the following database. One user can write one or more posts. Votes
In SQL Server 2008 I have two tables. One table for users: id_user -

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.