我有9列的教师表
TeacherID, TeacherName, Reception, Year1, Year2, Year3, Year4, Year5, ALevel
我需要将最小和最大位值(接收 - 年份5)连接到1,然后将其显示在select语句中,使其输出为(在本例中为年份1,2和3):Y1 - Y3
SELECT
TeacherID, TeacherName,
Reception + Year1 + Year2 + Year3 + Year4 + Year5 + ALevel as 'KeyStage'
FROM Teachers
关于如何做到这一点的任何想法?
非常感谢,马特
答案 0 :(得分:3)
试试这个; First Case
从Reception
开始, MinYear
和 Second Case
从{{1开始对于ALevel
。同时为每个案例添加MaxYear
,以避免在没有任何案例为1
else
答案 1 :(得分:2)
SELECT
TeacherID,
TeacherName,
CASE
WHEN Reception > 0 THEN 'Re'
WHEN Year1 > 0 THEN 'Y1'
WHEN Year2 > 0 THEN 'Y2'
WHEN Year3 > 0 THEN 'Y3'
WHEN Year4 > 0 THEN 'Y4'
WHEN Year5 > 0 THEN 'Y5'
WHEN ALevel > 0 THEN 'AL'
ELSE '' END
+ ' - ' +
CASE
WHEN ALevel > 0 THEN 'AL'
WHEN Year5 > 0 THEN 'Y5'
WHEN Year4 > 0 THEN 'Y4'
WHEN Year3 > 0 THEN 'Y3'
WHEN Year2 > 0 THEN 'Y2'
WHEN Year1 > 0 THEN 'Y1'
WHEN Reception > 0 THEN 'Re'
ELSE '' END AS Range
FROM
Teachers
答案 2 :(得分:0)
我不会。在您的客户端代码(php,c#whatever)中执行此操作。您的规格也不清楚,如果选中年份1,3和5,而2和4不是?
答案 3 :(得分:0)
这应该对你有用,我没有测试过,你可能需要做一些改变。请注意,如果没有检查位,那么您将获得一个空值。我会使用null,因为如果没有检查,那么你没有这个奇怪的短划线,你可以使用isnull将值更改为你想要的没有选择的年份。
SELECT TeacherID,
TeacherName,
case when Reception = 1 then 'R'
when Year1 = 1 then 'Y1'
when Year2 = 1 then 'Y2'
when Year3 = 1 then 'Y3'
when Year4 = 1 then 'Y4'
when Year5 = 1 then 'Y5'
when ALevel = 1 then 'AL'
else null end + ' - ' +
case when Reception = 1 then 'R'
when Year1 = 1 then 'Y1'
when Year2 = 1 then 'Y2'
when Year3 = 1 then 'Y3'
when Year4 = 1 then 'Y4'
when Year5 = 1 then 'Y5'
when ALevel = 1 then 'AL'
else null end as 'KeyStage'
FROM Teachers