我想将结果集中的某些值分组为新值。
例如我的结果集是:
KS2
2a
3c
4c
3c
2a
2a
1c
No KS2
1b
2c
5c
4c
我在底部组的当前 SQL 导致以下结果:
KS2
2a
3c
4c
1c
No KS2
1b
2c
5c
4c
我想将值 1a、1b 和 1c 分组到值 1 和 2a、2b 和 2c 下的单行中作为 2。所有其他值应该正常分组。所以我的结果集看起来像:
KS2
2
3c
4c
1
No KS2
5c
4c
这是我的代码:
GROUP BY
CASE Name
WHEN 'English' THEN
CASE WHEN [Ks2en]=NULL OR [Ks2en]='' THEN
'No KS2'
ELSE
[Ks2en]
END
WHEN 'Mathematics' THEN
CASE WHEN [Ks2ma]=NULL OR [Ks2ma]='' THEN
'No KS2'
ELSE
[Ks2ma]
END
ELSE
CASE WHEN [Ks2av]=NULL OR [Ks2av]='' THEN
'No KS2'
ELSE
[Ks2av]
END
END
编辑:这是基于 EricZ 回答的解决方案:
GROUP BY
CASE Name
WHEN 'English' THEN
CASE WHEN [Ks2en] IS NULL OR [Ks2en]='' THEN
'No KS2'
WHEN [Ks2en] IN ('1a','1b','1c') THEN
'1'
WHEN [Ks2en] IN ('2a','2b','2c') THEN
'2'
ELSE
[Ks2en]
END
WHEN 'Mathematics' THEN
CASE WHEN [Ks2ma] IS NULL OR [Ks2ma]='' THEN
'No KS2'
WHEN [Ks2ma] IN ('1a','1b','1c') THEN
'1'
WHEN [Ks2ma] IN ('2a','2b','2c') THEN
'2'
ELSE
[Ks2ma]
END
ELSE
CASE WHEN [Ks2av] IS NULL OR [Ks2av]='' THEN
'No KS2'
WHEN [Ks2av] IN ('1a','1b','1c') THEN
'1'
WHEN [Ks2av] IN ('2a','2b','2c') THEN
'2'
ELSE
[Ks2av]
END
END