我的桌子下面由不同颜色的球队组成,这些球队打过另一种颜色并获得积分。
CREATE TABLE COLOURS
(C_ID VARCHAR2 (4) NOT NULL,
HC VARCHAR2 (4),
AC VARCHAR2 (4),
TOTAL NUMBER (2),
SCORES NUMBER (2));
INSERT INTO COLOURS VALUES
('C1', 'RED', 'BLUE',8,'');
INSERT INTO COLOURS VALUES
('C2', 'RED', 'BLUE', 9,'');
INSERT INTO COLOURS VALUES
('C3','BLUE', 'RED', 10,'');
INSERT INTO COLOURS VALUES
('C4','BLUE', 'RED', 11,'');
接下来,我创建了一个计算团队总分的函数:
CREATE OR REPLACE FUNCTION TOTAL (i_HC COLOURS.HC%TYPE)
RETURN NUMBER
IS
v_COLOURS_total COLOURS.total%TYPE;
BEGIN
SELECT SUM (TOTAL)
INTO v_COLOURS_total
FROM COLOURS
WHERE
HC = i_HC;
IF v_COLOURS_total IS NULL THEN
v_COLOURS_total:=0;
END IF;
RETURN v_COLOURS_total;
END;
最后是调用该函数并一一显示每个团队的结果的过程:
CREATE OR REPLACE PROCEDURE COLOURS_SCORES (dnum VARCHAR2)
IS
W_HC COLOURS.HC%TYPE;
W_C_ID COLOURS.C_ID%TYPE;
W_TOTAL COLOURS.TOTAL%TYPE;
W_SCORES NUMBER(2);
TOTALSCORES NUMBER(5,2);
CURSOR COLOURS_CURSOR IS
SELECT HC,C_ID,TOTAL,SCORES
FROM COLOURS
WHERE HC = dnum;
BEGIN
DBMS_OUTPUT.PUT_LINE(' '||'COLOURS POINTS '||' ');
OPEN COLOURS_CURSOR;
TOTALSCORES :=TOTAL(dnum);
LOOP
FETCH COLOURS_CURSOR INTO W_HC,W_C_ID,W_TOTAL, W_SCORES;
EXIT WHEN COLOURS_CURSOR%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(W_C_ID || '----'|| W_TOTAL ||' ');
END LOOP;
DBMS_OUTPUT.PUT_LINE('---------------------------------------');
DBMS_OUTPUT.PUT_LINE(W_HC||' '||'FINAL SCORE = '||TO_CHAR(TOTALSCORES));
DBMS_OUTPUT.PUT_LINE('---------------------------------------');
CLOSE COLOURS_CURSOR;
END;
现在,在执行该过程后,例如EXECUTE COLOURS_SCORES ('RED');
我得到颜色的结果:
如果参数是RED我得到
(17)
.如果参数是BLUE我得到
(21)
.
我的问题是:
是否可以显示两个团队的结果并按最高总数排序?
或者也许,我只需要一个函数吗?
帮助将不胜感激,在此先感谢。