如何从一组ID号中获取变量的SUM? PLSQL

时间:2017-05-04 03:42:56

标签: mysql sql oracle plsql logic

我的问题是我有两张桌子,

SQL> select * from paydata1;

IDNO NAME                      J     SALARY      PAYHR
---- ------------------------- - ---------- ----------
1111 Ann French                S      75000          0
2222 Robert Costa              H          0         45
3333 Linda Ames                H          0         50
4444 Scott Brooks              S      78000          0
5555 Susan Ash                 S      57000          0
6666 James Smith               S      55000          0
7777 Mary Jones                H          0         36
8888 John Morse                H          0         39

SQL> select * from paytran1;

IDNO PRO    HOURSWK
---- --- ----------
1111 123         20
1111 456         10
1111 789         15
2222 123         17
2222 456         24
2222 789         20
3333 123         20
3333 789         20
4444 123         10
4444 456         28
4444 789          5

IDNO PRO    HOURSWK
---- --- ----------
5555 456         40
6666 456         44
7777 456         30
7777 789         15
8888 123         10
8888 456         25
8888 789          5

MY PL / SQL代码:

SET SERVEROUTPUT ON
DECLARE
        v_idno  paydata1.idno%TYPE;
        v_name  paydata1.name%TYPE;
        v_jcode paydata1.jobcode%TYPE;
        v_sal   paydata1.salary%TYPE;
        v_payh  paydata1.payhr%TYPE;
        v_hrsw  paytran1.hourswk%TYPE;
        CURSOR curse IS
                SELECT paydata1.idno, name, jobcode, salary, payhr, hourswk
                FROM paydata1, paytran1;
BEGIN
OPEN curse;
FETCH curse INTO v_idno, v_name, v_jcode, v_sal, v_payh, v_hrsw;
WHILE curse%ROWCOUNT < 9 AND curse%FOUND LOOP
        IF v_jcode = 'S' THEN
                v_sal := v_sal / 52;
                DBMS_OUTPUT.PUT_LINE(v_name || ': weekly salary is ' || v_sal);

        ELSE
                v_hrsw := sum(payhr)
                v_sal := v_payh * v_hrsw;
                DBMS_OUTPUT.PUT_LINE(v_name || ': overall pay is ' || v_sal);
        END IF;
   FETCH curse INTO v_idno, v_name, v_jcode, v_sal, v_payh, v_hrsw;
END LOOP;
CLOSE curse;
END;
/

SET SERVEROUTPUT OFF

我需要能够将员工划分到他们工资的地方,并找到每小时的工资乘以工作小时数(从第二张表开始) 我的问题是,在第二个表格中,工作时间分为多个部分。如何获得工作小时数的总和,以便将其放入我的变量&#39; v_hrsw&#39;?

现在它只抓住它在paytran1.hourswk中找到的第一个数字用作v_hrsw。

感谢您的帮助。

编辑:

结果目前看起来像......

Ann French: weekly salary is 1442.31
Robert Costa: overall pay is 900
Linda Ames: overall pay is 1000
Scott Brooks: weekly salary is 1500
Susan Ash: weekly salary is 1096.15
James Smith: weekly salary is 1057.69
Mary Jones: overall pay is 720
John Morse: overall pay is 780

但任何人都有&#34;整体薪水&#34;总数不正确。

1 个答案:

答案 0 :(得分:1)

直接使用此查询获取输出。

    select
      d.name||(case when d.j='S' then ': Weekly salary is ' 
          when d.j='H' then ': overall pay is '
      end)||(case when d.J='S' then d.salary/52 
          when d.j='H' then sum(t.hourswk)*d.payhr
        end)  as output
    from
     paydata1 d inner join paytran1 t
    on d.idno=t.idno
    group by d.idno ,d.name,d.J,d.salary,d.payhr;

因此您可以将PL / SQL代码更改为

declare
cursor curse is 
    select
      d.name||(case when d.j='S' then ': Weekly salary is ' 
          when d.j='H' then ': overall pay is '
      end)||(case when d.J='S' then d.salary/52 
          when d.j='H' then sum(t.hourswk)*d.payhr
        end)  as output
    from
     paydata1 d inner join paytran1 t
    on d.idno=t.idno
    group by d.idno ,d.name,d.J,d.salary,d.payhr;
  begin
  for c1 in curse
  loop
    DBMS_OUTPUT.PUT_LINE(c1.output);
 end loop;
 end;