Question

I have the following 2 tables:

enter image description here

I would like to create a procedure/func wherein upon entering a USERID, I will be able to get their ranking. Ranking is based on the total rank points multiplied by the tokens divided by the total tokens the user earned.

The Rank numerical equivalent are as follows: SU = 25, EX = 9, VG = 5, G = 3, F = 1

For example, based on my data, userid 45's ranking is = [25(4) + 3(5)]/4 + 5 = 12.77 (which would be my Output)

Here is my attempt... I messed up the math part, it just shows up "User Ranking is: " (blank/no value for the ranking)

create or replace procedure rank
(para_userid IN number, para_ranking OUT number)
is
    n number;
    rank number;
    r number;
CURSOR c1(uid number) IS 
        select decode(rank, 'SU', 25, 'EX', 9, 'VG', 5, 'G', 3, 'F',1) FROM A
            WHERE para_userid= uid;    
begin
        n := 0;
        para_ranking := 0;
        OPEN c1(para_userid);
        LOOP 
            FETCH c1 INTO rank; 
            EXIT WHEN c1%NOTFOUND;     
            r := r * rank; 
            n := n + 1; 
        END LOOP; 
        IF n > 0 THEN 
            para_ranking := r / n; 
        END IF; 
        CLOSE c1; 
    END; 


declare 
x number;
begin
rank(45, x);
DBMS_OUTPUT.PUT_LINE('User Ranking is: '||x);
end;
/
Était-ce utile?

La solution

Here's a query to get all the user rankings. It should be straightforward to restrict this to a single user and put it in a procedure:

Select
    a.userid,
    Sum(decode(a.rank, 'SU', 25, 'EX', 9, 'VG', 5, 'G', 3, 'F',1) * b.tokens)
        / sum(b.tokens) ranking
From
    A
        inner join
    B
        on a.GameID = b.GameID
group by
    a.userid
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top