日期:2014-05-18 浏览次数:20474 次
declare @CompanyDate datetime ; set @CompanyDate = '2012-04-08' ; select GroupCode, ProductCode, ProjectCode, ProjectStartTime, ProjectEndTime, CreateTime, Id from MarketingCaseExpire_Bak where datediff(mm,CreateTime,@CompanyDate)=1 and exists ( select * from MarketingCaseExpire_Bak b where GroupCode=b.GroupCode and ProductCode = b.ProductCode and ProjectCode=b.ProjectCode and convert(nvarchar(10),ProjectStartTime,120)= convert(nvarchar(10),b.ProjectStartTime,120) and convert(nvarchar(10),ProjectEndTime,120)= convert(nvarchar(10),b.ProjectEndTime,120) and convert(nvarchar(7),b.CreateTime,120)=convert(nvarchar(7),@CompanyDate,120) and id<>b.id )
declare @CompanyDate datetime ; set @CompanyDate = '2012-04-08' ; select GroupCode, ProductCode, ProjectCode, ProjectStartTime, ProjectEndTime, CreateTime, Id from MarketingCaseExpire_Bak b where datediff(mm,CreateTime,@CompanyDate)=1 and exists ( select * from MarketingCaseExpire_Bak where GroupCode=b.GroupCode and ProductCode = b.ProductCode and ProjectCode=b.ProjectCode and convert(nvarchar(10),ProjectStartTime,120)= convert(nvarchar(10),b.ProjectStartTime,120) and convert(nvarchar(10),ProjectEndTime,120)= convert(nvarchar(10),b.ProjectEndTime,120) and convert(nvarchar(7),b.CreateTime,120)=convert(nvarchar(7),@CompanyDate,120) and id<>b.id )
------解决方案--------------------
代码改成这样试试:
declare @CompanyDate datetime ; set @CompanyDate = '2012-04-08' ; select a.GroupCode, a.ProductCode, a.ProjectCode, a.ProjectStartTime, a.ProjectEndTime, a.CreateTime, a.Id from MarketingCaseExpire_Bak a inner join MarketingCaseExpire_Bak b on a.GroupCode=b.GroupCode and a.ProductCode = b.ProductCode and a.ProjectCode=b.ProjectCode and convert(nvarchar(10),a.ProjectStartTime,120)= convert(nvarchar(10),b.ProjectStartTime,120) and convert(nvarchar(10),a.ProjectEndTime,120)= convert(nvarchar(10),b.ProjectEndTime,120) and convert(nvarchar(7),b.CreateTime,120)=convert(nvarchar(7),@CompanyDate,120) and a.id<>b.id where datediff(mm,CreateTime,@CompanyDate)=1