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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:29:56+00:00 2026-05-16T15:29:56+00:00

I have table: CREATE TABLE [dbo].[test] ( [name] nvarchar(max) NULL, [date] datetime NULL )

  • 0

I have table:
CREATE TABLE [dbo].[test] (
[name] nvarchar(max) NULL,
[date] datetime NULL
)

And records on it:

a 2010-09-02 12:00:00 
a 2010-09-02 11:00:00 
b 2010-09-02 12:00:00 
b 2010-09-02 11:00:00

And i want to get all name with the newest date:

I may do:

select t.[name] from test t
group by t.[name]
having max(date) = (select MAX(DATE) from test where [name] = t.[name])

which have one problem – i can’t get a date

I may do:

select t.* 
    from test t
    where t.[date] = (select MAX(DATE) from test where [name] = t.[name])

which hasn’t any problem

My question is:
May i do this better?? I will fetch around 10,000 records from table incremental table(every day 10,000 more results).

Regards

  • 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-16T15:29:57+00:00Added an answer on May 16, 2026 at 3:29 pm

    What version of SQL Server?

    SQL 2005 and up:

    SELECT *
    FROM
       (SELECT Item = Row_Number() OVER (PARTITION BY [name] ORDER BY [date] DESC), * FROM test) X
    WHERE Item = 1
    

    SQL 2000:

    SELECT T.*
    FROM
       test T
       INNER JOIN (
          SELECT [name], MaxDt = Max([date]) FROM test GROUP BY [name]
       ) X ON T.[name] = X.[name] AND T.[date] = X.MaxDt
    

    If you can have duplicate dates then another step is needed for the sql 2000 version to get it down to one row.

    @Oded pointed out that you can simply get the max date. If all you need are the name and date then his query is best. But if my suspicion is correct that you need more items from the same row, then you’ll need queries like these.

    Here’s another SQL 2005 version:

    SELECT
       T.*
    FROM
       test T
       CROSS APPLY (
          SELECT TOP 1 [date]
          FROM test T2
          WHERE T.[name] = T2.[name]
          ORDER BY T2.[date] DESC
       ) X
    WHERE
       T.[date] = X.[date]
    

    This query will have problems with duplicate max dates for the same name

    Update

    Now that I know it’s SQL 2008:

    The row_number() solution is simplest and easiest. I’d start with that. If performance isn’t enough, and the table is a child of a parent table that has each [name] only once, try the CROSS APPLY solution with the outer table (test T) as the parent:

    SELECT
       X.*
    FROM
       Parent P
       CROSS APPLY (
          SELECT TOP 1 *
          FROM test T
          WHERE P.[name] = T.[name]
          ORDER BY T.[date] DESC
       ) X
    

    If there is no parent table, you can try the above queries or use SELECT DISTINCT [name] FROM test but I’m not convinced that will be a performance improvement:

    SELECT
       X.*
    FROM
       (SELECT DISTINCT [name] FROM test) P
       CROSS APPLY (
          SELECT TOP 1 *
          FROM test T
          WHERE P.[name] = T.[name]
          ORDER BY T.[date] DESC
       ) X
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this table CREATE TABLE [dbo].[friend_blocked_list]( [subdomain] [varchar](50) NOT NULL, [un] [nvarchar](50) NOT
I have Created a table like CREATE TABLE [dbo].[tab1]( [Id] [int] NOT NULL, [Name]
I have the following table: CREATE TABLE [dbo].[Data] ( [Id] UNIQUEIDENTIFIER NOT NULL, [Data]
I have a table defined as follows: CREATE TABLE [dbo].[T]( [FileID] [bigint] NOT NULL,
I have several tables: CREATE TABLE [dbo].[Tracks]( [Id] [uniqueidentifier] NOT NULL, [Artist_Id] [uniqueidentifier] NOT
I have this sql statement: CREATE TABLE [dbo].[User]( [UserId] [int] IDENTITY(1,1) NOT NULL, [FirstName]
I have the following table: CREATE TABLE [dbo].[Tree]( [AutoID] [int] IDENTITY(1,1) NOT NULL, [Category]
I've got the following SQL table: CREATE TABLE [dbo].[Test]( [TestID] [int] NOT NULL, [TestNum]
I have the following stored procedure ALTER PROCEDURE [dbo].Test AS BEGIN CREATE TABLE ##table
I have a following table CREATE TABLE [dbo].[test_table] ( [ShoppingCartID] [int] IDENTITY(1,1) NOT NULL,

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.