无法执行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"
我想用这个查询做的是:
- 阈值表包含列阈值类型,其中包含现金交易表的列名称
- 对于阈值表中的每个记录,我们需要根据现金交易表中的阈值类型来比较总和(金额)组。
- 并将获取的数量与阈值表的threshold_amount比较
- 我需要选择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,那么我们不需要在查询中包括分区,那么如何获取它呢?
-
只有在动态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);
-
SQLGrammarException:无法执行查询
2021-02-01 关注 0 浏览126 1答案
-
休眠:无法执行本机批量操作查询
2021-02-01 关注 0 浏览108 1答案
-
您是否遇到过SQL Server由于引用过多表而无法执行的查询?
2021-04-20 关注 0 浏览134 1答案
-
org.hibernate.exception.GenericJDBCException:无法执行查询
2021-02-01 关注 0 浏览107 1答案
-
MySQL 错误 2014 的原因无法执行查询而其他无缓冲查询处于活动状态
2021-07-06 关注 0 浏览109 1答案
-
获取org.hibernate.exception.JDBCConnectionException:即使通过JNDI也无法执行查询
2021-02-01 关注 0 浏览86 1答案
-
无法在 .map() 中执行 SQL 查询
2022-07-28 关注 0 浏览11 1答案
-
MySQL错误2014的原因其他未缓冲的查询处于活动状态时无法执行查询
2021-02-02 关注 0 浏览77 1答案
-
结果查看器无法执行项目列表中超过655列的查询
2021-06-15 关注 0 浏览97 1答案
-
Crontab无法执行Python脚本
2021-01-29 关注 0 浏览143 1答案