Skip to content
Advertisement

Select distinct returns duplicates

I have the following query:

SELECT 
DISTINCT (TK.TICKETID),
TK.DESCRIPTION,
TK.CREATIONDATE,
TK.REPORTEDBY,
TK.OWNER,
WF.ASSIGNCODE
FROM ticket TK
INNER JOIN wfassignment WF on WF.OWNERID = TK.TICKETUID
WHERE TK.status not in ('ЧЕРНОВИК', 'ЗАКРЫТ', 'ВЫПОЛНЕН') AND WF.ASSIGNSTATUS not in ('COMPLETE', 'INACTIVE')
ORDER BY TK.TICKETID;

But it returns duplicates in TK.TICKETID attribute. And if i remove other attributes all ok. e.g.

TK.TICKETID TK.DESCRIPTION TK.CREATIONDATE  TK.REPORTEDBY  TK.OWNER     WF.ASSIGNCODE
О1013249    Право доступа   02.06.14        CHERNOVDK      SKACHKOVSV   NOVIKOVVA
О1013249    Право доступа   02.06.14        CHERNOVDK      SKACHKOVSV   PRITULADV
О1013249    Право доступа   02.06.14        CHERNOVDK      SKACHKOVSV   SVESHNIKOVAV

How can I return only a single record per TK.TICKETID?

Advertisement

Answer

Try this:

SELECT 
TK.TICKETID,
TK.DESCRIPTION,
TK.CREATIONDATE,
TK.REPORTEDBY,
TK.OWNER,
max(WF.ASSIGNCODE)
FROM ticket TK
INNER JOIN wfassignment WF on WF.OWNERID = TK.TICKETUID
WHERE TK.status not in ('ЧЕРНОВИК', 'ЗАКРЫТ', 'ВЫПОЛНЕН') AND WF.ASSIGNSTATUS not in ('COMPLETE', 'INACTIVE')
group by
TK.TICKETID,
TK.DESCRIPTION,
TK.CREATIONDATE,
TK.REPORTEDBY,
TK.OWNER
ORDER BY TK.TICKETID;
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement