恐怕你把大家都搞糊涂了。)

虽然你的要求部分难以理解,但我认为如果我必须处理这类任务,我会做一件事。我将编写递归函数,计算从树的任何部分到叶的成本。

以下是与您类似的数据演示:

select prod.*, level, sys_connect_by_path(seq, '->') path,

calc_cost(comp) total

from prod connect by prior comp = part

start with base = 1;

SEQ PART COMP QTY COST CURR BASE AFO RFO LEVEL PATH TOTAL

------ ---- ---- ---------- ---------- ---- ---------- --- --- ------- ----------- ----------

1 A A1 5 1 1 ->1 850

2 A1 A11 3 0 B 2 ->1->2 114

3 A11 A111 4 2 EUR 0 B;D 3 ->1->2->3 8

4 A11 A112 2 15 EUR 0 3 ->1->2->4 30

5 A1 A12 8 7 EUR 0 2 ->1->5 56

11 B B1 5 1 1 ->11 870

12 B1 B11 3 0 2 ->11->12 174

13 B11 B111 4 12 GBP 0 3 ->11->12->13 48

14 B11 B112 2 5 GBP 0 3 ->11->12->14 10

total

包含组件成本,例如

B1

它是

5 * (3 * (4 * 12 + 2 * 5))

哪个是

870

.

函数和示例数据如下:

create or replace function calc_cost(i_comp in varchar2) return number is

v_qty number := 0;

v_cost number := 0;

begin

select qty, cost into v_qty, v_cost from prod where comp = i_comp;

if v_cost is null then

select sum(calc_cost(comp)) into v_cost from prod where part = i_comp;

end if;

return v_qty * v_cost;

exception when no_data_found then

return 0;

end;

数据:

create table prod(seq, part, comp, qty, cost, curr, base, afo, rfo) as (

select 1, 'A', 'A1', 5, null, null, 1, null, null from dual union all

select 2, 'A1', 'A11', 3, null, null, 0, 'B', null from dual union all

select 3, 'A11', 'A111', 4, 2, 'EUR', 0, null, 'B;D' from dual union all

select 4, 'A11', 'A112', 2, 15, 'EUR', 0, null, null from dual union all

select 5, 'A1', 'A12', 8, 7, 'EUR', 0, null, null from dual union all

select 11, 'B', 'B1', 5, null, null, 1, null, null from dual union all

select 12, 'B1', 'B11', 3, null, null, 0, null, null from dual union all

select 13, 'B11', 'B111', 4, 12, 'GBP', 0, null, null from dual union all

select 14, 'B11', 'B112', 2, 5, 'GBP', 0, null, null from dual );

您没有指定相同的货币是否可以有不同的货币

part

/

component

如果是这样的话,输出会是怎样的。不管怎样,你可以找到这些货币,然后独立地计算每种货币。您需要向函数添加第二个参数,并编写类似

where part = i_comp and curr = i_curr or curr is null

.

也适用于

ask_for_option

/

remove_for_option

你也许可以在

case when

.

我看到你在这个问题上付出了很大的努力,但在目前的问题形式下,很难更好地回答。您应该提供示例数据,而不仅仅是图像,并根据用户的选择向我们准确显示您期望的输出。

但我希望这个函数可以帮助您解决这个问题。我假设如果

cost

不为空,则我们位于叶中,否则函数将递归查找子组件。

编辑:

假设seq=14是欧元而不是英镑。

update prod set curr = 'EUR' where seq = 14;

正如我所说,精确的解决方案取决于您需要的输出。如果您知道所有可能的货币,那么您可以修改函数来处理货币和显示成本,如下所示:

create or replace function calc_cost(i_comp in varchar2, i_curr in varchar2)

return number is

v_qty number := 0;

v_cost number := 0;

begin

select qty, cost into v_qty, v_cost from prod

where comp = i_comp and (curr = i_curr or curr is null);

if v_cost is null then

select sum(calc_cost(comp, i_curr)) into v_cost

from prod where part = i_comp and (curr = i_curr or curr is null);

end if;

return v_qty * nvl(v_cost, 0);

exception when no_data_found then

return 0;

end;

select seq, part, comp, qty, cost, curr,

calc_cost(comp, 'EUR') eur, calc_cost(comp, 'GBP') gbp

from prod

connect by part = prior comp

start with part = 'B';

SEQ PART COMP QTY COST CURR EUR GBP

----- ---- ---- ---------- ---------- ---- ---------- ----------

11 B B1 5 150 720

12 B1 B11 3 30 144

13 B11 B111 4 12 GBP 0 48

14 B11 B112 2 5 EUR 10 0

部分

B

成本为150欧元和720英镑。

您可以在数据的有趣部分中找到所有不同的货币,将它们与表连接起来,然后这样调用函数。结果是每个人

seq

你可以得到和不同货币一样多的行。然后你可以用

listagg()

和现值

150 EUR; 720 GBP

在一个单元格中。

