获取有多少组将列的所有值设置为null?

时间:2016-06-29 15:06:02

标签: sql-server sql-server-2008 tsql

我有一个包含四行的表@EmplTable。它是四名员工的名字。

然后我有一张表@MonitorTable,用于监控这些员工的工作。

假设没有任何内容发生在桌面上,@MonitorTable将有12行,对应于这四名员工将要工作的三周。对于所有内容,列IsProcessed都为空,这意味着没有分配任何内容。

我们假设IsProcessed的{​​{1}} 3 EmpId设为WeekNum,我如何确定WeekNum有多少组{4}空值?在这种情况下,结果将是2(WeekNum 2& 3),因为WeekNum 1有一行IsProcessed设置为true。

我包括脚本。任何帮助表示赞赏。

感谢。

declare @EmplTable table
(
    EmpId INT IDENTITY NOT NULL PRIMARY KEY,   
    Name varchar(20)
)

insert into @EmplTable (Name) select 'John'
insert into @EmplTable (Name) select 'Mary'
insert into @EmplTable (Name) select 'Ron'
insert into @EmplTable (Name) select 'Bob'

declare @count int
select @count = count(*) from @EmplTable

declare @MonitorTable table
(
    EmpId int,
    WeekNum int,
    IsProcessed bit
)

insert into @MonitorTable (EmpId, WeekNum) select 1, 1
insert into @MonitorTable (EmpId, WeekNum) select 2, 1
insert into @MonitorTable (EmpId, WeekNum) select 3, 1
insert into @MonitorTable (EmpId, WeekNum) select 4, 1
insert into @MonitorTable (EmpId, WeekNum) select 1, 2
insert into @MonitorTable (EmpId, WeekNum) select 2, 2
insert into @MonitorTable (EmpId, WeekNum) select 3, 2
insert into @MonitorTable (EmpId, WeekNum) select 4, 2
insert into @MonitorTable (EmpId, WeekNum) select 1, 3
insert into @MonitorTable (EmpId, WeekNum) select 2, 3
insert into @MonitorTable (EmpId, WeekNum) select 3, 3
insert into @MonitorTable (EmpId, WeekNum) select 4, 3

update @MonitorTable set IsProcessed = 1 where EmpId = 1 and WeekNum = 1

SELECT  WeekNum
FROM    @MonitorTable
GROUP BY WeekNum
HAVING  COUNT(CASE WHEN IsProcessed = 1 THEN 0
                   ELSE 1
              END) = @count

-- Should return 2, 3 since WeekNum has been updated. It returns 1, 2, 3.

2 个答案:

答案 0 :(得分:1)

这应该为您提供所需的2条记录

<div id="navigation">
<div id="nav">
<ul class="ulnav">
  <li class="navli"><a class="active" href="#home">Home</a></li>
  <li class="navli"><a href="#news">Link</a></li>
  <li class="dropdown navli">
    <a href="#" class="dropbtn">Dropdown</a>
    <div class="dropdown-content">
      <a href="#">Droplink 1</a>
      <a href="#">Droplink 2</a>
    </div>
  </li>
  <li class="dropdown navli">
    <a href="#" class="dropbtn">Dropdown 2</a>
    <div class="dropdown-content">
      <a href="#">Droplink 2.1</a>
      <a href="#">Droplink 2.2</a>
      <a href="#">Droplink 2.3</a>
    </div>
  </li>
  <li class="dropdown navli">
    <a href="#" class="dropbtn">Dropdown 3</a>
    <div class="dropdown-content">
      <a href="#">Droplink3.1</a>
	  <a href="#">Droplink3.2</a>
      <a href="#">Droplink3.3</a>
    </div>
  </li>
  <li class="navli"><a href="#news">Link 2</a></li>
</ul>
</div>
</div>

答案 1 :(得分:0)

declare @EmplTable table
(
    EmpId INT IDENTITY NOT NULL PRIMARY KEY,   
    Name varchar(20)
)

insert into @EmplTable (Name) select 'John'
insert into @EmplTable (Name) select 'Mary'
insert into @EmplTable (Name) select 'Ron'
insert into @EmplTable (Name) select 'Bob'

declare @MonitorTable table
(
    EmpId int,
    WeekNum int,
    IsProcessed bit
)

insert into @MonitorTable (EmpId, WeekNum) select 1, 1
insert into @MonitorTable (EmpId, WeekNum) select 2, 1
insert into @MonitorTable (EmpId, WeekNum, IsProcessed) select 3, 1, 1
insert into @MonitorTable (EmpId, WeekNum) select 4, 1
insert into @MonitorTable (EmpId, WeekNum) select 1, 2
insert into @MonitorTable (EmpId, WeekNum) select 2, 2
insert into @MonitorTable (EmpId, WeekNum) select 3, 2
insert into @MonitorTable (EmpId, WeekNum) select 4, 2
insert into @MonitorTable (EmpId, WeekNum) select 1, 3
insert into @MonitorTable (EmpId, WeekNum) select 2, 3
insert into @MonitorTable (EmpId, WeekNum) select 3, 3
insert into @MonitorTable (EmpId, WeekNum) select 4, 3

select * from @MonitorTable

if object_id('tempdb..#temp') is not null drop table #temp

select WeekNum as ct
into #temp
from  @MonitorTable
group by WeekNum
having sum(cast(isnull(IsProcessed,0) as int)) = 0


select count(ct) as ct from #temp