如何使用“是”或“否”条目显示唯一的数据。

时间:2011-09-27 07:43:15

标签: sql-server sql-server-2005

这是我的数据

unitCode stunum assNo   subStatus 
SIT111  1000    1   Yes
SIT111  1000    2   No
SIT111  3000    1   No

如何仅使用“否”

生成结果

例如:只有学生3000应该出现。因为那个人还没有递交任何作业。 ?

2 个答案:

答案 0 :(得分:3)

您可以使用以下声明:

select
    *
from
(
    select
        unitCode,
        stunum,
        sum(case when subStatus = 'Yes' then 1 else 0 end) as CountYes,
        sum(case when subStatus = 'No' then 1 else 0 end) as CountNo
    from
        students
    group by
        unitCode, stunum
) as student
inner join student_details
    on student.stunum = student_details.stunum
where
    CountNo > 0 and CountYes = 0;

答案 1 :(得分:0)

declare @T table
(
  unitCode varchar(10),
  stunum int,
  assNo int,
  subStatus varchar(3)
)  

insert into @T values
('SIT111',  1000,    1,   'Yes'),
('SIT111',  1000,    2,   'No'),
('SIT111',  3000,    1,   'No')

select *
from @T as T
where T.subStatus = 'No' and
      T.stunum not in (select stunum
                       from @T
                       where subStatus = 'Yes')