I need to perform a query in SQL Server 2017, where I need to bring the following information: Codigo do chamado
, Data Abertura
, Interação
, Analista
, UltimaInteração
(would be the last interaction date) and Empresa
; I need this information only for calls that are open, I used the filter ( where
) in the sta_codigo = '1'
field, which means that the call is open, because the number 1 represents the open call code. However, when I try to do select
, it only brings all calls open, with all interactions, however I only want the last interaction that the call that it has open had.
Here's the select I've done:
select c.cha_codigo [Codigo do chamado], c.cha_dtabertura as [Data
Abertura],
Convert(varchar(5000),i.int_descricao) as [Interação], u.usu_nome
[Analista],
r.int_dtinteracao as UltimaInteração,
e.emp_fantasia as [Empresa]
from (select distinct max(int_dtinteracao) as int_dtinteracao from
interacoes) as r, interacoes as i
inner join chamados as c on c.cha_codigo = i.cha_codigo
inner join usuario as u on u.usu_cod = i.log_codigo
inner join empresa as e on e.emp_codigo = c.emp_codigo
where c.sta_codigo = '1'
group by i.cha_codigo, c.cha_codigo, c.cha_dtabertura,
Convert(varchar(5000), i.int_descricao), u.usu_nome, e.emp_fantasia,
r.int_dtinteracao
Order By c.cha_codigo desc