选择与其他表的列连接

时间:2013-08-22 08:45:01

标签: sql oracle

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),但我对此列有相同的值,

enter image description here

我知道问题因为我没有具体地说关键(bukti_id)..但我不知道 如何在没有存储过程+参数

的情况下循环此查询

3 个答案:

答案 0 :(得分: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,
  ...

答案 1 :(得分: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;

感谢

答案 2 :(得分: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