(选择过程)来自不同表的两个不同SMALLINT字段的SUM。我正在使用SQL Manager for Interbase和Firebird

时间:2017-04-24 01:37:17

标签: select sum firebird firebird2.5 interbase

我想在select过程中从不同的表中获取两个不同SMALLINT字段的SUM。我正在使用SQL Manager for Interbase和Firebird

表E中字段TRAINING_SRCVDAYS的值为1和2,而表A中字段LEAVE_WITHPAY_NUMDAY的值为2。

                       Table E        Table A
                           1             2
                           2

预期产出(总和)3 2

实际输出到我的程序3 4

这是我的代码。请帮忙

CREATE PROCEDURE AAAASAMPLE(
  UPDATEHANDLER VARCHAR(50) CHARACTER SET ISO8859_1 COLLATE ISO8859_1,
  WHAT_YEAR VARCHAR(50) CHARACTER SET ISO8859_1 COLLATE ISO8859_1)
RETURNS(
  FULLNAME VARCHAR(100) CHARACTER SET ISO8859_1 COLLATE ISO8859_1,
  TRAINING_SRCVDAYS SMALLINT,
  LEAVE_WITHPAY_NUMDAY SMALLINT,
  SICKLEAVE SMALLINT)
AS
BEGIN
  FOR
    SELECT
      C.EMP_SURNAME || ', ' || C.EMP_FIRSTNAME || ' ' || C.EMP_MIDDLENAME || ' '||C.EMP_SUFFIXNAME,
      SUM(E.TRAINING_SRCVDAYS),
      SUM(A.LEAVE_WITHPAY_NUMDAY),
      SUM(E.TRAINING_SRCVDAYS) - SUM(A.LEAVE_WITHPAY_NUMDAY)

    FROM LEAVE_TABLE A, POSITION_TABLE B, EMP_TABLE C, TRAINTRN_TABLE D, TRAINTYP_TABLE E
    WHERE    

    (A.EMP_PK =: UPDATEHANDLER AND A.LEAVE_FROM_YEAR =: WHAT_YEAR AND A.EMP_PK = C.EMP_PK
    AND A.POSITION_PK = B.POSITION_PK AND B.POSITION_CLASS_REF = 0 ) 

    AND 

    (D.EMP_PK = C.EMP_PK AND D.TRAINING_PK = E.TRAINING_PK AND D.EMP_PK =: UPDATEHANDLER 
    AND E.TRAINING_SRVCCRDT = 'Yes' AND E.TRAINING_FROMYEAR =: WHAT_YEAR)

     GROUP BY 
     C.EMP_SURNAME || ', ' || C.EMP_FIRSTNAME || ' ' || C.EMP_MIDDLENAME || ' '|| C.EMP_SUFFIXNAME

    INTO
      :FULLNAME,
      :TRAINING_SRCVDAYS,
      :LEAVE_WITHPAY_NUMDAY,
      :SICKLEAVE
  DO
    BEGIN
      SUSPEND;
    END
END;

0 个答案:

没有答案