We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Error in pl/sql function

axtur
axtur asked
on
Medium Priority
531 Views
Last Modified: 2013-12-19
I keep getting an error in the next pl sql function:

No group function allowe here says
CREATE OR REPLACE
FUNCTION CHOFER (HORARIOINI IN NUMBER, HORARIOFIN IN INTEGER) RETURN VARCHAR2 AS
v_nombre VARCHAR2(32);
v_cuenta NUMBER;
BEGIN
select tchofer.nombre_chofer into v_nombre, count(tcliente.cod_cliente)as pasajero
FROM
tchofer, tconduce, testa_constituida, tviaja, tcliente, tlineahorario
where tchofer.cod_chofer = tconduce.cod_chofer
AND tconduce.cod_bus = testa_constituida.cod_bus
AND tviaja.cod_linea = testa_constituida.cod_linea
AND tviaja.cod_cliente = tcliente.cod_cliente
AND tlineahorario.cod_linea = testa_constituida.cod_linea
AND tcliente.movilidad_reducida='S'
AND tlineahorario.horario between horarioini and horariofin
AND rownum < 2
group by tchofer.nombre_chofer
order by pasajeros desc;
  RETURN v_nombre;
END CHOFER;

Open in new window

Comment
Watch Question

Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009

Commented:
>select tchofer.nombre_chofer into v_nombre, count(tcliente.cod_cliente)as pasajero

a function can only return 1 single value, not a complete rowset
please clarify what the function should actually do?

Commented:
the format of your select statement is slightly off.  It should be:
SELECT col1, col2 ...
INTO var1, var2
FROM table
WHERE ...

Try this:
CREATE OR REPLACE
FUNCTION CHOFER (HORARIOINI IN NUMBER, HORARIOFIN IN INTEGER) RETURN VARCHAR2 AS
v_nombre VARCHAR2(32);
v_cuenta NUMBER;
BEGIN
select tchofer.nombre_chofer, count(tcliente.cod_cliente)as pasajero
INTO v_nombre, v_cuenta
FROM tchofer, tconduce, testa_constituida, tviaja, tcliente, tlineahorario
where tchofer.cod_chofer = tconduce.cod_chofer
AND tconduce.cod_bus = testa_constituida.cod_bus
AND tviaja.cod_linea = testa_constituida.cod_linea
AND tviaja.cod_cliente = tcliente.cod_cliente
AND tlineahorario.cod_linea = testa_constituida.cod_linea
AND tcliente.movilidad_reducida='S'
AND tlineahorario.horario between horarioini and horariofin
AND rownum < 2
group by tchofer.nombre_chofer
order by pasajeros desc;
  RETURN v_nombre;
END CHOFER;

Open in new window

Author

Commented:
it isn't working that either andy, forget about the v_cuenta NUMBER

this should return the name of the "chofer" with most "pasajeros" witht the given conditions...
CREATE OR REPLACE
FUNCTION CHOFER (HORARIOINI IN NUMBER, HORARIOFIN IN INTEGER) RETURN VARCHAR2 AS
v_nombre VARCHAR2(32);
BEGIN
select tchofer.nombre_chofer into v_nombre, count(tcliente.cod_cliente)as pasajero
FROM
tchofer, tconduce, testa_constituida, tviaja, tcliente, tlineahorario
where tchofer.cod_chofer = tconduce.cod_chofer
AND tconduce.cod_bus = testa_constituida.cod_bus
AND tviaja.cod_linea = testa_constituida.cod_linea
AND tviaja.cod_cliente = tcliente.cod_cliente
AND tlineahorario.cod_linea = testa_constituida.cod_linea
AND tcliente.movilidad_reducida='S'
AND tlineahorario.horario between horarioini and horariofin
AND rownum < 2
group by tchofer.nombre_chofer
order by pasajeros desc;
  RETURN v_nombre;
END CHOFER;

Open in new window

Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Commented:
You're query is not quite right for that.  Your query should be as follows:
SELECT  tchofer.nombre_chofer 
INTO v_nombre, 
FROM ( SELECT tchofer.nombre_chofer, count(tcliente.cod_cliente)as pasajero
       FROM tchofer, tconduce, testa_constituida, tviaja, tcliente, tlineahorario
       WHERE tchofer.cod_chofer = tconduce.cod_chofer
         AND tconduce.cod_bus = testa_constituida.cod_bus
          AND tviaja.cod_linea = testa_constituida.cod_linea
          AND tviaja.cod_cliente = tcliente.cod_cliente
          AND tlineahorario.cod_linea = testa_constituida.cod_linea
          AND tcliente.movilidad_reducida='S'
          AND tlineahorario.horario between horarioini and horariofin
          AND rownum < 2
     GROUP BY tchofer.nombre_chofer
     ORDER BY pasajeros desc )
WHERE rownum = 1;

Open in new window

awking00Information Technology Specialist
CERTIFIED EXPERT

Commented:
Andytw beat me to it, but you should remove the comma after
INTO v_nombre
awking00Information Technology Specialist
CERTIFIED EXPERT

Commented:
Didn't see that angelIII also beat me to it. I must learn to type faster :-)

Commented:
awking00: yes, just, but I was beaten to it by angelIII  ; - ).  Yes, a small typo there, with the trailing ',' after  v_nombre, thanks for spotting it.
pls  catch the exception .....

