Я пытаюсь использовать dense_rank
и row_number
оконные функции.
Вторая попытка -
declare @table table (Symbol varchar(10));
insert into @table values
('a')
,('ab')
,('abc')
,('abd')
,('abda')
,('abdb')
,('b')
,('ba')
,('bb')
,('bbbc')
,('bbc')
,('abcd');
;with cte as (
select left(symbol, 1) as FirstSymbol
from @table
group by left(symbol, 1)
),
cte2 as (
select *, ROW_NUMBER() over (partition by b.firstSymbol order by a.symbol) as RankInGroup
from @table as a
left join cte as b on left(a.Symbol, 1) = b.FirstSymbol
)
select a.Symbol as Symbol, b.Symbol as ParentSymbol
from cte2 as a
left join cte2 as b on a.FirstSymbol = b.FirstSymbol and a.RankInGroup = b.RankInGroup + 1;
Первая попытка -
declare @table table (Symbol varchar(10));
insert into @table values
('a')
,('ab')
,('abc')
,('abd')
,('abda')
,('abdb')
,('b')
,('ba')
,('bb')
,('bbbc')
,('bbc')
,('abcd');
--select * from @table;
;with cte as (
select
symbol,
dense_rank() over (
order by (
case
when symbol like 'a%' then 1 when symbol like 'b%' then 2 when symbol like 'c%' then 3 when symbol like 'd%' then 4
when symbol like 'e%' then 5 when symbol like 'f%' then 6 when symbol like 'g%' then 7 when symbol like 'h%' then 8
when symbol like 'i%' then 9 when symbol like 'j%' then 10 when symbol like 'k%' then 11 when symbol like 'l%' then 12
when symbol like 'm%' then 13 when symbol like 'n%' then 14 when symbol like 'o%' then 15 when symbol like 'p%' then 16
when symbol like 'q%' then 17 when symbol like 'r%' then 18 when symbol like 's%' then 19 when symbol like 't%' then 20
when symbol like 'u%' then 21 when symbol like 'v%' then 22 when symbol like 'w%' then 23 when symbol like 'x%' then 24
when symbol like 'y%' then 25 when symbol like 'z%' then 26
end)
) as GroupNumber,
row_number() over (
partition by (
case
when symbol like 'a%' then 1 when symbol like 'b%' then 2 when symbol like 'c%' then 3 when symbol like 'd%' then 4
when symbol like 'e%' then 5 when symbol like 'f%' then 6 when symbol like 'g%' then 7 when symbol like 'h%' then 8
when symbol like 'i%' then 9 when symbol like 'j%' then 10 when symbol like 'k%' then 11 when symbol like 'l%' then 12
when symbol like 'm%' then 13 when symbol like 'n%' then 14 when symbol like 'o%' then 15 when symbol like 'p%' then 16
when symbol like 'q%' then 17 when symbol like 'r%' then 18 when symbol like 's%' then 19 when symbol like 't%' then 20
when symbol like 'u%' then 21 when symbol like 'v%' then 22 when symbol like 'w%' then 23 when symbol like 'x%' then 24
when symbol like 'y%' then 25 when symbol like 'z%' then 26
end) order by symbol
) as RankInGroup
from @table
)
select a.Symbol as Symbol, b.Symbol as ParentSymbol
from cte a
left join cte b on a.GroupNumber = b.GroupNumber and a.RankInGroup = b.RankInGroup + 1;