列为空时如何省略聚合行

How to omit aggregated row when columns are null

我有一个 EAV table,我正在将其分为几列。

如果 [h1][h2][h3] 都是 null

,我不想连续 return
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
)

如何旋转此 table 并删除 [h] 列中具有 null 值的行?

根据 @Charlieface 的反馈。

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 (
  max( case when [key] = @h1 then [value] end) is not null
  or max( case when [key] = @h2 then [value] end) is not null
  or max( case when [key] = @h3 then [value] end) is not null
)

或通过 @astentx

的替代 having 子句
having max(case when [key] in (@h1, @h2, @h3) then 1 end) is not null