日期:2014-05-18 浏览次数:20514 次
WITH 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
SELECT * FROM CTE A WHERE EXISTS ( SELECT 1 FROM CTE WHERE Tel = A.Tel AND RowID <> A.RowID and Tel <> N'') order by Tel
------解决方案--------------------
SELECT * FROM CTE A WHERE A.Tel <>'' and EXISTS ( SELECT 1 FROM CTE WHERE Tel = A.Tel AND RowID <> A.RowID) order by Tel
------解决方案--------------------
看到2个大问题:
乱用子SQL语句
乱用CTE
------解决方案--------------------