CREATE TABLE my_table ( bank_account, bank_id, amount ) AS
SELECT 123, 600, 1500 FROM DUAL UNION ALL
SELECT 123, 600, 2500 FROM DUAL UNION ALL
SELECT 123, 600, 3500 FROM DUAL UNION ALL
SELECT 123, 700, 500 FROM DUAL UNION ALL
SELECT 123, 700, 1000 FROM DUAL UNION ALL
SELECT 456, 800, 2000 FROM DUAL UNION ALL
SELECT 456, 900, 2000 FROM DUAL UNION ALL
SELECT 456, 900, 4000 FROM DUAL;
我需要编写SQL代码,其结果如下:
其中:
total_amount - 在特定bank_id中进行的所有交易bank_account的总和
number_of_transactions - bank_account在特定bank_id中进行的交易次数
total_num_trans - bank_account的交易总数
total_am_trans - bank_account所做的交易总额
我只是设法获得了我需要的一些结果,但无法全部获得。 这与我的开始有关:
SELECT t.bank_account
, t.bank_id
, count(*) number_of_transactions
, sum(t.amount) total_amount
FROM my_table t
GROUP BY t.bank_account
, t.bank_id
ORDER BY t.bank_account
感谢。
答案 0 :(得分:4)
Oracle安装程序:
CREATE TABLE my_table ( bank_account, bank_id, amount ) AS
SELECT 123, 600, 1500 FROM DUAL UNION ALL
SELECT 123, 600, 2500 FROM DUAL UNION ALL
SELECT 123, 600, 3500 FROM DUAL UNION ALL
SELECT 123, 700, 500 FROM DUAL UNION ALL
SELECT 123, 700, 1000 FROM DUAL UNION ALL
SELECT 456, 800, 2000 FROM DUAL UNION ALL
SELECT 456, 900, 2000 FROM DUAL UNION ALL
SELECT 456, 950, 4000 FROM DUAL;
<强>查询强>:
SELECT bank_account,
bank_id,
total_amount,
number_of_transactions,
SUM( number_of_transactions ) OVER ( PARTITION BY bank_account ) AS total_num_trans,
SUM( total_amount ) OVER ( PARTITION BY bank_account ) AS total_am_trans,
number_of_transactions
/ SUM( number_of_transactions ) OVER ( PARTITION BY bank_account )
* 100 AS percentage_trans,
total_amount
/ SUM( total_amount ) OVER ( PARTITION BY bank_account )
* 100 AS percentage_trans
FROM (
SELECT bank_account,
bank_id,
count(*) AS number_of_transactions,
sum(amount) AS total_amount
FROM my_table
GROUP BY bank_account
, bank_id
)
<强>输出强>:
BANK_ACCOUNT BANK_ID TOTAL_AMOUNT NUMBER_OF_TRANSACTIONS TOTAL_NUM_TRANS TOTAL_AM_TRANS PERCENTAGE_TRANS PERCENTAGE_TRANS
------------ ---------- ------------ ---------------------- --------------- -------------- ---------------- ----------------
123 600 7500 3 5 9000 60 83.3333333
123 700 1500 2 5 9000 40 16.6666667
456 800 2000 1 3 8000 33.3333333 25
456 900 2000 1 3 8000 33.3333333 25
456 950 4000 1 3 8000 33.3333333 50
答案 1 :(得分:0)
试试这个;)
select t1.*, t2.total_num_trans, t2.total_am_trans, (t1.number_of_transactions / t2.total_num_trans) * 100 as percentage_trans, (t1.total_amount / t2.total_am_trans) * 100 as percentage_amount
from (
select bank_account, bank_id, sum(amount) as total_amount, count(1) as number_of_transactions
from my_table
group by bank_account, bank_id) t1
left join (
select bank_account, sum(total_amount) as total_am_trans, sum(number_of_transactions) as total_num_trans
from (
select bank_account, bank_id, sum(amount) as total_amount, count(1) as number_of_transactions
from my_table
group by bank_account, bank_id ) t
group by bank_account ) t2 on t1.bank_account = t2.bank_account
order by t1.bank_account
答案 2 :(得分:0)
尝试加入2个聚合,粗略的只有银行帐户分组,更精细的分组也是银行ID。
SELECT tfine.bank_account
, tfine.bank_id
, tfine.total_amount
, tfine.number_of_transactions
, tcoarse.total_num_trans
, tcoarse.total_am_trans
FROM (
SELECT t1.bank_account
, t1.bank_id
, count(*) number_of_transactions
, sum(t1.amount) total_amount
FROM my_table t1
GROUP BY t1.bank_account
, t1.bank_id
) tfine
JOIN (
SELECT t2.bank_account
, count(*) total_num_trans
, sum(t2.amount) total_am_trans
FROM my_table t2
GROUP BY t2.bank_account
) tcoarse
ON tcoarse.bank_account = tfine.bank_account
ORDER BY tfine.bank_account
, tfine.bank_id
;
在线演示on ideone。