SQL优化选择case语句

时间:2013-09-21 10:28:13

标签: sql-server-2008 optimization coding-style case

是否可以简化以下案例陈述,或者我是否已经以最优雅的形式拥有它?

select case when Ks2en = '' then 'No KS2' else ks2en end as 'KS2',
       nullif(count(case result when '' then 1 end),0) as 'No Result',
       nullif(count(case result when 'U' then 1 when '1a' then 1 when '1b' then 1 when '1c' then 1 end),0) as '1/U',
       nullif(count(case result when 'U' then 1 when '2a' then 1 when '2b' then 1 when '2c' then 1 end),0) as '2/U',
       nullif(count(case result when 'G-' then 1 when '3c' then 1 end),0) as '3c/G-',
       nullif(count(case result when 'G' then 1 when '3b' then 1 end),0) as '3b/G',
       nullif(count(case result when 'G+' then 1 when '3a' then 1 end),0) as '3a/G+',
       nullif(count(case result when 'F-' then 1 when '4c' then 1 end),0) as '4c/F-',
       nullif(count(case result when 'F' then 1 when '4b' then 1 end),0) as '4b/F'
       **snip**
  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

它会生成以下网格,我希望此结果保持不变:

KS2 No Result   1/U     2/U     3c/G-   3b/G    3a/G+   4c/F-   4b/F    **snip**
No  KS2         1       NULL    NULL    NULL    NULL    NULL    NULL    **snip**
2a  NULL        NULL    NULL    NULL    NULL    NULL    2       1       **snip**
3c  1           NULL    NULL    NULL    NULL    NULL    NULL    NULL    **snip**
3b  NULL        NULL    NULL    NULL    NULL    NULL    1       NULL    **snip**
3a  1           NULL    NULL    NULL    NULL    NULL    NULL    NULL    **snip**
4c  NULL        1       1       NULL    NULL    NULL    NULL    NULL    **snip**
4b  NULL        NULL    NULL    NULL    NULL    NULL    NULL    NULL    **snip**
4a  NULL        1       1       NULL    NULL    NULL    NULL    NULL    **snip**
5c  NULL        NULL    NULL    NULL    NULL    NULL    NULL    NULL    **snip**
5b  NULL        NULL    NULL    NULL    NULL    NULL    NULL    NULL    **snip**

我为了简洁而剪切了代码和结果,但是为了创建总计列,sql包含了更多的情况。

1 个答案:

答案 0 :(得分:1)

我不认为这段代码会提高性能,所以我发布了一种不同的方式,IMO更优雅。我使用result in ([listofoptions])而不是多个case ... when,我认为执行不会发生太大变化。

select case when Ks2en = '' then 'No KS2' else ks2en end as 'KS2',
      nullif(count(case when result = '' then 1 end),0) as 'No Result',
      nullif(count(case when result IN ('U', '1a', '1b', '1c') then 1 end),0) as '1/U',
      nullif(count(case when result IN ('U', '2a', '2b', '2c') then 1 end),0) as '2/U',
      nullif(count(case when result IN ('G-','3c') then 1 end),0) as '3c/G-',
      nullif(count(case when result IN ('G', '3b') then 1 end),0) as '3b/G',
      nullif(count(case when result IN ('G+','3a') then 1 end),0) as '3a/G+',
      nullif(count(case when result IN( 'F-','4c') then 1 end),0) as '4c/F-',
      nullif(count(case when result IN( 'F', '4b') then 1 end),0) as '4b/F'

from student 
inner 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