如何在这里使用 group by query

How to use group by query here

Id  BatchName      TblName           SubBatchName     Status
1   12032014    Tbl_12032014_raw2_1  raw2           Complete    
2   12032014    Tbl_12032014_raw2_2  raw2           Complete    
3   12182014    Tbl_12182014_raw3_1  raw3           Complete        
4   12182014    Tbl_12182014_raw3_2  raw3           Complete        
5   12182014    Tbl_12182014_raw4_1  raw4           Complete        
6   12182014    Tbl_12182014_raw4_2  raw4           Complete        
7   12062014    Tbl_12062014_raw5_1  raw5           Complete    
8   12062014    Tbl_12062014_raw5_2  raw5           Complete
9   12062014    Tbl_12062014_raw5_3  raw5           InComplete  

我想要如下输出

BatchName                                                                                 
--------------------
12032014  
12182014                                            

如果 batchname 的所有行都具有状态 complete 然后只有 BatchName Get select

谢谢

按批次分组,只处理除 Complete

之外没有其他状态的那些
select batchname
from your_table
group by batchname
having sum(case when status <> 'Complete' or status is null
                then 1 
                else 0 
           end) = 0

所以你想要所有 BatchName,其中所有状态都是 Complete

您也可以使用 NOT EXISTS:

而不是 Group By
SELECT DISTINCT t1.BatchName 
FROM dbo.TableName t1
WHERE t1.Status = 'Complete'
AND NOT EXISTS( SELECT 1 FROM dbo.TableName t2
                WHERE  t1.BatchName = t2.BatchName 
                 AND   ISNULL(t2.Status,'') <> 'Complete')

Demo