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 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
  • 1 View
  • 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

Related Questions

suppose you are given the following problem. You have two index sets that have
I have a problem with nested templates and their template specialization. Given the following
I have the following SQL problem. Scenario: I have two tables: Change and ChangeTicket.
Given two tables: (incoming greatly reduced/simplified example that exhibits the key problem) app_data represents
My problem is the following: I have two tables; persons and teams, I want
Given the following problem , I'd appreciate for any corrections since I have no
I have the following problem, given the following service contract, data contract, and service
I have the following problem: I am given a tree with N apples, for
We have three tables that we would like to query to find out the
Okay... So, I came across the following problem: I have one table that works

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.