avg(decode(t.t_region, ‘华北‘, t.t_money, null))和avg(decode(t.t_region, ‘华北‘, t.t_money, 0))区别

区别分析:两种DECODE表达式在AVG函数中的差异

这两个表达式都用于计算华北地区的平均金额,但处理非华北地区记录的方式不同,这会影响最终结果:

表达式1:avg(decode(t.t_region, '华北', t.money, null))

  • 处理逻辑
    • 当t_region为’华北’时,返回t_money值
    • 对于其他地区,返回NULL
  • 计算方式
    • 只对华北地区的t_money值进行平均
    • 非华北地区的记录完全被排除在计算之外(不参与计数也不参与求和)
  • 结果
    • 分子:华北地区所有t_money的总和
    • 分母:华北地区的记录数

表达式2:avg(decode(t.t_region, '华北', t.money, 0))

  • 处理逻辑
    • 当t_region为’华北’时,返回t_money值
    • 对于其他地区,返回0
  • 计算方式
    • 华北地区的t_money值被保留
    • 非华北地区的记录以0值参与计算
  • 结果
    • 分子:华北地区所有t_money的总和
SQL 错误 [903] [42000]: ORA-00903: 表名无效 Error : 903, Position : 2432, SQL = -- AX_OMP2024.V_ORDER_TRACK source CREATE OR REPLACE FORCE VIEW "AX_OMP2024"."V_ORDER_TRACK" ("ID", "FOFFERNUM", "FCCONTACTDATE", "FSELLER", "FSELLERNAME", "FCUSTOMNAME", "FCUSTOMERNUM", "FPRODUCTMODEL", "FQUANTITY", "FSEGMENTDESCRIPTION", "FORIGINALPRICETAX", "FORIGINALTOTALPRICETAX", "FDELIVERYDATEPLAN", "FOEMFLAG", "FNEEDPRODUCT", "FSTATUS", "FCONTRACTTYPE", "FITEMCHOOSE", "FDESCRIPTION", "FCUSTOMERMODEL", "FLINEID", "FUNIQUELINEID", "FERPLINENUMBER", "SUM_LEN_COMM_STORE", "SUM_SHIP_QTY", "SUM_INVOICE_QTY", "PRE_INVOICE_QTY","FLASTFHDATE", "SUM_LEN_COMM_OUT", "ISSHIPDONE", "ISPRODUCTDONE", "STOREAGE", "ITEM_CORE") AS with t_wms as ( select in_order_no, item_no, sum(t.len_comm) sum_len_comm_store from T_in_store_cab_from_WMS901 t where t.status = '在库' group by in_order_no, item_no ), t_wms_out as (--本地 --select in_order_no, item_no, sum(t.len_comm) sum_len_comm_out --from T_in_store_cab_from_WMS901 t where t.status = '已出库' group by in_order_no, item_no select FContractNo in_order_no, ITEM_CODE item_no, ERPLineNumber order_line_number, sum(t.quantity) sum_len_comm_out, max(TRANSACTION_DATE) TRANSACTION_DATE from T_OMP_Store_Out t where t.ftype = 1 group by t.FContractNo, t.ITEM_CODE, ERPLineNumber ), t_po_out as(--外协出库 select FContractNo in_order_no, ITEM_CODE item_no, ERPLineNumber order_line_number, sum(t.quantity) sum_len_comm_out, max(TRANSACTION_DATE) TRANSACTION_DATE from T_OMP_Store_Out t where t.ftype = 2 group by t.FContractNo, t.ITEM_CODE, t.ERPLineNumber ), t_ship as( select a.header_id, a.crm_line_number, sum(a.FApplyQuantity) sum_ship_qty from T_CRM_Delivery_Order_F a left join T_CRM_Delivery_Order b on a.t_crm_delivery_order_id = b.id where b.fstatus != -99 group by a.header_id, a.crm_line_number ), t_invoice as( select sum(t.FInvoiceQTY) sum_invoice_qty, t.FOrderHeadId, t.FUniqueLineID from T_CRM_INVOICE_LINETY t, T_CRM_INVOICE_HEADTY t1 where t.T_CRM_INVOICE_HEADTY_id = t1.id and t1.FState =100 and t1.fapplyType != 1 group by t.FOrderHeadId, t.FUniqueLineID ), t_invoice_pre as( select sum(t.FInvoiceQTY) sum_invoice_qty, t.FOrderHeadId, t.FUniqueLineID from T_CRM_INVOICE_LINETY t, T_CRM_INVOICE_HEADTY t1 where t.T_CRM_INVOICE_HEADTY_id = t1.id and t1.FState = 90 and t1.fapplyType = 1 group by t.FOrderHeadId, t.FUniqueLineID ), with t_wms_details as ( SELECT MAX(TT.OUT_TIME) ,tt.IN_ORDER_NO ,TT.ITEM_NO FROM SV_WMS_SHIP_DETAILS tt GROUP BY tt.IN_ORDER_NO ,TT.ITEM_NO ) select DISTINCT a.id, b.foffernum, b.fccontactdate,b.fseller, i.name fsellerName, h.fcompanyname fcustomname, b.fcustomernum, a.fproductmodel, a.fquantity, a.fsegmentdescription, round(a.FOriginalPriceTax, 4) FOriginalPriceTax, a.FOriginalTotalPriceTax, a.FDeliveryDatePlan, a.FOEMFlag, a.FNeedProduct, b.fstatus, b.fcontracttype, c.item_code fitemchoose, c.item_desc FDescription, a.FCustomerModel, a.flineid, a.funiquelineid, a.FERPLineNumber, d.sum_len_comm_store, e.sum_ship_qty,--发货指令数量 f.sum_invoice_qty, g.sum_invoice_qty pre_invoice_qty, tw.OUT_TIME, --decode(a.FOEMFlag, 1, d2.sum_len_comm_out, d1.sum_len_comm_out)sum_len_comm_out, --(nvl(d2.sum_len_comm_out, 0) + nvl(d1.sum_len_comm_out, 0)) sum_len_comm_out, (nvl(d3.sum_len_comm_out, nvl(d2.sum_len_comm_out, 0)) + nvl(d1.sum_len_comm_out, 0)) sum_len_comm_out, case when ((nvl(d3.sum_len_comm_out, nvl(d2.sum_len_comm_out, 0))+ nvl(d1.sum_len_comm_out, 0))) >= a.fquantity then 'Y' else 'N' end isShipDone, case when (nvl(d.sum_len_comm_store, 0) + (nvl(d3.sum_len_comm_out, nvl(d2.sum_len_comm_out, 0)) + nvl(d1.sum_len_comm_out, 0)) ) >= a.fquantity then 'Y' else 'N' end isProductDone, --发货数量大于开票数量才计算库龄 case when (nvl(e.sum_ship_qty, 0) - nvl(f.sum_invoice_qty, 0)) > 0 then round(sysdate - nvl(d1.TRANSACTION_DATE, d2.TRANSACTION_DATE), 0) end storeAge, TO_NUMBER(SUBSTR(c.item_code, 8, 4)) item_core from T_CRM_Orders_FDetail a left join T_CRM_Orders b on a.T_CRM_Orders_id = b.id left join T_ITEM_Info c on c.ids = a.fitemchoose left join t_wms d on d.in_order_no = b.foffernum and c.item_code = d.item_no --本地生产 left join t_wms_out d1 on d1.in_order_no = b.foffernum and c.item_code = d1.item_no and d1.order_line_number = a.FERPLineNumber left join t_po_out d2 on d2.in_order_no||'' = b.foffernum and c.item_code = d2.item_no --and d2.order_line_number = a.FERPLineNumber -- and (d2.order_line_number is null or (d2.order_line_number is not null and d2.order_line_number = a.FERPLineNumber)) --太慢了 left join t_po_out d3 on d3.in_order_no||'' = b.foffernum and c.item_code = d3.item_no and d3.order_line_number = a.FERPLineNumber left join t_ship e on e.header_id = b.FHeaderID and e.crm_line_number = a.funiquelineid left join t_invoice f on f.FOrderHeadId = b.id and f.FUniqueLineID = a.funiquelineid left join t_invoice_pre g on g.FOrderHeadId = b.id and g.FUniqueLineID = a.funiquelineid left join t_crm_customer h on h.id = b.fcustomname left join tuser i on i.id = b.FSeller left join t_wms_details tw ON tw.IN_ORDER_NO = b.foffernum AND tw.ITEM_NO = c.item_code where b.fcontracttype != 10008, Original SQL = -- AX_OMP2024.V_ORDER_TRACK source CREATE OR REPLACE FORCE VIEW "AX_OMP2024"."V_ORDER_TRACK" ("ID", "FOFFERNUM", "FCCONTACTDATE", "FSELLER", "FSELLERNAME", "FCUSTOMNAME", "FCUSTOMERNUM", "FPRODUCTMODEL", "FQUANTITY", "FSEGMENTDESCRIPTION", "FORIGINALPRICETAX", "FORIGINALTOTALPRICETAX", "FDELIVERYDATEPLAN", "FOEMFLAG", "FNEEDPRODUCT", "FSTATUS", "FCONTRACTTYPE", "FITEMCHOOSE", "FDESCRIPTION", "FCUSTOMERMODEL", "FLINEID", "FUNIQUELINEID", "FERPLINENUMBER", "SUM_LEN_COMM_STORE", "SUM_SHIP_QTY", "SUM_INVOICE_QTY", "PRE_INVOICE_QTY","FLASTFHDATE", "SUM_LEN_COMM_OUT", "ISSHIPDONE", "ISPRODUCTDONE", "STOREAGE", "ITEM_CORE") AS with t_wms as ( select in_order_no, item_no, sum(t.len_comm) sum_len_comm_store from T_in_store_cab_from_WMS901 t where t.status = '在库' group by in_order_no, item_no ), t_wms_out as (--本地 --select in_order_no, item_no, sum(t.len_comm) sum_len_comm_out --from T_in_store_cab_from_WMS901 t where t.status = '已出库' group by in_order_no, item_no select FContractNo in_order_no, ITEM_CODE item_no, ERPLineNumber order_line_number, sum(t.quantity) sum_len_comm_out, max(TRANSACTION_DATE) TRANSACTION_DATE from T_OMP_Store_Out t where t.ftype = 1 group by t.FContractNo, t.ITEM_CODE, ERPLineNumber ), t_po_out as(--外协出库 select FContractNo in_order_no, ITEM_CODE item_no, ERPLineNumber order_line_number, sum(t.quantity) sum_len_comm_out, max(TRANSACTION_DATE) TRANSACTION_DATE from T_OMP_Store_Out t where t.ftype = 2 group by t.FContractNo, t.ITEM_CODE, t.ERPLineNumber ), t_ship as( select a.header_id, a.crm_line_number, sum(a.FApplyQuantity) sum_ship_qty from T_CRM_Delivery_Order_F a left join T_CRM_Delivery_Order b on a.t_crm_delivery_order_id = b.id where b.fstatus != -99 group by a.header_id, a.crm_line_number ), t_invoice as( select sum(t.FInvoiceQTY) sum_invoice_qty, t.FOrderHeadId, t.FUniqueLineID from T_CRM_INVOICE_LINETY t, T_CRM_INVOICE_HEADTY t1 where t.T_CRM_INVOICE_HEADTY_id = t1.id and t1.FState =100 and t1.fapplyType != 1 group by t.FOrderHeadId, t.FUniqueLineID ), t_invoice_pre as( select sum(t.FInvoiceQTY) sum_invoice_qty, t.FOrderHeadId, t.FUniqueLineID from T_CRM_INVOICE_LINETY t, T_CRM_INVOICE_HEADTY t1 where t.T_CRM_INVOICE_HEADTY_id = t1.id and t1.FState = 90 and t1.fapplyType = 1 group by t.FOrderHeadId, t.FUniqueLineID ), with t_wms_details as ( SELECT MAX(TT.OUT_TIME) ,tt.IN_ORDER_NO ,TT.ITEM_NO FROM SV_WMS_SHIP_DETAILS tt GROUP BY tt.IN_ORDER_NO ,TT.ITEM_NO ) select DISTINCT a.id, b.foffernum, b.fccontactdate,b.fseller, i.name fsellerName, h.fcompanyname fcustomname, b.fcustomernum, a.fproductmodel, a.fquantity, a.fsegmentdescription, round(a.FOriginalPriceTax, 4) FOriginalPriceTax, a.FOriginalTotalPriceTax, a.FDeliveryDatePlan, a.FOEMFlag, a.FNeedProduct, b.fstatus, b.fcontracttype, c.item_code fitemchoose, c.item_desc FDescription, a.FCustomerModel, a.flineid, a.funiquelineid, a.FERPLineNumber, d.sum_len_comm_store, e.sum_ship_qty,--发货指令数量 f.sum_invoice_qty, g.sum_invoice_qty pre_invoice_qty, tw.OUT_TIME, --decode(a.FOEMFlag, 1, d2.sum_len_comm_out, d1.sum_len_comm_out)sum_len_comm_out, --(nvl(d2.sum_len_comm_out, 0) + nvl(d1.sum_len_comm_out, 0)) sum_len_comm_out, (nvl(d3.sum_len_comm_out, nvl(d2.sum_len_comm_out, 0)) + nvl(d1.sum_len_comm_out, 0)) sum_len_comm_out, case when ((nvl(d3.sum_len_comm_out, nvl(d2.sum_len_comm_out, 0))+ nvl(d1.sum_len_comm_out, 0))) >= a.fquantity then 'Y' else 'N' end isShipDone, case when (nvl(d.sum_len_comm_store, 0) + (nvl(d3.sum_len_comm_out, nvl(d2.sum_len_comm_out, 0)) + nvl(d1.sum_len_comm_out, 0)) ) >= a.fquantity then 'Y' else 'N' end isProductDone, --发货数量大于开票数量才计算库龄 case when (nvl(e.sum_ship_qty, 0) - nvl(f.sum_invoice_qty, 0)) > 0 then round(sysdate - nvl(d1.TRANSACTION_DATE, d2.TRANSACTION_DATE), 0) end storeAge, TO_NUMBER(SUBSTR(c.item_code, 8, 4)) item_core from T_CRM_Orders_FDetail a left join T_CRM_Orders b on a.T_CRM_Orders_id = b.id left join T_ITEM_Info c on c.ids = a.fitemchoose left join t_wms d on d.in_order_no = b.foffernum and c.item_code = d.item_no --本地生产 left join t_wms_out d1 on d1.in_order_no = b.foffernum and c.item_code = d1.item_no and d1.order_line_number = a.FERPLineNumber left join t_po_out d2 on d2.in_order_no||'' = b.foffernum and c.item_code = d2.item_no --and d2.order_line_number = a.FERPLineNumber -- and (d2.order_line_number is null or (d2.order_line_number is not null and d2.order_line_number = a.FERPLineNumber)) --太慢了 left join t_po_out d3 on d3.in_order_no||'' = b.foffernum and c.item_code = d3.item_no and d3.order_line_number = a.FERPLineNumber left join t_ship e on e.header_id = b.FHeaderID and e.crm_line_number = a.funiquelineid left join t_invoice f on f.FOrderHeadId = b.id and f.FUniqueLineID = a.funiquelineid left join t_invoice_pre g on g.FOrderHeadId = b.id and g.FUniqueLineID = a.funiquelineid left join t_crm_customer h on h.id = b.fcustomname left join tuser i on i.id = b.FSeller left join t_wms_details tw ON tw.IN_ORDER_NO = b.foffernum AND tw.ITEM_NO = c.item_code where b.fcontracttype != 10008, Error Message = ORA-00903: 表名无效 ORA-00903: 表名无效
10-29
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值