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

The Archive Base Latest Questions

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

I want a more elegant way to select the top category in the table

  • 0

I want a more elegant way to select the top category in the table below by LastUpdatedDateTime descending. Basically I only want to see the latest single row for each Category by CategoryID.

I can get that by doing the following…

SELECT Category,
   MAX(LastUpdateDateTime) as LastUpdateDateTime
INTO #t
FROM Settings
WHERE CatalogID = 123
GROUP BY Category
ORDER BY Category

SELECT s.*
FROM Settings s
INNER JOIN #t t
ON s.Category = t.Category
AND s.LastUpdateDateTime = t.LastUpdateDateTime

–> Settings table

CREATE TABLE [dbo].[Settings](
[ID] [int] IDENTITY(1,1) NOT NULL,
[CatalogID] [int] NOT NULL,
[Category] [varchar](50) NOT NULL,
[Facings] [bit] NOT NULL,
[Quantity] [bit] NOT NULL,
[LastUpdateDateTime] [datetime] NOT NULL,
 CONSTRAINT [PK_Settings_1] PRIMARY KEY CLUSTERED 
 (
[ID] ASC,
[CatalogID] ASC,
[Category] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF,     ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

This is pretty simple, just looking for someone to point me in the right direction to do this a little more efficiently. What I have WORKS. I want to refactor please.

Thanks for looking.

-B

  • 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-11T22:28:14+00:00Added an answer on June 11, 2026 at 10:28 pm

    How about a sub-query:

    select s1.*
    from settings s1
    inner join
    (
      select category, max(LastUpdateDateTime) as LastUpdateDateTime
      from settings
      where catalogid = 123
      group by category
    ) s2
      on s1.category = s2.category
      and s1.LastUpdateDateTime = s2.LastUpdateDateTime
    

    or even use CTE:

    ;with cte as
    (
      select *, 
        row_number() over(partition by catalogid 
                          order by LastUpdateDateTime desc) rn
      from settings
      where catalogid = 123
    )
    select *
    from cte
    where rn = 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a more elegant way of doing this: SELECT TOP (@NumOfGames) (SUM(IIF(Fixture.HomeTeamID =
What's a more elegant way of having the code below where i want to
I'm trying to style a select option list using CSS. I want more padding
Still kinda new to LINQ. Seems like there should be a more elegant way
I'm looking for a simpler, more elegant way to replace text in XML. For
EF4.1-Code-First-Gurus! I wonder if there is a more elegant way to handle the following
I'm looking for a more elegant way to do something like this: [data[i/2] if
Is there any more elegant way to escape SimpleXML attributes to an array? $result
I know there has got to be a cleaner more elegant way to do
Would there a more elegant way of writing the following syntax? Thread t0 =

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.