如何显示总结果和每个输出sql的结果的总和

问题描述

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

| 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:不是单组功能。但如果我删除

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

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 . . .
,

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

这就是你现在拥有的:

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

添加 GROUP BY 子句:

SQL> select deptno,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,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>