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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:23:28+00:00 2026-05-18T02:23:28+00:00

Apologies for the title, I am unsure how to express my problem… I have

  • 0

Apologies for the title, I am unsure how to express my problem…
I have a table that looks something like this:

A     | B
--------------
01    | 1
01A   | 1
01B   | 1
01C   | 1
02    | 1
03    | 1
03    | 2
03A   | 1
03B   | 1

Sometimes column A is qualified with a letter (##A,##B), sometimes it is not (##). I would need to

SELECT A,B from Table

if there were no suffixes. With the suffix, however, I need to select the maximum value of A for each B. (That was phrased poorly). I would expect the following results for the table above:

A     | B
--------------
01C   | 1
02    | 1
03    | 2
03B   | 1

Right now, I am using a subselect and my query is:

SELECT A,B FROM Table t1
WHERE t1.A = (SELECT MAX(t2.A) FROM Table t2WHERE LEFT(t1.A,2) = LEFT(t2.A,2)

This is incredibly inefficient, however, and my table is pretty large, so they query is taking far too long to run. Is there a better way?

Thanks!

  • 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-18T02:23:29+00:00Added an answer on May 18, 2026 at 2:23 am
    ;with cteRowNumbers as (
        select A, B, ROW_NUMBER() over(partition by B, left(A,2) order by A desc) as RowNumber
            from YourTable
     )
     select A, B
        from cteRowNumbers
        where RowNumber = 1
        order by A
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Apologies for the fuzzy title... My problem is this; I have a SQL Server
Apologies for the vague title, but this is quite a difficult problem to explain
Apologies for the appalling title. I have mocked up this code to mimic an
Apologies for the difficult title, but I essentially have a model Foo that has
First of all, my apologies for the title of this question, I don't have
Apologies for the confusing title, didn't know how to express it better. This is
(Apologies for the title of this question - I wasn't overly sure how to
Apologies for the poor question title. I have two tables, jobs and Persons-Jobs. Jobs
My apologies for an inaccurate title, but I'm not sure what this is called
First: apologies for the question title, I don't actually know what the problem is

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.