0

我正在尝试显示发票总输出成本的总和:这些是带有表示主键的斜体列的表:客户:

| 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
4

2 回答 2

0

所有未聚合的列都必须包含在GROUP BY子句中。例如(一个简单的):

这就是你现在所拥有的:

SQL> select deptno, job, sum(sal)
  2  from emp
  3  /
select deptno, job, sum(sal)
       *
ERROR at line 1:
ORA-00937: not a single-group group function

添加GROUP BY子句:

SQL> select deptno, job, sum(sal)
  2  from emp
  3  group by deptno ,job;

    DEPTNO JOB         SUM(SAL)
---------- --------- ----------
        20 CLERK            920
        30 SALESMAN        5600
        20 MANAGER         2975
        30 CLERK            950
        10 PRESIDENT      10000
        30 MANAGER         2850
        20 PRESIDENT       1100
        10 CLERK           1300
        10 MANAGER         2450
        20 ANALYST         6000

10 rows selected.

或者,看看sum- 在它的分析形式 - 做你想要的:

SQL> select deptno, job,
  2    sum(sal) over (partition by deptno order by null) sumsal
  3  from emp;

    DEPTNO JOB           SUMSAL
---------- --------- ----------
        10 MANAGER        13750
        10 PRESIDENT      13750
        10 CLERK          13750
        20 MANAGER        10995
        20 ANALYST        10995
        20 PRESIDENT      10995
        20 CLERK          10995
        20 ANALYST        10995
        30 SALESMAN        9400
        30 SALESMAN        9400
        30 SALESMAN        9400
        30 CLERK           9400
        30 MANAGER         9400
        30 SALESMAN        9400

14 rows selected.

SQL>
于 2020-12-22T13:43:44.437 回答
0

你似乎需要一个group by

select cu.name as customer_name, dq.d# as delivery_no,
       sum(item.price*dq.quantity) as sum
from . . .
where cu.cust# = 'c2'
group by cu.name, dq.d#;

这将返回每次交付的总数。它会删除所有项目特定信息——因为这可能会返回太多行。

如果您想要每个客户的总数,请同时删除交付:

select cu.name as customer_name
       sum(item.price*dq.quantity) as sum
from . . .
where cu.cust# = 'c2'
group by cu.name;

编辑:

如果您只想要所有行的总数(相同),请使用窗口/分析函数:

select . . . , -- all the columns you want
       sum(item.price * dq.quantity) over (partition by cu.cust#) as customer_total
from . . .
于 2020-12-22T13:43:04.327 回答