1
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 date of the last interaction) and Empresa
; need this information only of the calls that are open, I used the filter(where
) in the field sta_codigo = '1'
which means that the call is open, because the number 1 represents the open call code. However, when I try to realize the select
, it brings only all open calls, with all interactions, but I just want the last interaction that the call that is open had.
Follow the select I made:
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
You will use the
MAX
. See more here Operators Max, Min, Count, Update and Delete with Subqueries - Union and Subquery– rbz
Besides what @Rbz commented, who reads in your question "Using the open call filter" you have no idea what this is. . you commented something you know, but for us who are reading it does not make sense, you would need to explain better and clearly put the table structure. Anyway, this you solve in the
where
, and to bring only the one value, in the case "the last", you can usemax
– Ricardo Pontual
Ola Ricardo, I’m sorry for not explaining it right, I’m already tidying up.
– João Pedro Fernandes
Ola Rbz, I have already performed the MAX in Where, however it brings only 1 record, I need the select bring all open calls, represented by c.sta_code = '1' where 1 is the code of the calls that are open, and of those open calls, bring only the last interaction, because the calls can have 'n' interactions, but I just want the last.
– João Pedro Fernandes