これは醜いですが、 NULL
の帯域外の代替を見つける必要はありません :
declare @pivot_task table(age int null,[a] 数値(8,2),[b] 数値(8,2),[c] 数値(8,2),[d] 数値( 8,2),[e] numeric(8,2));insert into @pivot_task 値 (18, 0.5, null, 0.6, 1.21, 1.52),(19, 7.51, 6.51, 5.51, null, 3.53),( 20, 4.52, 4.52, 6.52, 3.53, null);select a.age, pmu.[over], [av]from (select 'a' as [over] union all all select 'b' union all select 'c' union all select 'd' union all select 'e') pmucross join (@pivot_task から年齢を選択) as aleft join@pivot_task ptunpivot( [av] for [over] in ([a], [b], [c], [ d], [e])) exon pmu.[over] =ex.[over] および a.age =ex.age
プレ>結果:
age over av----------- ---- -------------------------- -------------18 a 0.5018 b NULL18 c 0.6018 d 1.2118 e 1.5219 a 7.5119 b 6.5119 c 5.5119 d NULL19 e 3.5320 a 4.5220 b 4.5220 c 6.5220 d 3.5320 e NULL
プレ>ただし、このルートをたどる場合は、
UNPIVOT
を削除できます。 完全に:select a.age, pmu.[over], CASE pmu.[over] WHEN 'a' THEN a.a WHEN 'b' THEN a.b WHEN 'c' THEN a.c WHEN 'd' THEN a.d WHEN 'e ' THEN a.e END [av]from (select 'a' as [over] union all select 'b' union all select 'c' union all select 'd' union all select 'e') pmucross Join @pivot_task as aコード> プレ>