Guest User

Untitled

a guest
Jun 18th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.88 KB | None | 0 0
  1. select *into #temp1 from (
  2. select NoteID, parentRef,CREATEDBY,Employee_Name, Supervisor, Office, CreatedDate, CreatedTime, Body, Headline,TypeCode, b.Name
  3. ,LEAD(PARENTREF) over (Partition by ParentRef order by CreatedDate, CreatedTime) as next_ParentRef
  4. ,LEAD(NoteID) over (Partition by ParentRef order by CreatedDate, CreatedTime) as next_NotedID
  5. ,LEAD(Employee_Name) over (Partition by ParentRef order by CreatedDate, CreatedTime) as next_EmployeeName
  6. ,LEAD(Supervisor) over (Partition by ParentRef order by CreatedDate, CreatedTime) as next_Supervisor
  7. ,LEAD(Office) over (Partition by ParentRef order by CreatedDate, CreatedTime) as next_Office
  8. ,LEAD(CREATEDDATE) over (Partition by ParentRef order by CreatedDate, CreatedTime) as next_CreatedDate
  9. ,LEAD(CREATEDTIME) over (Partition by ParentRef order by CreatedDate, CreatedTime) as next_CreatedTime
  10. ,LEAD(Body) over (Partition by ParentRef order by CreatedDate, CreatedTime) as next_Body
  11. ,LEAD(Headline) over (Partition by ParentRef order by CreatedDate, CreatedTime) as next_Headline
  12. ,LEAD(Typecode) over (Partition by ParentRef order by CreatedDate, CreatedTime) as next_Typecode
  13. ,LEAD(Name) over (Partition by ParentRef order by CreatedDate, CreatedTime) as next_Name
  14.  
  15. from Stg.Note n
  16. left join dbo.NoteType b on n.TYPECODE = b.ID
  17. left outer join dbo.Employee e On N.CREATEDBY = e.UserName
  18. 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())
  19. )b
  20. where (Name = 'File Review' and next_Name = 'File Review')
  21. and (TYPECODE ='1200005' and next_Typecode = '1200005')
  22. and ((HEADLINE like '%Follow up letter%')
  23. and (next_Headline like '%Follow up letter%'))
  24.  
  25. and convert(date, (convert(varchar(10), next_createddate))) > dateadd(day, 10, convert(date, convert(varchar(10), createddate)))
  26. and PARENTREF = '2002861'
  27.  
  28.  
  29.  
  30. --existing row
  31. select a.* into #temp2
  32. from #temp1 a
  33. join Stg.Note b on b.PARENTREF = a.PARENTREF and a.NoteID = b.NoteID
  34.  
  35.  
  36. ---- NextRow
  37. select stg.*, c.next_Name, c.next_EmployeeName, c.next_Supervisor, c.next_Office
  38. into #temp3
  39. from #temp1 c
  40. join Stg.Note stg on stg.PARENTREF = c.next_ParentRef and stg.NoteID = c.next_NotedID
  41.  
  42.  
  43. select curr.NoteID
  44. ,curr.PARENTREF
  45. ,dbo.ConvertIGDateTime(curr.CREATEDDATE,curr.CREATEDTIME) AS CreatedDateTime
  46. ,curr.Employee_Name
  47. ,curr.Supervisor
  48. ,curr.Office
  49. , replace (replace (cast(curr.BODY as nvarchar(MAX)), char(10), ''), char(13), '') as Body
  50. ,cast(curr.HEADLINE as nvarchar(Max)) as Headline
  51. ,curr.TYPECODE
  52. ,curr.Name
  53. from #temp2 curr
  54.  
  55. UNION
  56.  
  57. select lf.NoteID
  58. ,lf.PARENTREF
  59. ,dbo.ConvertIGDateTime(lf.CREATEDDATE,lf.CREATEDTIME) AS CreatedDateTime
  60. ,lf.next_EmployeeName
  61. ,lf.next_Supervisor
  62. ,lf.next_Office
  63. ,replace (replace (cast(lf.BODY as nvarchar(MAX)), char(10), ''), char(13), '') as Body
  64. ,cast(lf.HEADLINE as nvarchar(Max)) as Headline
  65. ,lf.TYPECODE
  66. ,lf.next_Name
  67. from #temp3 lf
  68.  
  69. order by PARENTREF, CreatedDateTime
  70.  
  71.  
  72. drop table #temp1
  73. drop table #temp2
  74. drop table #temp3
Add Comment
Please, Sign In to add comment