0
I’m trying to make a query
that groups all the results in one line, that is, that does not bring duplicated results. I’ve used the distinct
and the group by
but one of the two has returned what I need.
My consultation is as follows::
SELECT TICKET, DATAHORA, TIPO, ESTADO, CRITICIDADE, LOGIN, PARECER, INFOPEND, PROP, DATAPROP, PREV, SRD, PROT, REAB, PRIENC
FROM (
SELECT DISTINCT TC.TN TICKET, TC.CREATE_TIME DATAHORA, TT.NAME TIPO, TS.NAME ESTADO, TP.NAME CRITICIDADE, US.LOGIN LOGIN,
CASE
WHEN TH.NAME LIKE '%PARECER%' THEN RIGHT(TH.NAME, POSITION('%' IN REVERSE(TH.NAME))-1)
ELSE ''
END PARECER,
CASE
WHEN TH.NAME LIKE '%INFORMACOESPENDENTES%' THEN RIGHT(TH.NAME, POSITION('%' IN REVERSE(TH.NAME))-1)
ELSE ''
END INFOPEND,
CASE
WHEN AR.A_SUBJECT LIKE '%ATUALIZAÇÃO PROPRIETÁRIO%' THEN LEFT(AR.A_FROM, POSITION('<' IN (AR.A_FROM))-1)
ELSE ''
END PROP,
CASE
WHEN AR.A_SUBJECT LIKE '%ATUALIZAÇÃO PROPRIETÁRIO%' THEN (AR.CREATE_TIME)
ELSE ''
END DATAPROP,
CASE
WHEN TH.NAME LIKE '%PREVISAOATENDIMENTO%' THEN RIGHT(TH.NAME, POSITION('%' IN REVERSE(TH.NAME))-1)
ELSE ''
END PREV,
CASE
WHEN TH.NAME LIKE '%SRD%' THEN RIGHT(TH.NAME, POSITION('%' IN REVERSE(TH.NAME))-1)
ELSE ''
END SRD,
CASE
WHEN TH.NAME LIKE '%PROTOCOLO%' THEN RIGHT(TH.NAME, POSITION('%' IN REVERSE(TH.NAME))-1)
ELSE ''
END PROT,
CASE
WHEN TS.NAME LIKE '%REABERTO' THEN (TS.NAME)
ELSE ''
END REAB,
CASE
WHEN AR.A_SUBJECT LIKE 'FECHAR' THEN AR.CREATE_TIME
ELSE ''
END PRIENC
FROM OTRS2.TICKET TC
INNER JOIN OTRS2.TICKET_TYPE TT ON TC.TYPE_ID = TT.ID
INNER JOIN OTRS2.TICKET_STATE TS ON TC.TICKET_STATE_ID = TS.ID
INNER JOIN OTRS2.TICKET_PRIORITY TP ON TC.TICKET_PRIORITY_ID=TP.ID
INNER JOIN OTRS2.USERS US ON TC.CREATE_BY=US.ID
INNER JOIN OTRS2.TICKET_HISTORY TH ON TC.ID=TH.TICKET_ID
INNER JOIN OTRS2.ARTICLE AR ON TC.ID=AR.TICKET_ID
)A
This query is generating the following result:
However, I need the ticket number not to be repeated, that is, in a row it aggregates all the columns.
If I use the group by ticket
happens the following:
With, the group by ticket
, it returns only one row, but the column information (like for example the column seems) goes away!
How can I put all information on one line?
Thank you
If it has no aggregation function, I don’t see why to use group by, I think it’s better to be distinct. after that, you would have to see this lot of case, because it seems to me unnecessary... for a better analysis, you have to provide the structure of the tables and some sample data. It would be very useful to put in Sqlfiddle
– Rovann Linhalis
If you want to group as it is in the second figure, you need to include all columns to group:
group by ticket, datahora, tipo, estado, criticidade, login,....
If any of them make a difference, it’ll bring more than one line. See that "look" has the empty value and "kkkkkk", it will generate two rows, and so on with the other columns– Ricardo Pontual