纵向转横向,取前4次
功能描述:要从理赔表中取出每个人的前四次理赔的出行时间,并找出一个月内理赔了4次的。
select *,case when DATEDIFF(DAY,c1,c4)<=30 then 1 else 0 end t from (select Name,Pid,Max(case when aa.i=1 then aa.DepPlanTime else '' end) c1, Max(case when i=2 then DepPlanTime else '' end) c2, Max(case when i=3 then DepPlanTime else '' end) c3, Max(case when i=4 then DepPlanTime else '' end) c4 from( select Name,Pid,(row_number() over(partition by Pid order by DepPlanTime)) i,DepPlanTime from Inf_CmbCaim where pid in (select Pid from Inf_CmbCaim where ClaimStatus='已理赔' group by Pid having count(1)>=4) ) aa group by aa.Name,aa.Pid) bb