UNPIVOT中的处理NULL值

问题描述 投票:7回答:4

我能够取消透视表,但结果中不包含空值。

create table pivot_task
(
age int null,
[a] numeric(8,2),
[b] numeric(8,2),
[c] numeric(8,2),
[d] numeric(8,2),
[e] numeric(8,2)
);

select * from pivot_task;

insert into pivot_task values (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 age, [over], [av]
from pivot_task
unpivot
(
 [av]
 for [over] in ([a], [b], [c], [d], [e])
) a;

您可以在http://sqlfiddle.com/#!6/2ab59/1上看到18岁以上[b]的结果,并且缺少空值,我也想在每次遇到空值时都包括空值。

我发现用不同的值替换null,然后替换所有那些不变的不同值方法对于我的工作不可行。我只想将其包括在内。

sql sql-server sql-server-2008 sql-server-2012 unpivot
4个回答
4
投票

这很丑陋,但不必依靠NULL的带外替换:

declare @pivot_task table
(
age int null,
[a] numeric(8,2),
[b] numeric(8,2),
[c] numeric(8,2),
[d] numeric(8,2),
[e] numeric(8,2)
);

insert into @pivot_task values (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 select 'b' union all select 'c'
        union all select 'd' union all select 'e') pmu
cross join (select age from @pivot_task) as a
left join
@pivot_task pt
unpivot
(
 [av]
 for [over] in ([a], [b], [c], [d], [e])
) ex
on pmu.[over] = ex.[over] and
   a.age = ex.age

结果:

age         over av
----------- ---- ---------------------------------------
18          a    0.50
18          b    NULL
18          c    0.60
18          d    1.21
18          e    1.52
19          a    7.51
19          b    6.51
19          c    5.51
19          d    NULL
19          e    3.53
20          a    4.52
20          b    4.52
20          c    6.52
20          d    3.53
20          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') pmu
cross join @pivot_task as a

3
投票

[尝试一下,它将在取消旋转之前用10000000替换所有空值,该值不是numeric(8,2)中的可接受数字,因此该值将不存在。然后,在取消透视后,该值将替换为null:

;WITH x as
(
select 
age,
coalesce(cast(a as numeric(9,2)), 10000000) a,
coalesce(cast(b as numeric(9,2)), 10000000) b,
coalesce(cast(c as numeric(9,2)), 10000000) c,
coalesce(cast(d as numeric(9,2)), 10000000) d,
coalesce(cast(e as numeric(9,2)), 10000000) e
from pivot_task
)
select age, [over], nullif([av], 10000000) av
from x
unpivot
(
 [av]
 for [over] in ([a], [b], [c], [d], [e])
) a;

1
投票

UN ISIVNULL(columnname,0)http://technet.microsoft.com/en-us/library/ms184325.aspx对于所有列,请先取消安装。

如下所示。

Select Age, Data, Case When (Value = 0) Then NULL Else Value End Value
from (
select age, 
    ISNULL([a],0)[a], ISNULL([b],0)[b], ISNULL([c],0)[c], ISNULL([d],0)[d], ISNULL([e],0)[e]
From pivot_task) As pvttask
UnPivot ([Value] for [Data] In ([a], [b], [c], [d], [e])) a

0
投票

您可以使用零代替零吗?

© www.soinside.com 2019 - 2024. All rights reserved.