您还可以创建一些类型对象并修改函数,以返回元组表(货币,成本单位为欧元)。问题是您希望如何显示数据。或者可以将值转换为通用货币,但为此需要每日的比率表。

oracle 三列数值相加,Oracle SQL/PLSQL:按货币拆分和求和值的分层查询相关推荐

  1. python dataframe列数值相加,python合并dataframe中的行并将值相加

    我想这会有帮助.这段代码可以处理任意数量的连续"T",甚至可以更改要组合的字符.我在代码中添加了注释来解释它的作用.在import pandas as pd def combine ...

  2. mysql 数据相加_mysql,php_mysql查询将两列数值相加问题,mysql,php,sql - phpStudy

    mysql查询将两列数值相加问题 如图所示: 我希望加一个total_price列,值为price+price2的和,这个应该怎么写SQL: select * from ims_goods_1 whe ...

  3. oracle 表列 自增,ORACLE表建立自增列

    create tablespace studentDB datafile 'E:\datafiles_1.dbf' size 10m; create user Huang_Ying_Bo identi ...

  4. oracle 修改列类型6,Oracle用户、权限、角色管理 编辑

    Oracle 数据库用户管理 Oracle 权限设置 一.权限分类: 系统权限:系统规定用户使用数据库的权限.(系统权限是对用户而言). 实体权限:某种权限用户对其它用户的表或视图的存取权限.(是针对 ...

  5. oracle 前导列_通过 PL/SQL Developer (Oracle)-数据库(26)

    本篇文章介绍了跟SQL语句性能提升有关的执行计划,工作时间长了,或者说高手的进阶途径之一,就是如何能够在数据量很大的情况下,数据库的查询效率还能保持良好的性能. 感兴趣的朋友,可以收藏这篇文章哦,未来 ...

  6. oracle 伪列访问序列,Oracle数据库对象,同义词、序列、视图、索引

    数据库对象简介 Oracle 数据库对象又称模式对象 数据库对象是逻辑结构的集合,最基本的数据库对象是表 其他数据库对象包括: 同义词是现有对象的一个别名. 简化SQL语句 隐藏对象的名称和所有者 提 ...

  7. oracle 三个口令管理,Oracle学习笔记(12)口令和资源管理

    口令和资源管理 1.Profiles: 概要文件,包含一些对口令和资源限制的一个命名的集合.通过CREATE USER 或 ALTER USER 命令来指定用户.它可以是enabled 或 disab ...

  8. oracle三种权限级别,ORACLE FGAC(细粒度权限控制)

    --主要完成ORACLE fine-grained access control , 以及使用存储过程进行权限封装 /*Oracle fine-grained access control 可以实现数 ...

  9. oracle数据列转行排序,oracle 列转行函数 WMSYS.WM_CONCAT 排序不规则处理

    业务中做报表,需要将一列列数据汇总成一行,然后汇总,如下: 需要将每个产品进行汇总,通过ichartjs进行展示,图表中需要数据的顺序是: var data = [ { name : '产品1', v ...

最新文章

  1. JavaScript(八)
  2. eclipse 添加 server library
  3. ios 分类(Category)
  4. 肝!2500字 字符串专题总结
  5. Association, Composition and Aggregation in UI5, CRM, S/4HANA and C4C
  6. Redmine for windows 一键安装
  7. Exchange笔记之Exchange Server 2003前端后端部署
  8. devexpress 打印一个form界面_通过回车键提交form表单时,你是否注意过这些问题?...
  9. 微课|中学生可以这样学Python(例3.1):闰年判断
  10. 【损失函数】Focal Loss for Dense Object Detection And RetinaNet
  11. [系统安全] 十五.Chrome密码保存功能渗透解析、Chrome蓝屏漏洞及音乐软件漏洞复现
  12. ubuntu彻底卸载Nvidia显卡驱动
  13. html 让360浏览器兼容模式,360浏览器兼容模式怎么设置?360浏览器兼容模式设置方法介绍...
  14. VBA中调用Excel函数
  15. 前端SVG实现各式图片和动画
  16. ML-czy的小组任务
  17. can not be represented as java.sql.Timestamp
  18. 非常详尽,多图慎入:Wayland与Weston简介
  19. 此实现不是 Windows 平台 FIPS 验证的加密算法的一部分解决办法
  20. Yao‘s GC 的通信最优解:Half Gate

热门文章

  1. 写在中国雅虎关闭之后
  2. ASP.NET应用程序设计的10大技巧
  3. copy构造函数使用深copy
  4. python 遍历目录或文件
  5. C++面试八股文快问快答の基础篇
  6. Spring Boot静态资源访问和配置全解析
  7. LIVE555再学习 -- testH264VideoStreamer 源码分析
  8. 1053 Path of Equal Weight
  9. zcmu-1783(01字典树)
  10. 深大转专业计算机,这所高校2020年1042人申请转专业!申请转出人数最多的竟是医学部...