/*由于对你的表结构不是太清楚,但是明白你的意思,所以我还是把思路给出来好了分3步: 第一步:得到第一次的回访时间,放在临时表#T0 */ select * into #T0 from CallBack ta where not exists(select 1 from CallBacktime tb where ta.InComeId=tb.InComeId and ta.CallBackTime<tb.CallBackTime) --第二步:得到第一次的回访时间,放在临时表#T1 select * into #T1 from CallBack ta,#T0 where ta.主键=#T0.主键 and not exists(select 1 from CallBacktime tb where ta.InComeId=tb.InComeId and ta.CallBackTime<tb.CallBackTime) --第三步:合并 select #T0.客户名字 #T0.CallBackTime as '第一次投诉时间',#T1.CallBackTime as '第一次投诉时间' from #T0,#T1 where #T0.主键=#T1.主键 --删除临时表 drop table #T0,#T1 --分开3步主要是为了思路清晰,其实还可以再精简些,不知LZ怎么看:)
/*由于对你的表结构不是太清楚,但是明白你的意思,所以我还是把思路给出来好了分3步: 第一步:得到第一次的回访时间,放在临时表#T0 */ select * into #T0 from CallBack ta where not exists(select 1 from CallBacktime tb where ta.InComeId=tb.InComeId and ta.CallBackTime<tb.CallBackTime) --第二步:得到第一次的回访时间,放在临时表#T1 select * into #T1 from CallBack ta,#T0 where ta.主键=#T0.主键 and not exists(select 1 from CallBacktime tb where ta.InComeId=tb.InComeId and ta.CallBackTime<tb.CallBackTime) --第三步:合并 select #T0.客户名字 #T0.CallBackTime as '第一次投诉时间',#T1.CallBackTime as '第二次投诉时间' from #T0,#T1 where #T0.主键=#T1.主键 --删除临时表 drop table #T0,#T1 --分开3步主要是为了思路清晰,其实还可以再精简些,不知LZ怎么看:)
第二步应该是select * into #T1 from CallBack ta,#T0 where ta.主键<>#T0.主键 and not exists(select 1 from CallBacktime tb where ta.InComeId=tb.InComeId and ta.CallBackTime<tb.CallBackTime
都可以
/*由于对你的表结构不是太清楚,但是明白你的意思,所以我还是把思路给出来好了分3步:
第一步:得到第一次的回访时间,放在临时表#T0
*/
select * into #T0
from CallBack ta
where not exists(select 1 from CallBacktime tb where ta.InComeId=tb.InComeId and ta.CallBackTime<tb.CallBackTime)
--第二步:得到第一次的回访时间,放在临时表#T1
select * into #T1
from CallBack ta,#T0
where ta.主键=#T0.主键
and not exists(select 1 from CallBacktime tb where ta.InComeId=tb.InComeId and ta.CallBackTime<tb.CallBackTime)
--第三步:合并
select #T0.客户名字 #T0.CallBackTime as '第一次投诉时间',#T1.CallBackTime as '第一次投诉时间'
from #T0,#T1
where #T0.主键=#T1.主键
--删除临时表
drop table #T0,#T1
--分开3步主要是为了思路清晰,其实还可以再精简些,不知LZ怎么看:)
第一步:得到第一次的回访时间,放在临时表#T0
*/
select * into #T0
from CallBack ta
where not exists(select 1 from CallBacktime tb where ta.InComeId=tb.InComeId and ta.CallBackTime<tb.CallBackTime)
--第二步:得到第一次的回访时间,放在临时表#T1
select * into #T1
from CallBack ta,#T0
where ta.主键=#T0.主键
and not exists(select 1 from CallBacktime tb where ta.InComeId=tb.InComeId and ta.CallBackTime<tb.CallBackTime)
--第三步:合并
select #T0.客户名字 #T0.CallBackTime as '第一次投诉时间',#T1.CallBackTime as '第二次投诉时间'
from #T0,#T1
where #T0.主键=#T1.主键
--删除临时表
drop table #T0,#T1
--分开3步主要是为了思路清晰,其实还可以再精简些,不知LZ怎么看:)
from CallBack ta,#T0
where ta.主键<>#T0.主键
and not exists(select 1 from CallBacktime tb where ta.InComeId=tb.InComeId and ta.CallBackTime<tb.CallBackTime