Error in pl/sql function

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

axturAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
>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?
0
AndytwCommented:
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

0
axturAuthor 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

0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
this should do:
CREATE OR REPLACE
FUNCTION CHOFER (HORARIOINI IN NUMBER, HORARIOFIN IN INTEGER) RETURN VARCHAR2 AS
v_nombre VARCHAR2(32);
BEGIN
SELECT nombre_chofer into v_nombre
FROM ( 
  select tchofer.nombre_chofer 
      , count(tcliente.cod_cliente) as pasajero
      , row_number() over (order by count(tcliente.cod_cliente) desc) r
    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
   ) sq
  WHERE sq.r = 1
  ;
  RETURN v_nombre;
END CHOFER;

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AndytwCommented:
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

0
awking00Information Technology SpecialistCommented:
Andytw beat me to it, but you should remove the comma after
INTO v_nombre
0
awking00Information Technology SpecialistCommented:
Didn't see that angelIII also beat me to it. I must learn to type faster :-)
0
AndytwCommented:
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.
0
shru_0409Commented:
pls  catch the exception .....

EXCEPTION
   WHEN NO_DATA_FOUND
   THEN
      RETURN NULL;

0
axturAuthor 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

0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
please try my suggestion above.


note: the ORDER BY + WHERE ROWNUM <= 2 will NOT give you the expected results!
0
AndytwCommented:
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"

0
AndytwCommented:
>>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).
0
axturAuthor Commented:
Good angel, whats the meaning of sq where sq.r=1
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
> , 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
0
shru_0409Commented:
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;
0
latorreconsultoresCommented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Oracle Database

From novice to tech pro — start learning today.