Home > Mobile >  Oracle SQL - Group function not allowed here
Oracle SQL - Group function not allowed here

Time:11-07

How can this code work well:

select count(distinct t.title_id), count(*)
    from member m, rental r, title t
  where m.member_id = r.member_id
  and r.title_id = t.title_id
  and m.first_name = 'Carmen'
  and m.last_name = 'Velasquez';

and this not:

set serveroutput on;
DECLARE
  lst_name employees.last_name%TYPE := '&lst';
  fst_name employees.first_name%TYPE := '&fst';
  res NUMBER(3);
  titles NUMBER(3);
BEGIN
  select count(*) into res, count(distinct t.title_id) into titles
    from member m, rental r, title t
  where m.member_id = r.member_id
  and r.title_id = t.title_id
  and m.first_name = fst_name
  and m.last_name = lst_name;
  
  dbms_output.put_line(fst_name || ' ' || lst_name || ':' || res || ' number of titles: ' || titles);
END;

The error I get for the second snippet is: PL/SQL: ORA-00934: group function is not allowed here.

The error comes for sure from: select count(*) into res, count(distinct t.title_id) into titles, but I can not understand why in the first code snippet it works but in the second doesn't.

CodePudding user response:

Wrong syntax. select first, into next

select count(*), count(distinct t.title_id)
into res, titles
from ...
  • Related