如何使用 oracle sql select 语句将价格列求和到另一个总计列?
how to sum price column to another total column use oracle sql select statement?
我可以知道如何使用基于 invoice_no 的总价列和 total_amount 列中的总计以及
每行的 total_amount 值始终是每个 invoice_no 的总和。 (例如 invoice_no 10001 total_amount 是 150.00)
table_a
invoice_no. items price qty
10001 apple 50.00 1
10001 papaya 100.00 1
10002 water melon 50.00 1
10002 orange 50.00 1
我的查询
select invoice_no,items,price,qty from (select invoice_no,items,price,qty from table_a where invoice_no='10001' union all
select null,null,null,sum(price) from table_a where invoice_no='10001') table_a;
我的输出
invoice_no. items price qty
10001 apple 50.00 1
10001 papaya 100.00 1
150.00
正确结果
invoice_no. items total_amount price qty
10001 apple 150.00 50.00 1
10001 papaya 150.00 100.00 1
10002 water melon 100.00 50.00 1
10002 orange 100.00 50.00 1
这里可以使用SUM()
作为解析函数:
SELECT
invoice_no,
items,
SUM(price) OVER (PARTITION BY invoice_no) AS total_amount,
price,
qty
FROM table_a;
我可以知道如何使用基于 invoice_no 的总价列和 total_amount 列中的总计以及 每行的 total_amount 值始终是每个 invoice_no 的总和。 (例如 invoice_no 10001 total_amount 是 150.00)
table_a
invoice_no. items price qty
10001 apple 50.00 1
10001 papaya 100.00 1
10002 water melon 50.00 1
10002 orange 50.00 1
我的查询
select invoice_no,items,price,qty from (select invoice_no,items,price,qty from table_a where invoice_no='10001' union all
select null,null,null,sum(price) from table_a where invoice_no='10001') table_a;
我的输出
invoice_no. items price qty
10001 apple 50.00 1
10001 papaya 100.00 1
150.00
正确结果
invoice_no. items total_amount price qty
10001 apple 150.00 50.00 1
10001 papaya 150.00 100.00 1
10002 water melon 100.00 50.00 1
10002 orange 100.00 50.00 1
这里可以使用SUM()
作为解析函数:
SELECT
invoice_no,
items,
SUM(price) OVER (PARTITION BY invoice_no) AS total_amount,
price,
qty
FROM table_a;