我有 3 个表 Box、Documemts、Batches。每个盒子包含超过 1 个文档,每个文档有多个批次。我想选择文档中至少有 1 个批次的值为 IsDeleted = 0 的文件夹中的文档计数。
让我们假设文档有 10 个批次,如果至少有一个批次的值 isdeleted = 0,我们将计算它,否则如果所有批次的 isdeleted = 1,我们将不计算它。
如何根据上述情况选择编号文件?
我试过了,但它不起作用
SELECT b.ID as BOXID,d.ID as DocID, count(1) as CountDoc
FROM Documents as d
INNER JOIN dbo.Boxes as b on d.boxid = b.id
INNER JOIN dbo.Batches as t on d.ID = t.DocumentID
WHERE d.ID = t.DocumentID
AND d.boxid= b.id
AND t.isDeleted = 0
GROUP BY d.ID , t.DocumentID ,b.ID --HAVING t.isDeleted = 0