Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- select *into #temp1 from (
- select NoteID, parentRef,CREATEDBY,Employee_Name, Supervisor, Office, CreatedDate, CreatedTime, Body, Headline,TypeCode, b.Name
- ,LEAD(PARENTREF) over (Partition by ParentRef order by CreatedDate, CreatedTime) as next_ParentRef
- ,LEAD(NoteID) over (Partition by ParentRef order by CreatedDate, CreatedTime) as next_NotedID
- ,LEAD(Employee_Name) over (Partition by ParentRef order by CreatedDate, CreatedTime) as next_EmployeeName
- ,LEAD(Supervisor) over (Partition by ParentRef order by CreatedDate, CreatedTime) as next_Supervisor
- ,LEAD(Office) over (Partition by ParentRef order by CreatedDate, CreatedTime) as next_Office
- ,LEAD(CREATEDDATE) over (Partition by ParentRef order by CreatedDate, CreatedTime) as next_CreatedDate
- ,LEAD(CREATEDTIME) over (Partition by ParentRef order by CreatedDate, CreatedTime) as next_CreatedTime
- ,LEAD(Body) over (Partition by ParentRef order by CreatedDate, CreatedTime) as next_Body
- ,LEAD(Headline) over (Partition by ParentRef order by CreatedDate, CreatedTime) as next_Headline
- ,LEAD(Typecode) over (Partition by ParentRef order by CreatedDate, CreatedTime) as next_Typecode
- ,LEAD(Name) over (Partition by ParentRef order by CreatedDate, CreatedTime) as next_Name
- from Stg.Note n
- left join dbo.NoteType b on n.TYPECODE = b.ID
- left outer join dbo.Employee e On N.CREATEDBY = e.UserName
- left outer join dbo.[Transfer] I On e.Employee_Name = I.AdjusterName and dbo.ConvertIGDateTime(n.CREATEDDATE,n.CREATEDTIME) between DateStart And ISNULL(Dateend,GETDATE())
- )b
- where (Name = 'File Review' and next_Name = 'File Review')
- and (TYPECODE ='1200005' and next_Typecode = '1200005')
- and ((HEADLINE like '%Follow up letter%')
- and (next_Headline like '%Follow up letter%'))
- and convert(date, (convert(varchar(10), next_createddate))) > dateadd(day, 10, convert(date, convert(varchar(10), createddate)))
- and PARENTREF = '2002861'
- --existing row
- select a.* into #temp2
- from #temp1 a
- join Stg.Note b on b.PARENTREF = a.PARENTREF and a.NoteID = b.NoteID
- ---- NextRow
- select stg.*, c.next_Name, c.next_EmployeeName, c.next_Supervisor, c.next_Office
- into #temp3
- from #temp1 c
- join Stg.Note stg on stg.PARENTREF = c.next_ParentRef and stg.NoteID = c.next_NotedID
- select curr.NoteID
- ,curr.PARENTREF
- ,dbo.ConvertIGDateTime(curr.CREATEDDATE,curr.CREATEDTIME) AS CreatedDateTime
- ,curr.Employee_Name
- ,curr.Supervisor
- ,curr.Office
- , replace (replace (cast(curr.BODY as nvarchar(MAX)), char(10), ''), char(13), '') as Body
- ,cast(curr.HEADLINE as nvarchar(Max)) as Headline
- ,curr.TYPECODE
- ,curr.Name
- from #temp2 curr
- UNION
- select lf.NoteID
- ,lf.PARENTREF
- ,dbo.ConvertIGDateTime(lf.CREATEDDATE,lf.CREATEDTIME) AS CreatedDateTime
- ,lf.next_EmployeeName
- ,lf.next_Supervisor
- ,lf.next_Office
- ,replace (replace (cast(lf.BODY as nvarchar(MAX)), char(10), ''), char(13), '') as Body
- ,cast(lf.HEADLINE as nvarchar(Max)) as Headline
- ,lf.TYPECODE
- ,lf.next_Name
- from #temp3 lf
- order by PARENTREF, CreatedDateTime
- drop table #temp1
- drop table #temp2
- drop table #temp3
Add Comment
Please, Sign In to add comment