たった2つの用語で、次のようなことができます。
declare @T table (display_term char(1) not null,occurence int not null)
insert into @T (display_term,occurence) values
('A',1),
('A',4),
('B',3),
('B',9)
select top 1
*
from
@T t1
cross join
@T t2
where
t1.display_term = 'A' and
t2.display_term = 'B'
order by ABS(t1.occurence - t2.occurence)
生成するもの:
display_term occurence display_term occurence
------------ ----------- ------------ -----------
A 4 B 3
UNPIVOT
(要求した結果セットが正確に必要な場合は、ベースのソリューションを検索できます)
これをより多くの用語に拡張する必要があるかどうかはあなたの質問からは明らかではありません-より多くの用語の要件を再解釈する明確な方法がないので、私は今のところそれを元に戻しました。
UNPIVOT
正確な結果セットが必要な場合は、ベースのソリューション。@T
上記のように設定します。
select display_term,occurence from (
select top 1
t1.occurence as A,
t2.occurence as B
from
@T t1
cross join
@T t2
where
t1.display_term = 'A' and
t2.display_term = 'B'
order by ABS(t1.occurence - t2.occurence)
) t unpivot (occurence for display_term in (A,B)) as u
結果:
display_term occurence
------------------------------------ -----------
A 4
B 3