我如何加入以下查询?

时间:2009-12-07 05:41:04

标签: sql-server

SELECT 
PB_BANK_CODE,    ---- DB ITEM
PB_BANK_NAME,  ---- DB ITEM
TOTAL_AMOUNT   --- NON DB ITEM
        FROM    GLAS_PDC_BANKS          

where PB_COMP_CODE=:parameter.COMP_CODE AND  pb_bank_code in(select distinct pc_bank_from from glas_pdc_cheques where PC_COMP_CODE=:parameter.COMP_CODE AND pc_due_dateTIME between :block01.date_from and :block01.date_to AND ISNULL(pc_discd,'X') = 'R')
order by pb_bank_code

总金额:

    BEGIN
  SELECT SUM(PC_AMOUNT) INTO :BLOCK02.TOTAL_AMOUNT
    FROM GLAS_PDC_CHEQUES
   WHERE PC_DUE_DATE BETWEEN :BLOCK01.DATE_FROM AND :BLOCK01.DATE_TO
     AND   PC_BANK_FROM = :BLOCK02.PB_BANK_CODE
     AND   NVL(PC_DISCD, 'X') = 'R';
EXCEPTION WHEN OTHERS THEN :BLOCK02.TOTAL_AMOUNT := 0;
END;

是否需要加入?给予解决方案

2 个答案:

答案 0 :(得分:1)

使用:

  SELECT b.pb_bank_code,
         b.pb_bank_name,
         x.total_amount
    FROM GLAS_PDC_BANKS b
    JOIN (SELECT c.pc_bank_from,
                 SUM(c.pc_amount) AS total_amount
            FROM GLAS_PDC_CHEQUES c
           WHERE c.pc_due_dateTIME BETWEEN :block01.date_from AND :block01.date_to 
             AND c.pc_discd = 'R'
        GROUP BY c.pc_bank_from) x ON x.pc_bank_from = b.pb_bank_code
   WHERE b.pb_comp_code = :parameter.COMP_CODE 
ORDER BY b.pb_bank_code

答案 1 :(得分:0)

从列表中合并,为每个表提供别名Banks b。

然后将选择列表组合在每个字段名称b.code。

之前的相应别名之前

对于where和order by执行相同操作。

最后将where子句添加到与表中的记录匹配的条件,c.id = b.cheques_id或其他任何内容。