Search code examples
oracle-databaseplsqlplsqldeveloper

Do not show duplicate values ​in a cursor PL/SQL


Hi at this moment I am returning these values

--Group: Junior -- Name: Jared Ramirez -- Id Agent: 1
--Group: Junior -- Name: Jared Ramirez -- Id Agent: 1
--Group: Junior -- Name: Lilah Bailey -- Id Agent: 26
--Group: Junior -- NoNamebre: Lilah Bailey -- Id Agent: 26
--Group: Junior -- Name: Lilah Bailey -- Id Agent: 26

But I want to return only 1 for each agent number for example

--Grupo: Junior -- Nombre: Jared Ramirez -- Id Agente: 1
--Grupo: Junior -- Nombre: Lilah Bailey -- Id Agente: 26
DECLARE 
idCalls CALLS.CALL_ENTRY_ID%type;
idAgenteLlamada CALLS.ID_AGENT%type;

CURSOR c_Call (idCalls CALLS.CALL_ENTRY_ID%type) is
                                                SELECT ID_AGENT
                                                FROM CALLS                                                                                                
                                                WHERE CALL_ENTRY_ID = idCalls;

BEGIN
    OPEN c_Call (idCalls);
    FETCH c_Call INTO idAgentCall;
    WHILE c_Call %FOUND 
         LOOP
         dbms_output.put_line('--Group: '||usergroup||' -- Name: '|| userName|| ' -- Id Agent: ' || idAgentCall);
         FETCH c_Call INTO idAgentCall;
         END LOOP;
    CLOSE c_Call ;
END; 

I have tried using distinct and GROUP BY but it still keeps returning the repeated values. Any ideas?


Solution

  • if you execute this query in SQL plus then do you get multiple rows and if you get multiple rows for ID_AGENT, then can you try using DISTINCT and see if you are getting one row or multiple tows per agent.

    SELECT ID_AGENT FROM CALLS WHERE CALL_ENTRY_ID = idCalls;