选择相对于sql中其他列值多出50%的列值

时间:2017-03-24 19:44:55

标签: c# sql sql-server

我需要编写一个SQL查询(SQL Server)。

我有一张看起来像这样的桌子。 表名:类

np.mean (with the axis parameter)
np.std  (with the axis parameter)
np.roll (again with the axis parameter)
np.mgrid
np.max  (again with axis parameter)

依旧......

我想显示包含所有列的表格,但是对于主题列,找到学生姓名的出现率为50%或更高

例如,如果学生姓名 - '詹姆斯'对于2名4名受试者,则应显示他的名字,并且应排除其他符合50%标准的人。

由于

2 个答案:

答案 0 :(得分:4)

使用......

select Student
from Class
group by Student
having count(Student) >= (select count(distinct [Subject]) from Class) / 2.0

或者如果你喜欢变量......

declare @numOfSubjects int = (select count(distinct [Subject]) from Class)

select Student
from Class
group by Student
having count(Student) >= @numOfSubjects / 2.0

要将所有列都带回CTE

with cte as(
    select Student
    from Class
    group by Student
    having count(Student) >= (select count(distinct [Subject]) from Class)) / 2.0)

select 
    c.*
from Class c
inner join cte on cte.Student = c.Student

答案 1 :(得分:1)

只需获取所有列......

/