Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 如果组中的所有文档的值都设置为1,如何计数?_Sql_Sql Server - Fatal编程技术网

Sql 如果组中的所有文档的值都设置为1,如何计数?

Sql 如果组中的所有文档的值都设置为1,如何计数?,sql,sql-server,Sql,Sql Server,在文档审阅工具中,可以创建批文档。批次是一组相关文档,由GroupID标识 这些文档组将呈现给审阅者,审阅者将更新名为Testcompleted的字段。此字段有3种可能的状态:1、0或null。组中的文档数量各不相同 在下面的示例中,我有3组批文档。例如,第一批_0001有两个文档58和59 #Document ArtifactID Testcompleted GroupID -------------------------------------- 58

在文档审阅工具中,可以创建批文档。批次是一组相关文档,由GroupID标识

这些文档组将呈现给审阅者,审阅者将更新名为Testcompleted的字段。此字段有3种可能的状态:1、0或null。组中的文档数量各不相同

在下面的示例中,我有3组批文档。例如,第一批_0001有两个文档58和59

#Document

ArtifactID    Testcompleted    GroupID
--------------------------------------
58            1                4
59            1                4
60            null             6
61            1                6
62            null             7
63            null             7
64            null             7

#DocumentBatch

BatchArtifactID    DocumentArtifactID
-------------------------------------
66                 58
66                 59
67                 60
67                 61
68                 62
68                 63
68                 64


#Batch

ArtifactID    Name
------------------------
66            batch_0001
67            batch_0002
68            batch_0003
我需要知道批处理何时完成,也就是说,当该批处理中的所有文档都将Testcompleted字段设置为1时。在本例中,批次_0001就是这种情况

我想要的输出是:

batch           documents       reviewed     completed
------------------------------------------------------
batch_0001      2               2            yes
batch_0002      2               1            no
batch_0003      3               0            no
我从加入表格开始:

select
    *
from
    #Document d

    left join #DocumentBatch db
    on db.DocumentArtifactID = b.ArtifactID

    left join #Batch b
    on db.BatchArtifactID = b.ArtifactID
where
    d.Testcompleted = 1
;

这显然没有返回我需要的结果,但我被卡住了。非常感谢您对如何解决此问题的帮助。

您可以尝试以下方法:

select b.name
     , count(*) as documents
     , sum(d.Testcompleted) as reviewed
     , (case when count(*) = sum(d.Testcompleted) then 'yes' else 'no' end) as completed