EXCEPTION
   WHEN NO_DATA_FOUND
   THEN
      RETURN NULL;

Author

Commented:
I keep getting this:

Error(5,1): PL/SQL: SQL Statement ignored
Error(5,8): PL/SQL: ORA-00904: "TCHOFER"."NOMBRE_CHOFER": identificador no vĂ¡lido
create or replace FUNCTION CHOFER (HORARIOINI IN NUMBER, HORARIOFIN IN INTEGER) RETURN VARCHAR2 AS
v_nombre VARCHAR2(32);
BEGIN
SELECT tchofer.nombre_chofer
INTO v_nombre 
FROM (SELECT tchofer.nombre_chofer, count(tcliente.cod_cliente) as pasajeros
       FROM tchofer, tconduce, testa_constituida, tviaja, tcliente, tlineahorario
       WHERE tchofer.cod_chofer = tconduce.cod_chofer
         AND tconduce.cod_bus = testa_constituida.cod_bus
          AND tviaja.cod_linea = testa_constituida.cod_linea
          AND tviaja.cod_cliente = tcliente.cod_cliente
          AND tlineahorario.cod_linea = testa_constituida.cod_linea
          AND tcliente.movilidad_reducida='S'
          AND tlineahorario.horario between horarioini and horariofin
          AND rownum < 2
     GROUP BY tchofer.nombre_chofer
     ORDER BY pasajeros desc );
END CHOFER;

Open in new window

Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009

Commented:
please try my suggestion above.


note: the ORDER BY + WHERE ROWNUM <= 2 will NOT give you the expected results!

Commented:
ORA-00904: "%s": invalid identifier.
It's not recognising NOMBRE_CHOFER as a column, I suspect that column name is a quoted identifier.

In SQL*Plus can you issue the command "DESC tchofer"?  You should see output similar to the following:
SQL> DESC tchofer
Name                            ....
-----------
NOMBRE_CHOFER
....
You need to quote the column name in your query, and copy the case as returned by the command above  e.g.
"Nombre_chofer"

Commented:
>>this should return the name of the "chofer" with most "pasajeros" witht the given conditions...
angelIII is correct, your query will NOT give the right answer.  In order to get the result you want, you should try either angelIII's solution (http:#24745780) or my solution (http:#24745784).

Author

Commented:
Good angel, whats the meaning of sq where sq.r=1
Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009

Commented:
> , row_number() over (order by count(tcliente.cod_cliente) desc) r
defined the column alias "r"

and the "sq" (subquery) comes from here:

SELECT nombre_chofer into v_nombre
FROM (   <subquery>
   ) sq   --- <<< here
  WHERE sq.r = 1
CREATE OR REPLACE FUNCTION chofer(horarioini IN NUMBER, horariofin IN INTEGER)
   RETURN VARCHAR2
AS
   v_nombre   VARCHAR2(32);
   v_cuenta   NUMBER;
BEGIN
   SELECT d.nombre_chofer
     INTO v_nombre
     FROM (SELECT   tchofer.nombre_chofer,
                    COUNT(tcliente.cod_cliente) AS pasajero
               FROM tchofer,
                    tconduce,
                    testa_constituida,
                    tviaja,
                    tcliente,
                    tlineahorario
              WHERE tchofer.cod_chofer = tconduce.cod_chofer
                AND tconduce.cod_bus = testa_constituida.cod_bus
                AND tviaja.cod_linea = testa_constituida.cod_linea
                AND tviaja.cod_cliente = tcliente.cod_cliente
                AND tlineahorario.cod_linea = testa_constituida.cod_linea
                AND tcliente.movilidad_reducida = 'S'
                AND tlineahorario.horario BETWEEN horarioini AND horariofin
           GROUP BY tchofer.nombre_chofer
           ORDER BY pasajeros DESC) d
    WHERE ROWNUM = 1;

   RETURN v_nombre;
EXCEPTION
   WHEN NO_DATA_FOUND
   THEN
      RETURN NULL;
END chofer;
hello axtur:

1 . tu necesitas una funcion que te traiga el nombre del chofer, dandole solo el horario de inicio y fin.

2. necesitas modificar tu consulta:
      - el error te lo marca porque tienes un count y no tienes un group by.

select tchofer.nombre_chofer, count(tcliente.cod_cliente)as pasajero
INTO v_nombre, v_cuenta
FROM tchofer, tconduce, testa_constituida, tviaja, tcliente, tlineahorario
where tchofer.cod_chofer = tconduce.cod_chofer
AND tconduce.cod_bus = testa_constituida.cod_bus
AND tviaja.cod_linea = testa_constituida.cod_linea
AND tviaja.cod_cliente = tcliente.cod_cliente
AND tlineahorario.cod_linea = testa_constituida.cod_linea
AND tcliente.movilidad_reducida='S'
AND tlineahorario.horario between horarioini and horariofin
group by  tchofer.nombre_chofer

 // tambien puedes consultarme en joaquin_ma hotmail
preguntas:
 
1. creo que no necesitas la tabla de clientes para esta consulta.
2. es interesante ver que este querie te puede traer mas de un registro porque puedes tener 2 choferes con los mismos horarios. creo que aqui tienes un problema de la definicion del problema.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.