with temp
as
(
select '蟾殊' nm union all
select '蟾碁' union all
select '蠍企宏' union all
select '螳' union all
select '' union all
select '碁' union all
select '朱企宏' union all
select '殊企' union all
select '襷覿' union all
select '覈レ' union all
select '覦ロ殊' union all
select '覦レ'
), part1(seq, part1)
as
(
select 0, '' union all
select 1, '' union all
select 2, '' union all
select 3, '' union all
select 4, '' union all
select 5, '' union all
select 6, '' union all
select 7, '' union all
select 8, '' union all
select 9,'' union all
select 10,'' union all
select 11,'' union all
select 12,'' union all
select 13,'' union all
select 14,'' union all
select 15,'' union all
select 16,'' union all
select 17,'' union all
select 18,''
), part2(seq, part2)
as
(
select 0, '' union all
select 1, '' union all
select 2, '' union all
select 3, '' union all
select 4, '' union all
select 5, '' union all
select 6, '' union all
select 7, '' union all
select 8, '' union all
select 9,'' union all
select 10,'' union all
select 11,'' union all
select 12,'' union all
select 13,'' union all
select 14,'' union all
select 15,'' union all
select 16,'' union all
select 17,'' union all
select 18,'' union all
select 19,'' union all
select 20,''
), part3(seq, part3)
as
(
select 0, '' union all
select 1, '' union all
select 2, '' union all
select 3, '' union all
select 4, '' union all
select 5, '' union all
select 6, '' union all
select 7, '' union all
select 8, '' union all
select 9,'' union all
select 10,'' union all
select 11,'' union all
select 12,'' union all
select 13,'' union all
select 14,'' union all
select 15,'' union all
select 16,'' union all
select 17,'' union all
select 18,'' union all
select 19,'' union all
select 20,'' union all
select 21,'' union all
select 22,'' union all
select 23,'' union all
select 24,'' union all
select 25,'' union all
select 26,'' union all
select 27,''
), rs
as
(
select
nchar(part1) part1
, case when sum(part2)/count(*) = min(part2) then nchar(min(part2)) end part2
, case when
sum(part2)/count(*) = min(part2)
and sum(part3)/count(*) = min(part3)
then nchar(min(part3))
end part3
from (
select
nm
, 0x1100 + ((unicode(nm) - 0xAC00) / (21*28)) part1
, 0x1161 + ((unicode(nm) - 0xAC00) % (21*28)) / 28 part2
, 0x11A7 + ((unicode(nm) - 0xAC00) % 28) part3
from temp
) t
group by
nchar(part1)
)
select
case
when a.part1 is not null and a.part2 is null and a.part3 is null
then a.part1
when a.part1 is not null and a.part2 is not null and a.part3 is null
then nchar(0xAC00 + (b.seq*21*28)+(c.seq*28))
when a.part1 is not null and a.part2 is not null and a.part3 is not null
then nchar(0xAC00 + (b.seq*21*28)+(c.seq*28)+d.seq)
end 蟆郁骸
from rs a
left join part1 b
on a.part1 = b.part1
left join part2 c
on a.part2 = c.part2
left join part3 d
on a.part3 = d.part3
/*
蟆郁骸
----
覦
(5螳 )
*/