Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. SELECT L.NOME AS Empresa,
  2. CONVERT(date, L.CREATEDAT, 111) AS DataCadastro,
  3. CONVERT(date, L.UpdatedAt, 111) AS DataAtualizacao,
  4. U.Nome PreVendedor,
  5.  
  6. CONVERT(date, (select DATEADD(HOUR,-3, max(hl1.CreatedAt)) from Historicolead hl1
  7. where hl1.FaseLeadId = (select top 1 flentr.id from faselead flentr
  8. where flentr.empresaclienteid = l.empresaclienteid and flentr.defase = 'Entrada')
  9. and l.id = hl1.LeadId), 111) DataFiltro1,
  10.  
  11. (SELECT top 1
  12. ISNULL(DATEDIFF(Mi,l.createdat,hl9.CreatedAt),0) from Historicolead hl9
  13. where hl9.leadid = l.id
  14. and hl9.faseleadid = (select top 1 flentr.id from faselead flentr
  15. where flentr.empresaclienteid = l.empresaclienteid and flentr.defase = 'Entrada')) as 'Intervalo_Cad_x_F1',
  16.  
  17. CONVERT(date, (select DATEADD(HOUR,-3, max(hl2.CreatedAt)) from Historicolead hl2
  18. where hl2.FaseLeadId = (select top 1 flfiltro1.id from faselead flfiltro1 where flfiltro1.empresaclienteid = l.empresaclienteid and flfiltro1.defase = 'Filtro 1')
  19. and l.id = hl2.LeadId), 111) DataFiltro2,
  20.  
  21. (SELECT top 1
  22. ISNULL(DATEDIFF(Mi,l.createdat,hl8.CreatedAt),0) from Historicolead hl8
  23. where hl8.leadid = l.id
  24. and hl8.faseleadid = (select top 1 flentr.id from faselead flentr
  25. where flentr.empresaclienteid = l.empresaclienteid and flentr.defase = 'Filtro 1')) as 'Intervalo_Cad_x_F2',
  26.  
  27.  
  28. CONVERT(date, (select DATEADD(HOUR,-3, max(b3.DtAgendamento)) from Historicobox b3 where b3.leadid = l.id), 111) DataAgendamento,
  29.  
  30. (SELECT top 1
  31. ISNULL(DATEDIFF(Mi,l.createdat,hb8.dtagendamento),0) from HistoricoBox hb8
  32. where hb8.leadid = l.id) as 'Intervalo_Cad_x_Agendamento',
  33.  
  34. CONVERT(date, (select DATEADD(HOUR,-3, max(b4.HrIni)) from Box b4 where l.id = b4.LeadId), 111) DataReuniao,
  35.  
  36. CONVERT(date, DATEADD(HOUR,0, ve.DtVenda), 111) DataVenda,
  37.  
  38. (SELECT top 1
  39. ISNULL(DATEDIFF(Mi,l.createdat,v8.dtvenda),0) from venda v8
  40. where v8.leadid = l.id) as 'Intervalo_Cad_x_Venda'
  41.  
  42. FROM LEAD L
  43.  
  44. JOIN Usuario U ON u.Id = l.PreVendedorId
  45. LEFT JOIN Venda ve ON ve.LeadId = l.Id
  46.  
  47. WHERE l.empresaclienteid=@EmpresaClienteId
  48. order by l.nome
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement