如何解决oracle按列求和而不使用并集
我有此表:(供应表:存储中有多少种产品)
Storage_id product_id amount
1 1000 55
1 1005 1
...
29 1000 3
29 1421 21
29 1566 0
30 1259 921
storage_id product_id amount
1 1000 55
2 1000 61
...
30 1000 10
total_except_storage_30 1000 1505
1 1001 1
2 1001 50
...
30 1001 56
total_except_storage_30 1001 1251
...
“ Total_except_storage_30”具有除存储号30之外的存储中所有产品的总数。例如,第一个“ total_except_storage_30”用于除storage_id 30以外的所有存储中的product_id 1000,第二个用于product_id 1001。
***我不允许使用“联盟”。
我尝试使用完全外部联接,但这没有用,结果是没有“ total_except_storage_30”:
Select t.Storage_id,t.product_id,t.amount
from myTable t full outer join
(
select 'total_except_storage_30' as storage_id,product_id,sum(amount)
from myTable
group by product_id
) total
on t.storage_id = total.storage_id
解决方法
这样的事情应该做
select
product,storage_id,sum(case when storage_id != 30 then sal end)
from scott.emp
group by grouping sets (
(storage_id,product),(product)
)
order by product,storage_id;
以下是使用标准EMP DEPT的示例
SQL> select
2 deptno,3 empno,4 sum(sal)
5 from scott.emp
6 group by grouping sets (
7 (empno,deptno),8 (deptno)
9 )
10 order by deptno,empno;
DEPTNO EMPNO SUM(SAL)
---------- ---------- ----------
10 7782 2450
10 7839 5000
10 7934 1300
10 8750
20 7369 800
20 7566 2975
20 7788 3000
20 7876 1100
20 7902 3000
20 10875
30 7499 1600
30 7521 1250
30 7654 1250
30 7698 2850
30 7844 1500
30 7900 950
30 9400
17 rows selected.
您可以看到整个小计
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。