【发布时间】:2019-02-22 17:42:31
【问题描述】:
我正在使用 sqlplus (oracle)。 我试图写一个语句来显示基于所有交易价值的账户价值变化
- 每个 customer_account_combination
- 每位客户
- 全行
此外,借方交易应为负数,贷方交易应保持为正数。 我需要在 oracle 中使用增强的聚合报告功能来解决它。 另外,应该有客户总数和总计。
这是我到目前为止所得到的。
select wc.first_name "First", wc.surname "Last", wt.account_type "Act Type"
case when wt.transaction_type is NULL then NULL
when wt.transaction_type = 'D'then TO_CHAR(transaction_amount*-1, '$9,999.99')
else TO_CHAR(transaction_amount, '$9,999.99')
end "Total"
from wgb_customer wc join wgb_account wa on wa.customer_number = wc.customer_number join wgb_account_type wt on wa.account_type = wt.account_type
left outer join wgb_transaction wt on wt.customer_number = wa.customer_number
and wt.account_type = wa.account_type
order by 2,3,6;
但是,它不起作用,并且不显示客户总数或总计。 请帮忙!
ERD
预期输出
【问题讨论】:
-
现在不在我的电脑后面向您展示示例,但您正在寻找的是“GROUP BY CUBE”或“GROUP BY ROLLUP”。它允许您开箱即用地生成小计和总计。
-
选择 wc.first_name "First", wc.surname "Last", wa.account_type "Type", SUM(transaction_amount) AS wt.transaction_type 为 NULL 时的总情况,然后 wt.transaction_type = NULL 'D'then TO_CHAR(transaction_amount*-1, '$9,999.99') else TO_CHAR(transaction_amount, '$9,999.99') end "Total" from wgb_customer wc, wgb_account wa, wgb_transaction wt where wc.customer_number=wa.customer_number and wa.account_type= wt.account_type GROUP BY ROLLUP (first_name, surname, account_type, transaction_amount) order by 2,3;
-
我想出了上面的评论,但它不起作用:(
-
提供样本输入和预期输出。
-
我已将预期输出附加为图像