我有一张名为student的表:
学生[UPN,ks2en,ks2ma,ks2ma]
这是一个名为subject:
的表主题[UPN,结果]
我使用case语句指定要在结果集中使用哪个Student列。这是我的案例陈述:
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
结果集还包括取决于主题结果的计数函数,但这对我的问题并不重要。
我想要做的是强制结果集包含一组特定的值,无论这些值是否存在于原始列中。
例如,主题是英语,因此结果集使用学生列ks2en,但这只包含以下值:
4a, 4a, 3b, 3c, 5a. The resultset when using a GROUP BY gives:
4a
3b
3c
5a
但是我希望GROUP BY默认至少包含以下内容,但是在计数和数字中有NULL值。
1, 2, 3c, 3b, 3a, 4c, 4b, 4a, 5c, 5b, 5a, 6c, 6b, 6a
答案 0 :(得分:0)
一般构造将是:
;WITH fixed_values AS
(
SELECT x FROM (VALUES('1'),('2'),('3c'), ...) AS x(x)
)
SELECT x FROM fixed_values
LEFT OUTER JOIN
(
-- your grouped query
) AS q
ON fixed_values.x = q.kswhatever