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

  • Home
  • SEARCH
  • 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 8916661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:17:50+00:00 2026-06-15T05:17:50+00:00

I have an ugly table with codes and names from a source I can’t

  • 0

I have an ugly table with codes and names from a source I can’t control, which is like this one (OriginalTable):

Code    | Name
--------------------
001-001 | Name1_a
001-002 | Name1_a
001-002 | Name1_b
001-003 | Name1_a
002-001 | Name2_a
002-001 | Name2_b
002-002 | Name2_a
003-001 | Name3
...

The problem is that I need an unique name for the first 3 digits of each code (SmallCode), like in the following table:

Id  | Code  | Name
--------------------
1   | 001   | NameX
2   | 002   | NameY
3   | 003   | NameZ

The criteria I want use for choosing a name is that it should be the most repeated name or the first one in each SmallCode.
For example NameX is the most repeated name on of all codes starting with 001 or the first one (Name1_a in both cases). Same with NameY for 002 and NameZ for 003.

Right now I was using this query:

select Substring(Code,1,3) as SmallCode, Code, Name
into #tmpCode
from OriginalTable

select SmallCode, Min(Code) as Code
into #tmpReducedCode
from #tmpCode
group by SmallCode

insert into ResultTable (Code, Name)
select a.SmallCode, a.Name
from #tmpCode a
    inner join #tmpReducedCode b
        on a.Code = b.Code

But this is my result, which is wrong because there are 2 different names for code 002-001 (Name2_a, Name2_b)

1   | 001   | Name1_a
2   | 002   | Name2_a
3   | 002   | Name2_b
4   | 003   | Name3

So the question is: How can I separate OriginalTable into those 2 tables choosing the most repeated or first appearing name for each small code?

  • 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-15T05:17:51+00:00Added an answer on June 15, 2026 at 5:17 am

    For the first table:

    select Substring(Code,1,3) as SmallCode, Code, Name
    into #tmpCode
    from OriginalTable
    
    select SmallCode, Name
    into #tmpReducedCode
    from (
        select SmallCode, Name, row_number() over (partition by SmallCode order by Total desc) rn
        from (
            select SmallCode, Name, count(*) Total
            from #tmpCode
            group by SmallCode, Name) x) y
    where rn=1;
    
    select distinct a.SmallCode, b.Name
    from #tmpCode a
        inner join #tmpReducedCode b
            on left(a.Code,3) = b.SmallCode
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two files. One is a file of table creation strings dumped from
I have two tables. One table looks like ORDER_ID | SHOPPER_NAME | DATE -------------------------------
I have this ugly SELECT statement : Select 'Urbain' as Véhicule, Count( DISTINCT Plaintes.IDEvenements)
I have a table called informations (http://pastebin.com/h3UiUhpx) and also have this associated tables educations,
I have a very large table in Mathematica ((dimcub-1)^3 elements) coming from an inverse
Hi I have made this function that takes a table and prepare the label
I have a few before filters which I am using to control access to
I have a partial view which renders a table. On page load, I am
I have a table containing another table. It's pretty ugly but it's what I'm
I have a standard 'can-I-avoid-a-loop' problem, but cannot find a solution. I answered this

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.