0

我有 9 列的教师表

TeacherID, TeacherName, Reception, Year1, Year2, Year3, Year4, Year5, ALevel

我需要将最小和最大位值(Reception - Year5)与 1 连接,然后将其显示在 select 语句中,使其输出为(在本例中,勾选第 1、2 和 3 年):Y1 - Y3。

SELECT 
    TeacherID, TeacherName, 
    Reception + Year1 + Year2 + Year3 + Year4 + Year5 + ALevel as 'KeyStage' 
FROM Teachers

关于如何做到这一点的任何想法?

非常感谢,马特

4

4 回答 4

3

尝试这个; First CaseReception for 开始,从MinYearfor开始。如果它们都不是 1,还要为每种情况添加以避免出现 nullSecond CaseALevelMaxYearelse

SELECT TeacherID, TeacherName, Reception,
       Case when Reception = 1 then 'R'
            when Year1 = 1 then 'Y1'
            ...
            when ALevel = 1 then 'AL'
            else ''
       End + ' - '
       Case when ALevel = 1 then 'AL'
            when Year5 = 1 then 'Y5'
            ...
            when Reception= 1 then 'R'
            else '' 
       End As KeyStage  
FROM Teachers
于 2013-02-13T14:41:55.270 回答
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
于 2013-02-13T14:44:45.460 回答
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
于 2013-02-13T14:55:10.380 回答
0

我不会。在您的客户端代码(php、c# 等)中执行此操作。您的规格也不清楚,如果勾选了第 1、3 和 5 年而没有勾选第 2 和第 4 年怎么办?

于 2013-02-13T14:43:06.637 回答