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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:59:44+00:00 2026-05-13T21:59:44+00:00

Problem Given the following two tables, I’d like to select all Ids for Posts

  • 0

Problem


Given the following two tables, I’d like to select all Ids for Posts that have their most recent (i.e. last) comment made in the given time span (e.g. Feb 2010).

The result of the query should only return Post ID 1, since the most recent comment for Post ID 2 is outside the range of the time span filter.

Question


I’ve created the SELECT statement below that seems correct and handles all the test cases thrown at it.

However, in a quest to continue to improve my SQL skills, I’m asking the community if there is a "better" method to use for this scenario, any suggestions on improving the existing statement, and/or edge cases that are not covered.

Note that this is an loose translation of the actual tables, changed with the intent of making the question easier to understand. For what it’s worth, I’m using SQL Server 2005.

Tables


Post

Id    Text     Visible
1     Post 1   1
2     Post 2   1
3     Post 3   0
.     ...
n     Post n   1

Comment

Id    Post_Id    Text                  CommentNumber    Timestamp
1     1          Comment 1, Post 1     1                2/3/2010
2     1          Comment 2, Post 1     2                2/4/2010
3     2          Comment 1, Post 2     1                3/1/2010
.     .          .
n     m          Comment n, Post m     x                xx/xx/xxxx

SQL Command


SELECT [Id],[Text]
FROM [Post]  
WHERE [Id] IN (  
    SELECT comment1.[Post_Id]  
    FROM (  
        SELECT max([CommentNumber]) as maxComment,  
            [Post_id]  
        FROM [Comment]  
        GROUP BY [Post_id]  
    ) as comment2  
    INNER JOIN [Comment] as comment1 on comment1.[Post_id] = comment2.[Post_id]  
    WHERE comment1.[Timestamp] BETWEEN '2/1/2010 00:00:00.000' AND '2/28/2010 23:59:59.999'  
    AND comment1.[CommentNumber] = comment2.maxComment  
)
AND [Post].[Visible] = 1

Bonus Question


Is it possible to create this query with NHiberate (either using the Criteria API or HQL)?

  • 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-13T21:59:44+00:00Added an answer on May 13, 2026 at 9:59 pm
    SELECT
        Post_Id
    FROM
        Comment
    GROUP BY
        Post_Id
    HAVING
        MAX(Timestamp) >= '2/1/2010'
    

    Thinkg of HAVING as a WHERE that takes place after GROUP BY, operating on the grouped resultset.

    Don’t know about NHibernate though.

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

Sidebar

Ask A Question

Stats

  • Questions 493k
  • Answers 493k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If bar is bound to an anonymous function, then it… May 16, 2026 at 10:47 am
  • Editorial Team
    Editorial Team added an answer You have two ManyToOne relationships using the same foreign key… May 16, 2026 at 10:47 am
  • Editorial Team
    Editorial Team added an answer <tr> <<-- 1.) <<-- 6.) <td> <a href="#" class="xx"></a> <<--… May 16, 2026 at 10:47 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have the following SQL problem. Scenario: I have two tables: Change and ChangeTicket.
Okay... So, I came across the following problem: I have one table that works
I have two tables, timetable and lesson_booking these are linked via timetable_id . timetable
using mysql and php, I have two tables, I'm french so the table names
Here's my situation. I have two tables: pledges and pledge_transactions. When a user makes
here is my problem I have the following array (for example) string[] arr =
I have some strange problem. I have a solution with the following structure http://i33.tinypic.com/10fbzbq.jpg
I have a problem with a subquery involving IPV4 addresses stored in MySQL (MySQL
Given the following snippet: #include <stdio.h> typedef signed long long int64; typedef signed int
I have sed running with the following argument fine if I copy and paste

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.