0

我有一个 SQL 查询,它根据名为 ks2en 的列中的值进行分组。列中的空白结果分组很好,但我想用文本“No KS2”替换空白值,以便在分组时得到“No KS2”而不是空白值。

这是我的查询当前的结果:

KS2         No Result   A*
            1           0
2a          0           0
3c          1           0
3b          0           0
3a          1           0
4c          0           0
4b          0           0
4a          0           0
5c          0           0
5b          0           0

这是我希望它看起来的样子:

KS2         No Result   A*
No KS2      1           0
2a          0           0
3c          1           0
3b          0           0
3a          1           0
4c          0           0
4b          0           0
4a          0           0
5c          0           0
5b          0           0

这是我当前的 SQL:

select ks2en as 'KS2',
       count(case result when '' then 1 end) as 'No Result',
       count(case result when 'A*' then 1 end) as 'A*'
  from student join subject 
    on subject.upn=student.upn 
 where name='English'
 group by ks2en
 order by
    case when ks2en = 'W' Then 0 Else 1 End,
    left(ks2en, 1),
    right(ks2en, 1) desc
4

2 回答 2

2

Coalesce assumes null, not blank. just case statement it instead

case when Ks2en = '' then 'No KS2' else ks2en end as 'KS2' 
于 2013-09-20T22:27:36.007 回答
1

If there are spaces only.

 select case when LEN(RTRIM(Ks2en)) = 0 then 'No KS2' else 'KS2' end
于 2013-09-20T22:28:22.760 回答