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?
For the first table: