Sql 不同行上的多个条件

Sql 不同行上的多个条件,sql,sql-server,group-by,where-clause,having-clause,Sql,Sql Server,Group By,Where Clause,Having Clause,但此查询不返回任何记录 我想要这个结果 select * from mytable where (course='Math' and score>10) and (course='Lab' and score>11) 年龄|姓名 ____________ 10 |詹姆斯 其中条件(数学>=10和实验室>11)是动态生成的,可能有2个条件或100个或更多 请帮助我您查询的是同时满足这两个条件的记录-这不可能发生,因为每个记录都有一个课程 您需要一个适用于具有相同名称的行的条件,因

但此查询不返回任何记录

我想要这个结果

select * from mytable
where (course='Math' and score>10) and  (course='Lab' and score>11) 
年龄|姓名 ____________ 10 |詹姆斯 其中条件(数学>=10和实验室>11)是动态生成的,可能有2个条件或100个或更多


请帮助我

您查询的是同时满足这两个条件的记录-这不可能发生,因为每个记录都有一个课程

您需要一个适用于具有相同名称的行的条件,因此建议改为聚合:

age | name ____________ 10 |James
您可以查询同时满足这两个条件的记录—这是不可能发生的,因为每个记录都有一个课程

您需要一个适用于具有相同名称的行的条件,因此建议改为聚合:

age | name ____________ 10 |James
如果您将问题表述为:

  • 选择所有唯一的(姓名、年龄)组合
  • 有一行分数>=10的课程数学
  • 这是课程实验室的一行,分数>11
  • 然后,您可以将其转换为SQL中非常类似的内容:

    select age, name 
    from mytable
    where course in ('Math', 'Lab')
    group by age, name 
    having
        max(case when course = 'Math' then score end) > 10
        and max(case when course = 'Lab' then score end) > 11
    

    如果您将问题表述为:

  • 选择所有唯一的(姓名、年龄)组合
  • 有一行分数>=10的课程数学
  • 这是课程实验室的一行,分数>11
  • 然后,您可以将其转换为SQL中非常类似的内容:

    select age, name 
    from mytable
    where course in ('Math', 'Lab')
    group by age, name 
    having
        max(case when course = 'Math' then score end) > 10
        and max(case when course = 'Lab' then score end) > 11
    

    我认为您的数据或您的条件不适合获取输出。尽管基于您的条件,您可以单独使用您的条件,然后使用“从两个选择中相交”并获取过滤后的数据。就像下面的代码

    select distinct t1.age, t1.name            -- unique combinations
    from mytable t1
    where exists ( select top 1 'x'            -- with a row math score >= 10
                   from mytable t2
                   where t2.name = t1.name
                     and t2.age = t1.age
                     and t2.course = 'math'
                     and t2.score >= 10 )
      and exists ( select top 1 'x'            -- with a row lab score > 11
                   from mytable t3
                   where t3.name = t1.name
                     and t3.age = t1.age
                     and t3.course = 'lab'
                     and t3.score > 11 );
    

    我认为您的数据或您的条件不适合获取输出。尽管基于您的条件,您可以单独使用您的条件,然后使用“从两个选择中相交”并获取过滤后的数据。就像下面的代码

    select distinct t1.age, t1.name            -- unique combinations
    from mytable t1
    where exists ( select top 1 'x'            -- with a row math score >= 10
                   from mytable t2
                   where t2.name = t1.name
                     and t2.age = t1.age
                     and t2.course = 'math'
                     and t2.score >= 10 )
      and exists ( select top 1 'x'            -- with a row lab score > 11
                   from mytable t3
                   where t3.name = t1.name
                     and t3.age = t1.age
                     and t3.course = 'lab'
                     and t3.score > 11 );
    

    可以使用相关子查询编写查询

    select Age,Name
    from Table_1
    where Course ='Math' and Score>=10
    INTERSECT
    select Age,Name
    from Table_1
    where Course ='Lab' and Score>11
    

    可以使用相关子查询编写查询

    select Age,Name
    from Table_1
    where Course ='Math' and Score>=10
    INTERSECT
    select Age,Name
    from Table_1
    where Course ='Lab' and Score>11
    

    如果需要名称,请使用聚合和having子句:

    select * from table_1 t1
        where score >11 and course ='lab' 
            and [name] in (select [name] from table_1 t2 where t1.[name] =t2.[name] and t1.age =t2.Age
            and t2.Score >=10 and course = 'Math')
    
    如果需要详细记录,请使用窗口功能:

    select name, age
    from mytable
    where (course = 'Math' and score > 10) or
          (course = 'Lab' and score > 11) 
    group by name, age
    having count(distinct course) = 2;
    

    SQL Server不支持将
    count(distinct)
    作为窗口函数。但是您可以通过两次使用
    densite\u rank()
    来实现它。

    如果需要名称,请使用聚合和having子句:

    select * from table_1 t1
        where score >11 and course ='lab' 
            and [name] in (select [name] from table_1 t2 where t1.[name] =t2.[name] and t1.age =t2.Age
            and t2.Score >=10 and course = 'Math')
    
    如果需要详细记录,请使用窗口功能:

    select name, age
    from mytable
    where (course = 'Math' and score > 10) or
          (course = 'Lab' and score > 11) 
    group by name, age
    having count(distinct course) = 2;
    
    SQL Server不支持将
    count(distinct)
    作为窗口函数。但是您可以通过使用两次
    densite\u rank()
    来实现它