Try it like this:
SET SERVEROUTPUT ON
Create or replace type t_email_type is table of varchar2(100);
/
Declare -- Outer block - just to populate variable emails of type t_email_type
Cursor c_emails IS
Select 'a@mail.com' "EMAIL" From Dual Union All
Select 'b@mail.com' "EMAIL" From Dual Union All
Select 'c@mail.com' "EMAIL" From Dual Union All
Select 'd@mail.com' "EMAIL" From Dual Union All
Select 'c@mail.com' "EMAIL" From Dual Union All
Select 'c@mail.com' "EMAIL" From Dual Union All
Select 'a@mail.com' "EMAIL" From Dual Union All
Select 'a@mail.com' "EMAIL" From Dual Union All
Select 'b@mail.com' "EMAIL" From Dual Union All
Select 'b@mail.com' "EMAIL" From Dual Union All
Select 'c@mail.com' "EMAIL" From Dual;
email VarChar2(100);
emails t_email_type := t_email_type();
i Number(3) := 0;
Begin
OPEN c_emails;
LOOP
FETCH c_emails Into email;
EXIT WHEN c_emails%NOTFOUND;
i := i + 1;
emails.extend;
emails(i) := email;
END LOOP;
CLOSE c_emails;
-- Inner block - get distinct emails and number of appearances from variable emails of type t_email_type
Declare
Cursor c Is
Select COLUMN_VALUE "EMAIL", Count(*) "NUM_OF_APPEARANCES"
From TABLE(emails)
Group By COLUMN_VALUE
Order By COLUMN_VALUE;
cSet c%ROWTYPE;
i Number(3) := 0;
Begin
OPEN c;
LOOP
FETCH c Into cSet;
EXIT WHEN c%NOTFOUND;
i := i + 1;
If i = 1 Then
DBMS_OUTPUT.PUT_LINE(RPAD('EMAIL', 20, ' ') || ' ' || LPAD('NUM_OF_APPEARANCES', 20, ' '));
DBMS_OUTPUT.PUT_LINE(RPAD('-', 20, '-') || ' ' || LPAD('-', 20, '-'));
End If;
DBMS_OUTPUT.PUT_LINE(RPAD(cSet.EMAIL, 20, ' ') || ' ' || LPAD(cSet.NUM_OF_APPEARANCES, 20, ' '));
END LOOP;
CLOSE c;
End;
End;
/* R e s u l t :
anonymous block completed
EMAIL NUM_OF_APPEARANCES
-------------------- --------------------
a@mail.com 3
b@mail.com 3
c@mail.com 4
d@mail.com 1
*/
Outer block of this code is here just to generate data and to insert it into emails variable that is of type you have defined (t_email_type). You, probably, need just the inner block to get you list of emails with number of appearances within the table type.
SQL that gives you expected result is, actualy, the cursor in the inner block:
Select COLUMN_VALUE "EMAIL", Count(*) "NUM_OF_APPEARANCES"
From TABLE(emails)
Group By COLUMN_VALUE
Order By COLUMN_VALUE;
Result:
EMAIL |
NUM_OF_APPEARANCES |
a@mail.com |
3 |
b@mail.com |
3 |
c@mail.com |
4 |
d@mail.com |
1 |
Regards...