【问题标题】:How to show the sum of the total results and the result each of the output sql如何显示总结果的总和以及每个输出sql的结果
【发布时间】: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


【解决方案1】:

所有未聚合的列必须包含在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>

【讨论】:

    【解决方案2】:

    你似乎需要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 . . .
    

    【讨论】:

    • 我要做的是显示输出中缺少的结果的总和,通过执行您给它的操作,只会给我一个 ORA-00979 错误,而不是按表达式分组。你能进一步澄清吗?
    • 我在编辑中尝试了您的代码,但对于 cust#='c2',customer_total 给出的不是 12,您能澄清一下吗?
    • @JoeLiu 。 . .您的代码可能会产生重复,这可能是由于一个订单的多次交付。
    • 这正是我想要的,但我只希望总和是成本的总和
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-10
    相关资源
    最近更新 更多