如何从表中获取具有相同值的同一表的2行的列名?

时间:2014-12-24 08:52:58

标签: c# sql-server-2005

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行的状态都已完成,那么只有BatchName获取选择

由于

2 个答案:

答案 0 :(得分:1)

试试这个: -

SELECT BatchName, SubBatchName
FROM [Table]
WHERE Status = 'Complete' 
GROUP BY BatchName,SubBatchName
HAVING COUNT(SubBatchName) > 1

答案 1 :(得分:0)

select BatchName,SubBatchname from tablename where BatchName='12032014' and Status='Complete' group by BatchName,SubBatchName;

Group By