小编典典

无法执行SQL查询

sql

select tt.threshold_id
from   (select sum(amount) over (partition by tt.threshold_type 
                                     order by tt.threshold_type ) amt
        from   cash_transactions) cash,
       thresholds tt
where  tt.threshold_amount < cash.amt

所涉及的rdms是oracle错误是

" ORA-00904: "TT"."THRESHOLD_TYPE": invalid identifier"

我想用这个查询做的是:

  1. 阈值表包含列阈值类型,其中包含现金交易表的列名称
  2. 对于阈值表中的每个记录,我们需要根据现金交易表中的阈值类型来比较总和(金额)组。
  3. 并将获取的数量与阈值表的threshold_amount比较
  4. 我需要选择threshold_id

阈值表:

Threshold_id        Threshold_type          Threshold_amount
============================================================
threshold_1         p_id                    450
threshold_2         p_id,to_acc_main_num    100

现金交易表:

Tran_inst_id    p_id    amount    to_acc_main_num
=================================================
1               E1      100       123
2               E2      200       5765  
3               E1      200       687
4               E2      300       890
5               E1      100       462

期望的输出:

让我们进行第一个提取:阈值表中的第一个记录

Threshold_id        Threshold_type          Threshold_amount
============================================================
threshold_1         p_id                    100000

1.现在threshold_type是p_id好的2.所以我需要根据cash_transactions表中的pid进行分组。3.因此从此获得的预期结果是(但我必须仅基于p_id求和)在这种情况下不是tran_inst_id

Tran_inst_id  p_id    sum(amount)
======================================
1             E1        400
2             E2        500
3             E1        400
4             E2        500
5             E1        400

1.现在,将以上每个记录的数量与threshold_1记录的数量进行比较。2.因此,将threshold_1的450
threshold_amount与所有上述记录3.进行比较,因此所需的输出将是

theshold_id   Tran_inst_id
==================================
thresold_1      2
threshold_1     4 
- the above result is for first record of threshold table ,now the same continues for the second record.

编辑:假设如果threshold_type为null,那么我们不需要在查询中包括分区,那么如何获取它呢?


阅读 183

收藏
2021-04-28

共1个答案

小编典典

只有在动态sql中才有可能,因为group by子句中的列数是可变的。例如带有一个函数:

    create or replace
    function sum_cash_transactions ( p_threshold_type varchar2) return number
    is
      v_result NUMBER;
    begin
      execute immediate ' select max( sum_amount) 
                          from( select sum(amount) as sum_amount
                                from   cash_transactions
                                group by ' || p_threshold_type || ' )'
     into v_result;
     return v_result;
     end;
/

接着

select threshold_id
 from thresholds
 where threshold_amount < sum_cash_transactions(threshold_type);

根据新要求进行编辑:

CREATE OR REPLACE package pkg AS
  TYPE res_rec_type IS RECORD (
    threshold_id  VARCHAR2(200)
  , Tran_inst_id  NUMBER(10,0)
  , sum_amount    NUMBER(22)
  );
  TYPE res_tab_type IS TABLE of res_rec_type;
  FUNCTION f1 RETURN  res_tab_type PIPELINED;
END;
/

CREATE OR REPLACE PACKAGE BODY pkg AS

  FUNCTION f1 RETURN  res_tab_type PIPELINED
  IS
    CUR    SYS_REFCURSOR;
    v_rec  res_rec_type;
  BEGIN
    FOR treshold in ( SELECT Threshold_id,  Threshold_type,   Threshold_amount FROM thresholds)
    LOOP
      OPEN CUR FOR 'SELECT ' || threshold.Threshold_id || ', tTran_inst_id,  s FROM (SELECT  tTran_inst_id, SUM(AMOUNT) OVER (PARTITION BY ' || p_Threshold_type || ') as s from cash_transactions ) WHERE s > ' || treshold.Threshold_amount ;
      LOOP
        FETCH cur INTO v_rec;
        EXIT WHEN cur%NOTFOUND;
        pipe row(v_rec);
      END LOOP;
    END LOOP;
    CLOSE cur;
    RETURN;
  END;
END;
/

SELECT * form table(pkg.f1);
2021-04-28