2014-04-08 55 views
0

每個患者應該有獨特的randomID這些randomID的應該是序列順序,所以我應該 帶回(複製RandomID)和(出序列RandomID的)複製和輸出序列號

這裏是我的查詢

declare @valueCheck table 
(RowNumber Int Identity 
,PatientID varchar(25) 
,RandomID varchar(25)) 

insert into @ValueCheck 
(PatientID, RandomID) 

select 
PatientID 
, RandomID 

From dbo.CurrentData 

where QuestionID= 200 
order by patientid, RandomID 

如果我申請這個條件,我會得到(亂序RandomID)

Select s1.PatientID, s1. RandomID as OutOFSequence 
From @ValueCheck s1 
Left Join @ValueCheck s2 
On s1.RandomID = s2.RandomID – 1 
Where s2.RandomID is Null 

,如果我申請這個,我會得到(複製蘭特奧米德)

​​

我想是的,我想這既帶來了條件一起

回答

1

你爲什麼不辦一個聯合查詢的表。

Select s1.PatientID, s1. RandomID, 'OutOFSequence' 
From @ValueCheck s1 
Left Join @ValueCheck s2 
On s1.RandomID = s2.RandomID – 1 
Where s2.RandomID is Null 
union all 
select PatientID, RandomID, 'Duplicate' from @ValueCheck 
where RandomID in 
(Select RandomID 
From @ValueCheck 
Group by RandomID 
Having Count(RandomID) > 1) 
+0

我不能使用UNION ALL,該錯誤消息指出:所有的查詢使用UNION合併,必須在其目標表達了相同數量的列出 – user3509096

+0

你的場數和類型應該在兩個聯合查詢匹配。我已更新查詢以反映此更改。 –