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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:56:06+00:00 2026-06-15T00:56:06+00:00

This question has been asked multiple times on SO but all the answers refer

  • 0

This question has been asked multiple times on SO but all the answers refer to SQL 2005 or later (e.g. OUTER APPLY) and we are still using SQL 2000 (for corporate reasons too complex to go into here!)

I have a table of Things and a table of Widgets with a 0 to Many relationship:

CREATE TABLE Things ( ThingId INT, ThingName VARCHAR(50) )

CREATE TABLE Widgets ( WidgetId INT, ThingId INT, WidgetName VARCHAR(50) )

INSERT INTO Things VALUES ( 1, 'Thing 1' )

INSERT INTO Things VALUES ( 2, 'Thing 2' )

INSERT INTO Things VALUES ( 3, 'Thing 3' )

INSERT INTO Widgets VALUES ( 1, 2, 'Thing 2 Widget 1' )

INSERT INTO Widgets VALUES ( 2, 2, 'Thing 2 Widget 2' )

INSERT INTO Widgets VALUES ( 3, 3, 'Thing 3 Widget 1' )

A standard LEFT OUTER JOIN returns the expected 4 rows

SELECT * FROM Things t LEFT OUTER JOIN Widgets w ON t.ThingId = w.ThingId

 ThingId | ThingName | WidgetId | ThingId | WidgetName       
---------+-----------+----------+---------+------------------
 1       | Thing 1   | NULL     | NULL    | NULL             
 2       | Thing 2   | 1        | 2       | Thing 2 Widget 1 
 2       | Thing 2   | 2        | 2       | Thing 2 Widget 2 
 3       | Thing 3   | 3        | 3       | Thing 3 Widget 1 

However, I only want the newest Widget for each Thing, i.e.:

 ThingId | ThingName | WidgetId | ThingId | WidgetName       
---------+-----------+----------+---------+------------------
 1       | Thing 1   | NULL     | NULL    | NULL             
 2       | Thing 2   | 2        | 2       | Thing 2 Widget 2 
 3       | Thing 3   | 3        | 3       | Thing 3 Widget 1 

My starting point was:

SELECT * FROM Things t LEFT OUTER JOIN (SELECT TOP 1 * FROM Widgets subw WHERE subw.ThingId = t.ThingId ORDER BY subw.WidgetId DESC) w ON t.ThingId = w.ThingId

But this is not valid because the parent t.ThingId does not exist in the sub query.

Can this be achieved using SQL 2000?

  • 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-15T00:56:08+00:00Added an answer on June 15, 2026 at 12:56 am

    If (ThingId, WidgetId) combination is unique in table Widgets, then this will work correctly:

    SELECT t.*, w.* 
    FROM 
        dbo.Things AS t 
      LEFT OUTER JOIN 
          ( SELECT ThingId, MAX(WidgetId) AS WidgetId 
            FROM dbo.Widgets 
            GROUP BY ThingId
          ) AS 
        subw
          ON  subw.ThingId = t.ThingId
      LEFT OUTER JOIN
        dbo.Widgets AS w
          ON  w.ThingId = subw.ThingId
          AND w.WidgetId = subw.WidgetId ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this question has been asked multiple times but all the answers seem
This question has been asked before perhaps multiple times, but I can't get the
I know that this question has been asked multiple times already , but I
I know this flavor of question has been asked multiple times, but I've spent
I know this question has been asked multiple times before, but none of the
This question has been asked multiple times, however none of the answers seem to
The question has been asked multiple times, but this is not a repeat. I
I know this question has been asked multiple times, but nobody has been able
Apologies, this question has been asked multiple times, but asking again due to the
This question has been asked before but the answers aren't always clear or are

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.