SQL codeWITH CTE AS (
select RowID = ROW_NUMBER() OVER (ORDER BY (case when Time is null then TimeC else Time end) ASC,NEWID()),*
from(
select * from
(
select * from (select * from Orders) a
full join (select * from PatientCome) b
on a.GUID = b.ComeGUID
) c
where (c.ProjectID=16 or RealProjectID=16)
and
(
(convert(varchar(10),Time,120) >= '2010-01-01' and convert(varchar(10),Time,120) <='2015-02-02')
or (convert(varchar(10),TimeC,120) >= '2010-01-01' and convert(varchar(10),TimeC,120) <='2015-02-02')
)
) d
)
SELECT * FROM CTE A
WHERE EXISTS (
SELECT 1 FROM CTE WHERE Tel = A.Tel AND RowID <> A.RowID) order by Tel