【发布时间】:2021-05-07 02:44:44
【问题描述】:
我有一个 EAV 表,我将其转为几列。
如果[h1]、[h2] 和[h3] 都是null,我不想返回一行。
declare
@h1 nvarchar(10) = 'A',
@h2 nvarchar(10) = 'B',
@h3 nvarchar(10) = 'C'
select
[id],
[h1] = isNull(max( case when [key] = @h1 then [value] end ), ''),
[h2] = isNull(max( case when [key] = @h2 then [value] end ), ''),
[h3] = isNull(max( case when [key] = @h3 then [value] end ), ''),
from some..db
group by [id]
having (
and max( case when [key] = @h1 then [value] end) is not null
and max( case when [key] = @h2 then [value] end) is not null
and max( case when [key] = @h3 then [value] end) is not null
)
如何旋转此表并删除 [h] 列中具有 null 值的行?
【问题讨论】:
-
我有点迷路了。您使用的是
isnull(),所以返回的值都不是NULL。 -
我认为,因为我在
having子句中重新评估isnull()将被忽略。having子句中的<> ''会解决这个问题吗? -
No
<> ''不行,也不要在HAVING中使用ISNULL,只需将OR改为AND -
如果
全部为空,我不想返回一行 - 所以直接将其转换为过滤器:not(col1 is null and col2 is null and col3 is null)
标签: sql sql-server null pivot