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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:30:03+00:00 2026-06-13T22:30:03+00:00

I have the following query I want to fire: SELECT DISTINCT TOP(5) fp.PostId FROM

  • 0

I have the following query I want to fire:

SELECT DISTINCT TOP(5) fp.PostId FROM dbForumPosts fp
LEFT JOIN dbForumEntry fe ON fp.PostId = fe.PostId
Order by fe.Datemade DESC

However, when I fire it, I get the error:

Msg 145, Level 15, State 1, Line 1
ORDER BY items must appear in the select list if SELECT DISTINCT is specified.

I tried to change the query, so it used GROUP BY instead, but then I have the following problem:

Msg 8127, Level 16, State 1, Line 4
Column "dbForumEntry.Datemade" is invalid in the ORDER BY clause because it is not contained in either an aggregate function or the GROUP BY clause.

WHAT DO I WANT:

Think of this as a forum. There are posts (dbForumPosts) and entries (dbForumEntry). There are 0-many entries pr post.

What I want is to the get posts with the most recent activity (posts with the latest updated entries in).

  • 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-13T22:30:04+00:00Added an answer on June 13, 2026 at 10:30 pm

    You could find the most recent Datemade per PostId with row_number. Then you can search for the most recent 5 posts:

    select  top 5 PostId
    from    (
            select  PostId
            ,       Datemade
            ,       row_number() over (partition by PostId
                        order by Datemade) as rn
            from    dbForumEntry
            ) SubQueryAlias
    where   rn = 1 -- Most recent row per PostId
    order by
            Datemade desc
    

    Alternatively, you can achieve the same with a group by subquery:

    select  top 5 PostId
    from    (
            select  PostId
            ,       max(Datemade) as LastDate
            from    dbForumEntry
            group by
                    PostId
            ) SubQueryAlias
    order by
            LastDate desc
    

    If dbForumEntry has an ID column (say ForumEntryId), a query like this might perform better. The database can run this without compiling the row_number or max(Datemade) for the entire table.

    select  top 5 PostId
    from    dbForumPosts fp
    where   not exists -- No later entry for the same post exists
            (
            select  *
            from    dbForumPosts fp2
            where   fp2.PostId = fp.PostId
                    and fp2.ForumEntryId > fp.ForumEntryId
            )
    order by
            Datemade desc
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following query: SELECT `people`.`surename`, calcWeight(`people`.`surname`,'kiera',6) as `weight` FROM `people` LEFT JOIN
I have the following query: 'SELECT * FROM posts LEFT JOIN taxi ON taxi.taxiID
I have the following query: select distinct(ab.id) from WidgetClicks ab join ManufacturerWidgets aa on
I have the following query: SELECT carBrand, carYear, carModel FROM Cars; What I want
I have the following query: SELECT id,name FROM categ WHERE email_activated='1' ORDER BY id
I have the following query: SELECT title, karma, DATE(date_uploaded) as d FROM image ORDER
I have the following query select * from Properties join PPHL on PPHL.postcode =
I have following query: $query=SELECT language_value, votes, user_id FROM labels WHERE approved=1 AND label_value=.
I have following query , select * from process where name like 'abc'; now
I have following query in PostgreSQL: SELECT COUNT(a.log_id) AS overall_count FROM Log as a,

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.