1
SELECT   PB.BUKTI_ID,
           PB.BUKTI_DATE,
           CBD.DISBURSE_DATE,
           PB.CB_ID,
           PB.BUKTI_RECEIVED_BY,
           PB.NPWP,
           (SELECT   SUM (TOTAL_AMOUNT)
              FROM      TRX_PPH23_BUKTI PB
                     LEFT JOIN
                        TRX_PPH23_BUKTI_DETAIL PBD
                     ON PB.BUKTI_ID = PBD.REF_BUKTI_ID)
              AS TOTAL_AMOUNT,
           PB.BRANCH_ID,
           DECODE (PB.BUKTI_STATUS, 'O', 'Open', 'B', 'Cancel', 'Close')
              BUKTI_STATUS
    FROM      TRX_PPH23_BUKTI PB
           LEFT JOIN
              ( (SELECT   DISBURSE_NO,
                          DISBURSE_DATE,
                          RECEIVED_BY,
                          TOTAL_AMOUNT,
                          DISBURSE_STATUS
                   FROM   TRX_CASH_DISBURSE_HEADER)
               UNION
               (SELECT   DISBURSE_NO,
                         DISBURSE_DATE,
                         RECEIVED_BY,
                         TOTAL_AMOUNT,
                         DISBURSE_STATUS
                  FROM   TRX_BANK_DISBURSE_HEADER)) CBD
           ON PB.CB_ID = CBD.DISBURSE_NO
ORDER BY   PB.BUKTI_ID DESC;

我想从其他表(TRX_PPH23_BUKTI_DETAIL)中选择字段总和(total_amount),但我对此列的值相同,

在此处输入图像描述

我知道问题因为我没有特别说密钥(bukti_id).. 但我不知道如何在没有存储过程+参数的情况下循环这个查询

4

3 回答 3

1

您必须将内部查询与外部表关联起来,而不是再次声明它

  ...
  PB.BUKTI_RECEIVED_BY,
   PB.NPWP,
   (SELECT SUM (TOTAL_AMOUNT)
    FROM TRX_PPH23_BUKTI_DETAIL PBD
    WHERE PBD.REF_BUKTI_ID = PB.BUKTI_ID)
      AS TOTAL_AMOUNT,
   PB.BRANCH_ID,
  ...
于 2013-08-22T08:50:38.210 回答
1

这可能有效,但应在联接中包含带有总和的查询,

    SELECT   PB.BUKTI_ID,
               PB.BUKTI_DATE,
               CBD.DISBURSE_DATE,
               PB.CB_ID,
               PB.BUKTI_RECEIVED_BY,
               PB.NPWP,
               (SELECT   SUM (TOTAL_AMOUNT)
                  FROM      TRX_PPH23_BUKTI PB2
                         LEFT JOIN
                            TRX_PPH23_BUKTI_DETAIL PBD
                         ON PB2.BUKTI_ID = PBD.REF_BUKTI_ID and PB.BUKTI_ID = PB2.BUKTI_ID)
                  AS TOTAL_AMOUNT,
               PB.BRANCH_ID,
               DECODE (PB.BUKTI_STATUS, 'O', 'Open', 'B', 'Cancel', 'Close')
                  BUKTI_STATUS
        FROM      TRX_PPH23_BUKTI PB
               LEFT JOIN
                  ( (SELECT   DISBURSE_NO,
                              DISBURSE_DATE,
                              RECEIVED_BY,
                              TOTAL_AMOUNT,
                              DISBURSE_STATUS
                       FROM   TRX_CASH_DISBURSE_HEADER)
                   UNION
                   (SELECT   DISBURSE_NO,
                             DISBURSE_DATE,
                             RECEIVED_BY,
                             TOTAL_AMOUNT,
                             DISBURSE_STATUS
                      FROM   TRX_BANK_DISBURSE_HEADER)) CBD
               ON PB.CB_ID = CBD.DISBURSE_NO
join ( (SELECT   SUM (TOTAL_AMOUNT)
                  FROM      TRX_PPH23_BUKTI PB2
                         LEFT JOIN
                            TRX_PPH23_BUKTI_DETAIL PBD
                         ON PB2.BUKTI_ID = PBD.REF_BUKTI_ID group by PB2.BUKTI_ID  )) as X_2 on X_2.BUKTI_ID = PB.BUKTI_ID
where 
    ORDER BY   PB.BUKTI_ID DESC;

谢谢

于 2013-08-22T08:52:53.063 回答
0

这适合你吗?

SELECT * FROM (
    SELECT   PB.BUKTI_ID,
           PB.BUKTI_DATE,
           CBD.DISBURSE_DATE,
           PB.CB_ID,
           PB.BUKTI_RECEIVED_BY,
           PB.NPWP,
           (SELECT   SUM (TOTAL_AMOUNT)
              FROM      TRX_PPH23_BUKTI PB
                     LEFT JOIN
                        TRX_PPH23_BUKTI_DETAIL PBD
                     ON PB.BUKTI_ID = PBD.REF_BUKTI_ID)
              AS TOTAL_AMOUNT,
           PB.BRANCH_ID,
           DECODE (PB.BUKTI_STATUS, 'O', 'Open', 'B', 'Cancel', 'Close')
              BUKTI_STATUS
    FROM      TRX_PPH23_BUKTI PB
           LEFT JOIN
              ( (SELECT   DISBURSE_NO,
                          DISBURSE_DATE,
                          RECEIVED_BY,
                          TOTAL_AMOUNT,
                          DISBURSE_STATUS
                   FROM   TRX_CASH_DISBURSE_HEADER)
               UNION
               (SELECT   DISBURSE_NO,
                         DISBURSE_DATE,
                         RECEIVED_BY,
                         TOTAL_AMOUNT,
                         DISBURSE_STATUS
                  FROM   TRX_BANK_DISBURSE_HEADER)) CBD
           ON PB.CB_ID = CBD.DISBURSE_NO
) TRX INNER JOIN TRX_PPH23_BUKTI_DETAIL TRXb ON Trx.BUKTI_ID =TRXb.BUKTI_ID
于 2013-08-22T09:48:38.640 回答