If it is only about multiplying the salary by 2
when a player has played more than 50
matches, then you can simply avoid doing the join by moving the% share% off the% main% co within a subquery .
I leave you 2 options, one of which is similar to the one presented by @Miguel in its good response, but simplified:
Using partidos
:
update jugadores
set sueldo = sueldo * 2
where rut in (select rut
from partidos
group by rut
having count(*) > 50)
Or using update
:
update jugadores j
set j.sueldo = j.sueldo * 2
where exists (select null
from partidos p
where p.rut = j.rut
having count(*) > 50);
Now, if the idea is that you will want to put the salary up to date depending on the number of games played, such as avalanches with the following comments:
Then, each player who has played more than 50 matches ( can be 10, 20 or any other number ) is given double the salary in the table player ( may be a percentage more ), but depends on the number of matches played .
... then it is necessary to restructure the query so that the IN
can take into account the number of matches when allocating the new salary.
To give an example, let's say that the desired logic is that, if the player has played more than 20 games, then he receives a 50% increase, but if he has played more than 50 games, then the salary doubles.
Normally, this type of EXISTS
would be done with a join, but with Oracle, doing it this way, although not impossible, is extremely uncomfortable. The best thing in this case is to use the statement UPDATE
:
merge into jugadores j
using (
select rut,
count(*) as cantidad
from partidos
group by rut
having count(*) > 20
) p
on (p.rut = j.rut)
when matched then
update set j.sueldo = case when p.cantidad > 50
then j.sueldo * 2
else j.sueldo * 1.5
end;
If you want to use a UPDATE
, the query would be written like this:
update (
select j.rut,
j.sueldo,
p.cantidad
from jugadores j
join (select rut, count(*) as cantidad
from partidos
group by rut
having count(*) > 20) p
on p.rut = j.rut
)
set sueldo = case when cantidad > 50
then sueldo * 2
else sueldo * 1.5
end;
But, again, I recommend you use the MERGE
statement since to write a UPDATE
statement that includes joins and that runs successfully, you must be very careful not to receive the following error:
ORA-01732: data manipulation operation not legal on this view
In fact, I think that the rules as to what types of joins in the updates are allowed have been changing from version to version. So it's possible that the example of MERGE
that I put to you does not work according to your version of Oracle.