---------------求SQL语句!!!!!!!!!!!!-----------------
表A:
AccountID CandidateID FieldID RowID JobID RKAnsID
----------- ----------- ----------- ----------- ----------- -----------
12100144 100616 1042 1 0 5
12100144 100616 1042 2 0 4
12100144 100616 1042 3 0 3
12100144 100617 1042 1 0 5
12100144 100617 1042 2 0 4
我要得到这样一个CanidateID,
这个CandidateID必须要符合,
RowID=1 and RKAnsID=5
RowID=2 and RKAnsID=4
RowID=3 and RKAnsID=3
也就是结果应该是 100616
请问该怎么写
------解决方案--------------------declare @ta table(AccountID int, CandidateID int, FieldID int, RowID int, JobID int, RKAnsID int)
insert @ta select 12100144, 100616, 1042, 1, 0, 5
insert @ta select 12100144, 100616, 1042, 2, 0, 4
insert @ta select 12100144, 100616, 1042, 3, 0, 3
insert @ta select 12100144, 100617, 1042, 1, 0, 5
insert @ta select 12100144, 100617, 1042, 2, 0, 4
select *
from @ta ta
where
exists
(select count(distinct rtrim(RowID)+ '- '+rtrim(RKAnsID)) as num
from @ta where CandidateID=ta.CandidateID and
((RowID=1 and RKAnsID=5)or(RowID=2 and RKAnsID=4)or(RowID=3 and RKAnsID=3))
having count(1)=3)
(1 行受影响)
(1 行受影响)
(1 行受影响)
(1 行受影响)
(1 行受影响)
AccountID CandidateID FieldID RowID JobID RKAnsID
----------- ----------- ----------- ----------- ----------- -----------
12100144 100616 1042 1 0 5
12100144 100616 1042 2 0 4
12100144 100616 1042 3 0 3
(3 行受影响)