【发布时间】:2011-05-11 11:46:08
【问题描述】:
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 ok 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.so 450 threshold_amount for threshold_1 与上述所有记录进行比较 3.so所需的输出将是
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,那么我们不需要在查询中包含partition by part,那么如何获取呢?
【问题讨论】:
-
如果您将其放入代码中并格式化行,这样您就可以看到缺少的内容,这样会更容易阅读。我的猜测是您缺少内部选择的 from 语句,但我没有 Oracle,因此无法对其进行测试...
-
@marc_s : 是的,我没有提到细节...抱歉
-
@Paul W:我没明白你的意思
-
请提供
desc thresholds和desc cash_transactions的输出以及两个表的一些示例行。根据目前的信息,很难理解您要达到的目标。