-2

我有一个名为“学生”的表,其中包含......

Student          Code1      Code2      Code3      Code4

Mark             0           1         1          1        1
Joseph           0           1         0          1        0
Bryan.           0           0         1          1        1

因此,SQL 查询应该选择有两个 1 以上的学生,并且它们也应该是连续的。在上述情况下......应该返回 Mark 和 Bryan,但不返回 Joseph

4

3 回答 3

1

另一种方法

SELECT Student
FROM 
(
  SELECT Student,(CONVERT(CHAR(1),Code1) + 
                  CONVERT(CHAR(1),Code2) + 
                  CONVERT(CHAR(1),Code3) + 
                  CONVERT(CHAR(1),Code4)) AS Codes
  FROM Students
) s1
WHERE CHARINDEX('11', Codes) > 0

这是sqlfiddle

于 2013-02-10T06:55:49.843 回答
0

如果只有少数列:

Select *
From Students
Where ( Code1 + Code2 ) = 2
    Or ( Code2 + Code3 ) = 2
    Or ( Code3 + Code4 ) = 2
    Or ( Code4 + Code5 ) = 2

另一种解决方案:

With NormalizedData As
    (
    Select Student, Code1, 1 As Sequence From Students Where Code1 = 1
    Union All Select Student, Code2, 2 From Students Where Code2 = 1
    Union All Select Student, Code3, 3 From Students Where Code3 = 1
    Union All Select Student, Code4, 4 From Students Where Code4 = 1
    Union All Select Student, Code5, 5 From Students Where Code5 = 1
    )
Select ...
From Students As S
Where Exists    (
                Select 1
                From NormalizedData As N1
                    Join NormalizedData As N2
                        On N2.Student = N1.Student
                            And N2.Sequence = N1.Sequence + 1
                Where N1.Student = S.Student
                )
于 2013-02-10T06:37:04.533 回答
0

虽然您的架构确实非常非常奇怪,但假设您使用的是 SQL Server 2005 或更高版本,您可以使用 UNPIVOT 轻松解决它。请注意,您必须将所有代码的硬编码列表提供给 unpivot ...

WITH pvt (student, code, value) as (
        SELECT 
            Student,
            ROW_NUMBER()OVER(Partition By Student Order By Codes) as CodeId, 
            Value
        FROM 
            ( select * from students ) P
        UNPIVOT ( 
            Value FOR Codes IN (Code1, Code2, Code3, Code4, Code5) 
        ) Code
    )

    select 
    distinct
        pvt.student
    from 
        pvt
        inner join pvt p2
            on p2.code = pvt.code + 1
            and p2.student = pvt.student
    where 
        pvt.value = 1 and p2.value = 1

这里真正的优势是您只需在一个地方处理各种代码 - 例如,如果您添加 Code6,您只需将其添加到 UNPIVOT 中,它将被合并到代码中。

另请注意,这只是解决了“学生在代码中必须有 2 个连续的 1”这一更一般的要求。它不检查学生有多少个 1,也不关心 2 值落在哪里。

最后一个警告是 ROW_NUMBER() 函数假设您的代码是按顺序命名的,没有间隙,所以如果您从 Code7 转到 Code9,它只会假设这两个是“连续的”。

于 2013-02-10T06:45:58.580 回答