2011-03-02 61 views
0

我有一个表格,具有以下列。SQL组通过问题

我需要找出那些具有相同jobcategoryrowid的2个ApplicantRowid的人,并且AssessmentTest应该至少有一行NULL与不同的Appstatusrowid。

结果应该看起来像下表所示。

Rowid ApplicantRowid JobCategoryRowid AssessmentTestRowid AppstatusRowid 
10770598 6952346  157    3      5 
11619676 6952346  157    NULL    6 
+2

我建议重新书写方式,也将这个问题帮助我们回答它。源表是什么样的?数据是什么样的?什么是列类型? – jonnii 2011-03-02 21:13:20

+1

..你已经尝试了什么? – GolezTrol 2011-03-02 21:14:21

+0

请求输出的样本输入数据是什么 – 2011-03-02 21:20:20

回答

0
select t.* 
from 
(
    select ApplicantRowid, JobCategoryRowid 
    from tbl 
    group by ApplicantRowid, JobCategoryRowid 
    having count(AssessmentTestRowid) < count(*) 
     and count(distinct AppstatusRowid) > 1 
) x 
inner join t on t.ApplicantRowid = x.ApplicantRowid 
      and t.JobCategoryRowid = x.JobCategoryRowid 

计数不包括NULL,所以count(AssessmentTestRowid) < count(*)确保至少有一个NULL

count(distinct AppstatusRowid) > 1确保有不同AppstatusRowids