【发布时间】:2021-04-01 05:11:28
【问题描述】:
我正在尝试显示发票总输出成本的总和:这些是带有表示主键的斜体列的表: 客户:
| name | *cust#* |
| -------- | -------------- |
| Radio Spares | c1 |
| Pyramid| c2 |
项目:
| *item#* | iname | price |
| -------- | -------------- | ----------
| i1 | CD | 1 |
| i2 | DVD | 2 |
排名顺序:
| *so#* | cust# | item# |
| -------- | -------------- | ----------
| so1 | c1 | i1 |
| so2 | c2 | i2 |
发票:
| *in#* | cust# |
| -------- | -------------- |
| i1 | c1 |
| i2 | c2 |
交货:
| *d#* | in# |
| -------- | -------------- |
| d1 | 1 |
| d2 | 2 |
| d3 | 1 |
| d4 | 2 |
| d5 | 1 |
| d6 | 2 |
数量:
| so# | d# | quantity |
| -------- | -------------- | -----|
| d1 | 1 | 1|
| d2 | 2 | 2|
| d3 | 1 | 1|
| d4 | 2 | 2|
| d5 | 1 | 1|
| d6 | 2 | 2|
我要求:请求输出后的总成本之和:
distinct cu.name as customer_name,
dq.d# as delivery_no,
item.item# as item_no,
item.iname as description,
dq.quantity as quantity,
item.price as item_price,
item.price*dq.quantity as cost,
sum(item.price*dq.quantity) as sum
from customer cu
inner join standingorder sor on cu.cust#=sor.cust#
inner join dquantity dq on sor.order#=dq.order#
inner join invoice iv on cu.cust#=iv.cust#
inner join delivery dl on iv.inv#=dl.inv#
inner join item on sor.item#=item.item#
where cu.cust# = 'c2';
但是当我这样做时,我得到 ORA-00937: not a single-group group function。但如果我删除了
sum(item.price*dq.quantity) as sum
它运行但没有我需要的总和。 因此在这种情况下:其中 cu.cust# = 'c1' 的总和为 3,而 'c2' 的总和为 12。
我想看到的:对于 c2:
CUSTOMER_NAME DELIVE ITEM_N IName QUANTITY
------------------------ ------ ------ -------------------- ----------
ITEM_PRICE COST
---------- ----------
Pyramid D2 I2 DVD 2
2 4
D4 I2 DVD 2
2 4
D6 I2 DVD 2
2 4
Sum
---
12
【问题讨论】:
-
你需要一个 GROUP BY 子句。
-
显示您希望看到的结果会很有帮助。我不确定你想用
sum值做什么。
标签: sql oracle oracle12c sqlplus ora-00937