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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T15:15:55+00:00 2026-05-12T15:15:55+00:00

The DDL creates the schema and data. I am looking for a where statement

  • 0

The DDL creates the schema and data. I am looking for a where statement where it returns only one row for each ID and that row would be the last inserted row based on the inserteddate column.

So the result would be John, 5 and Debbie, 5

select Table1.Name, Table2.Rating
From table1 join table2 on table1.ID = table2.ID
where inserteddate = max(insertedate) 

.. for each ID? It seems simple but I am having a brain block.

DDL:

CREATE TABLE [dbo].[Table1](
    [Table1ID] [int] NULL,
    [Name] [varchar](50) NULL
)

CREATE TABLE [dbo].[Table2](
    [Table2ID] [int] NULL,
    [InsertedDate] [datetime] NULL,
    [Rating] [varchar](50) NULL
)

INSERT INTO [dbo].[Table1]([Table1ID], [Name])
SELECT 1, N'John' UNION ALL
SELECT 2, N'Debbie'
INSERT INTO [dbo].[Table2]([Table2ID], [InsertedDate], [Rating])
SELECT 1, '20090101 00:00:00.000', N'6' UNION ALL
SELECT 1, '20090401 00:00:00.000', N'5' UNION ALL
SELECT 2, '20090202 00:00:00.000', N'3' UNION ALL
SELECT 2, '20090303 00:00:00.000', N'5'
  • 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-12T15:15:55+00:00Added an answer on May 12, 2026 at 3:15 pm

    How about this:

    SELECT 
      Table1.Name, Table2.Rating
    FROM
      table1 
    INNER JOIN 
      table2 ON table1.Table1ID = table2.Table2ID
    WHERE
      inserteddate = (SELECT MAX(InsertedDate) 
                        FROM Table2 t2 
                       WHERE t2.Table2ID = Table2.Table2ID)
    

    In this case, an index on the InsertedDate column would be very helpful!

    Or if you’re on SQL Server 2005 and up, you could also use a CTE (Common Table Expression) with a ROW_NUMBER() and a PARTITION OVER statement like this:

    WITH HelperCTE AS
    (
      SELECT
        Table1.Name, Table2.Rating,
        ROW_NUMBER() OVER(PARTITION BY Table1.Table1ID 
                          ORDER BY Table2.InsertedDate DESC) AS 'RowNum'
      FROM
        table1 
      INNER JOIN
        table2 ON table1.Table1ID = table2.Table2ID
    )
    SELECT Name, Rating FROM HelperCTE
    WHERE RowNum = 1
    

    This creates like a “temporary” view (the CTE) and numbers the entries partitioned by Table1ID (individual numbering for each separate Table1ID) and orders them descending by InsertedDate – so for each unique Table1ID, the most recent entry will have RowNum = 1.

    Marc

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

Sidebar

Related Questions

I am looking into using only a ddl to run my query, not a
Does anyone know of a reasonably priced tool that will create DDL statements to
I have a Default.aspx that redirects to a Page1.aspx. My ddl is loaded and
I have an existing database that has some testing data into and I'm interested
I would like to confirm that the following analysis is correct: I am building
I need a code which creates me the table row based on the selection
I maintain a product that has a home-grown data access layer, programmed in C++/COM
I have a maven build that creates an ear wich is deployed to Glassfish
I have a migration that's breaking in the middle of a couple of schema
I am trying to generate an insert statement from a row in a table?

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.