from [#Document] d
join [#DocumentBatch] db on db.DocumentArtifactID = d.ArtifactID
join [#Batch] b on db.BatchArtifactID = b.ArtifactID
group by b.name
select b.name as batchname, count(d.ARtifactID) as numdocuments,
       sum(case when d.testCompleted = 1 then 1 else 0 end) as NumCompleted,
       (case when sum(case when d.testCompleted = 1 then 0 else 1 end) > 0
             then 'No'
             else 'Yes'
        end) as AllCompleted
from #Batch b left join
     #DocumentBatch db     
     on db.BatchArtifactID = b.ArtifactID
     #Document d left join
     on db.DocumentArtifactID = b.ArtifactID left join
group by b.name;

计数*包括计算所有值; Testcompleted仅统计Testcompleted为1的情况;
您可以尝试以下方法:

select b.name
     , count(*) as documents
     , sum(d.Testcompleted) as reviewed
     , (case when count(*) = sum(d.Testcompleted) then 'yes' else 'no' end) as completed
from [#Document] d
join [#DocumentBatch] db on db.DocumentArtifactID = d.ArtifactID
join [#Batch] b on db.BatchArtifactID = b.ArtifactID
group by b.name
select b.name as batchname, count(d.ARtifactID) as numdocuments,
       sum(case when d.testCompleted = 1 then 1 else 0 end) as NumCompleted,
       (case when sum(case when d.testCompleted = 1 then 0 else 1 end) > 0
             then 'No'
             else 'Yes'
        end) as AllCompleted
from #Batch b left join
     #DocumentBatch db     
     on db.BatchArtifactID = b.ArtifactID
     #Document d left join
     on db.DocumentArtifactID = b.ArtifactID left join
group by b.name;

计数*包括计算所有值; Testcompleted仅统计Testcompleted为1的情况;
您需要的是聚合,因此需要一个分组依据。大概是这样的:

select b.name
     , count(*) as documents
     , sum(d.Testcompleted) as reviewed
     , (case when count(*) = sum(d.Testcompleted) then 'yes' else 'no' end) as completed
from [#Document] d
join [#DocumentBatch] db on db.DocumentArtifactID = d.ArtifactID
join [#Batch] b on db.BatchArtifactID = b.ArtifactID
group by b.name
select b.name as batchname, count(d.ARtifactID) as numdocuments,
       sum(case when d.testCompleted = 1 then 1 else 0 end) as NumCompleted,
       (case when sum(case when d.testCompleted = 1 then 0 else 1 end) > 0
             then 'No'
             else 'Yes'
        end) as AllCompleted
from #Batch b left join
     #DocumentBatch db     
     on db.BatchArtifactID = b.ArtifactID
     #Document d left join
     on db.DocumentArtifactID = b.ArtifactID left join
group by b.name;

我认为不需要外部连接。您应该能够使用内部联接,除非有没有文档的批处理。如果您确实使用外部联接,那么从批处理开始比从文档开始更有意义,因为您是在批处理级别进行聚合。

您需要的是聚合,因此您需要一个分组依据。大概是这样的:

select b.name
     , count(*) as documents
     , sum(d.Testcompleted) as reviewed
     , (case when count(*) = sum(d.Testcompleted) then 'yes' else 'no' end) as completed
from [#Document] d
join [#DocumentBatch] db on db.DocumentArtifactID = d.ArtifactID
join [#Batch] b on db.BatchArtifactID = b.ArtifactID
group by b.name
select b.name as batchname, count(d.ARtifactID) as numdocuments,
       sum(case when d.testCompleted = 1 then 1 else 0 end) as NumCompleted,
       (case when sum(case when d.testCompleted = 1 then 0 else 1 end) > 0
             then 'No'
             else 'Yes'
        end) as AllCompleted
from #Batch b left join
     #DocumentBatch db     
     on db.BatchArtifactID = b.ArtifactID
     #Document d left join
     on db.DocumentArtifactID = b.ArtifactID left join
group by b.name;
select
    b.name, 
    count(db.DocumentArtifactID) as documents,
    -- count only completed
    count(case when d.Testcompleted = 1 then d.ArtifactID end) as reviewed,
    -- if the minimum = 1 there's no 0 or NULL
    case when min(cast(Testcompleted as tinyint)) = 1 then 'yes' else 'no' end as completed
from
    #Batch b

    left join #DocumentBatch db
    on db.BatchArtifactID = b.ArtifactID

    left join #Document d
    on db.DocumentArtifactID = d.ArtifactID
group by b.name;
我认为不需要外部连接。您应该能够使用内部联接,除非有没有文档的批处理。如果您确实使用外部联接,那么从批处理开始比从文档开始更有意义,因为您是在批处理级别进行聚合的

select
    b.name, 
    count(db.DocumentArtifactID) as documents,
    -- count only completed
    count(case when d.Testcompleted = 1 then d.ArtifactID end) as reviewed,
    -- if the minimum = 1 there's no 0 or NULL
    case when min(cast(Testcompleted as tinyint)) = 1 then 'yes' else 'no' end as completed
from
    #Batch b

    left join #DocumentBatch db
    on db.BatchArtifactID = b.ArtifactID

    left join #Document d
    on db.DocumentArtifactID = d.ArtifactID
group by b.name;
如果没有丢失的行,您可以切换到内部联接


如果没有丢失的行,您可以切换到内部联接…

那么,当一个集合的SUMTestCompleted等于同一集合的计数时?@thursdaysgeek Correct:为什么允许空值?那么,当一个集合的SUMTestCompleted等于同一集合的计数时?@thursdaysgeek Correct:你为什么允许空值?我不确定你是否想计算零。如果TestCompleted是零呢?您应该改用SUMd.Testcompleted。事实上-只有当Testcompleted的值=1时,文档才被视为已审阅。在本例中,0也被计数,这是一个错误的结论。我认为您不确定是否要计数0。如果TestCompleted为零怎么办?您应该改用SUMd.Testcompleted。事实上-只有当Testcompleted的值=1时,文档才被视为已审阅。在这种情况下,0也被计数,从而得出错误的结论。我得到一个错误:操作数数据类型位对于min运算符无效?@Pr0no:您没有说它是位,好吧,这是0/1的自然选择。您必须强制转换它,我将编辑我的答案。我得到一个错误:操作数数据类型位对于最小运算符无效?@Pr0no:您没有告诉我它是位,好吧,这是0/1的自然选择。你必须投下它,我会编辑我的答案。