Advertisement
Pandaaaa906

Untitled

Sep 20th, 2023
811
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 19.66 KB | None | 0 0
  1. -- 解决一个月多次入库,跨月多次入库,研发成本取数重复统计问题
  2. WITH year_ratio as (
  3. SELECT
  4. 1 n,
  5. cast(1.0 as float) ratio
  6.  
  7. UNION ALL
  8.  
  9. SELECT
  10. year_ratio.n + 1 n,
  11. ratio + power(0.7, year_ratio.n) ratio
  12. FROM year_ratio
  13. WHERE year_ratio.n < 10
  14. ),
  15. raw_records as (
  16.     SELECT
  17.     rd08_m.dDate rd08_ddate,
  18.     rd08_m.cCode rd08_code,
  19.     rd08_d.AutoID,
  20.     rd08_d.cInvCode,
  21.     rd08_d.cBatch,
  22.     rd08_d.cItemCode,
  23.     rd08_d.iQuantity,
  24.     rd08_d.iPrice,
  25.     rd08_d.cDefine30,
  26.     rd08_d.iQuantity / (SUM(rd08_d.iQuantity) OVER(partition by DATEADD(month, DATEDIFF(month, 0, rd08_m.dDate), 0), rd08_d.cItemCode)) ratio, -- 项目当月入库分摊比例
  27.     row_number() OVER(partition by rd08_d.cInvCode, rd08_d.cBatch ORDER BY rd08_m.dDate asc) batch_r,  -- 批次排序
  28.     row_number() OVER(partition by rd08_d.cItemCode ORDER BY rd08_m.dDate asc) prj_r  -- 项目编码排序
  29.     FROM rdrecords08_U8_009 rd08_d with(nolock)
  30.  
  31.     LEFT JOIN rdrecord08_U8_009 rd08_m with(nolock)
  32.     ON rd08_d.ID = rd08_m.ID
  33.  
  34.     WHERE cRdCode = '0110'
  35.     AND rd08_m.dnverifytime IS NOT NULL
  36. ), mid_records as (
  37.  
  38. SELECT
  39. raw_records.*,
  40. last_prj_record.rd08_ddate last_prj_rd08_ddate,
  41. SUM(prj_fee.total_fee) OVER(partition by DATEADD(month, DATEDIFF(month, 0, raw_records.rd08_ddate), 0), raw_records.cItemCode) prj_total_fee,
  42. SUM(prj_fee.raw_material_cost) OVER(partition by DATEADD(month, DATEDIFF(month, 0, raw_records.rd08_ddate), 0), raw_records.cItemCode)  * raw_records.ratio raw_material_cost, --按当月同项目入库数量分摊后的原料费用
  43. SUM(prj_fee.depreciation_fee) OVER(partition by DATEADD(month, DATEDIFF(month, 0, raw_records.rd08_ddate), 0), raw_records.cItemCode)  * raw_records.ratio depreciation_fee, --按当月同项目入库数量分摊后的折旧费用
  44. SUM(prj_fee.rent_utility_cost) OVER(partition by DATEADD(month, DATEDIFF(month, 0, raw_records.rd08_ddate), 0), raw_records.cItemCode)  * raw_records.ratio rent_utility_cost, --按当月同项目入库数量分摊后的租金水电费用
  45. SUM(prj_fee.labor_cost) OVER(partition by DATEADD(month, DATEDIFF(month, 0, raw_records.rd08_ddate), 0), raw_records.cItemCode)  * raw_records.ratio labor_cost, --按当月同项目入库数量分摊后的人工费用
  46. SUM(prj_fee.other_fee) OVER(partition by DATEADD(month, DATEDIFF(month, 0, raw_records.rd08_ddate), 0), raw_records.cItemCode)  * raw_records.ratio other_fee, --按当月同项目入库数量分摊后的其他费用
  47.  
  48. SUM(prj_fee.total_fee) OVER(partition by DATEADD(month, DATEDIFF(month, 0, raw_records.rd08_ddate), 0), raw_records.cItemCode)  * raw_records.ratio total_fee --按当月同项目入库数量分摊后的研发费用
  49. FROM raw_records
  50.  
  51. -- 查询上次同项目入库时间
  52. OUTER APPLY (
  53.     SELECT
  54.     rd08_ddate
  55.     FROM raw_records lst_record
  56.     WHERE lst_record.cItemCode = raw_records.cItemCode
  57.     AND lst_record.prj_r = raw_records.prj_r - 1
  58. )last_prj_record
  59.  
  60. OUTER APPLY (
  61.     SELECT
  62.     sum(case when item_type.type = '原料费用' then ledger.md else 0 end) raw_material_cost,
  63.     sum(case when item_type.type = '人工费' then ledger.md else 0 end) labor_cost,
  64.     sum(case when item_type.type = '租金水电费' then ledger.md else 0 end) rent_utility_cost,
  65.     sum(case when item_type.type = '折旧费用' then ledger.md else 0 end) depreciation_fee,
  66.     sum(case when item_type.type = '其他费用' then ledger.md else 0 end) other_fee,
  67.     sum(ledger.md) total_fee
  68.     FROM AuxiliaryLedger_U8_009 ledger with(nolock)
  69.    
  70.     OUTER APPLY (
  71.         SELECT TOP 1
  72.         c.type
  73.         FROM params_prj_item_cls c
  74.         WHERE c.code = ledger.ccode
  75.     ) item_type
  76.    
  77.     WHERE ledger.citem_class = '00'
  78.     AND ledger.citem_id = raw_records.cItemCode
  79.     AND ledger.cdigest not like '研发产品入库%'
  80.     AND ledger.dbill_date < DATEADD(month, DATEDIFF(month, 0, raw_records.rd08_ddate) + 1, 0) -- 取当月入库前的凭证
  81.     AND (
  82.         last_prj_record.rd08_ddate IS NULL
  83.         OR ledger.dbill_date >= DATEADD(month, DATEDIFF(month, 0, last_prj_record.rd08_ddate) + 1, 0)
  84.     )
  85. )prj_fee
  86.  
  87. ), records as (
  88.  
  89. SELECT
  90. mid_records.rd08_ddate,
  91. mid_records.rd08_code,
  92. mid_records.cDefine30,
  93. mid_records.AutoID,
  94. mid_records.cInvCode,
  95. mid_records.cBatch,
  96. mid_records.cItemCode,
  97. mid_records.ratio,
  98. -- mid_records.iQuantity,
  99. -- mid_records.iPrice,
  100. count(*) OVER(partition by mid_records.cInvCode, mid_records.cBatch) rd08_count,
  101. sum(mid_records.iQuantity) OVER(partition by mid_records.cInvCode, mid_records.cBatch) iQuantity,
  102. sum(mid_records.iPrice) OVER(partition by mid_records.cInvCode, mid_records.cBatch) iPrice,
  103.  
  104. sum(mid_records.raw_material_cost) OVER(partition by mid_records.cInvCode, mid_records.cBatch) raw_material_cost,
  105. sum(mid_records.labor_cost) OVER(partition by mid_records.cInvCode, mid_records.cBatch) labor_cost,
  106. sum(mid_records.rent_utility_cost) OVER(partition by mid_records.cInvCode, mid_records.cBatch) rent_utility_cost,
  107. sum(mid_records.other_fee) OVER(partition by mid_records.cInvCode, mid_records.cBatch) other_fee,
  108. sum(mid_records.depreciation_fee) OVER(partition by mid_records.cInvCode, mid_records.cBatch) depreciation_fee,
  109.  
  110. sum(mid_records.total_fee) OVER(partition by mid_records.cInvCode, mid_records.cBatch) total_fee
  111. FROM mid_records
  112. WHERE mid_records.batch_r = 1
  113. )
  114.  
  115. SELECT
  116. records.rd08_ddate 最早研发入库时间,
  117. records.rd08_code 最早入库单号,
  118. records.rd08_count 入库次数,
  119. records.AutoID 入库ID,
  120. records.cInvCode "研发入库货号-原料",
  121. SUBSTRING(records.cInvCode, 3, len(records.cInvCode)) 研发入库货号,
  122. inv_cls.prd_line 产品线,
  123. records.cBatch 入库批次,
  124. records.iQuantity 批次总入库数量,
  125. records.iPrice 批次总入库成本,
  126. records.cDefine30 备货类型,
  127. records.ratio ratio,
  128.  
  129. records.raw_material_cost 原料成本,
  130. records.labor_cost 人工成本,
  131. records.rent_utility_cost 租金水电成本,
  132. records.depreciation_fee 折旧成本,
  133. records.other_fee 其他成本,
  134. records.total_fee 研发总成本,
  135.  
  136. inq_summary.last_year_inq_count 近一年询价次数,
  137. inq_summary.so_inq_rate 产品成单率,
  138. inq_summary.last_year_so_inq_rate 近一年成单率,
  139. inq_summary_after_instock.total_inq_count 研发入库后总询价次数,
  140. inq_summary_after_instock.last_year_inq_count 研发入库后近一年询价次数,
  141. inq_summary_after_instock.so_inq_rate 研发入库后产品成单率,
  142. inq_summary_after_instock.last_year_so_inq_rate 研发入库后近一年成单率,
  143.  
  144. so_summary.last_year_so_count 近一年销售次数,
  145. so_summary_after_instock.so_count 研发入库后销售次数,
  146. so_summary_after_instock.so_sum 研发入库后销售含税合计,
  147. so_summary_after_instock.last_year_so_count 研发入库后近一年销售次数,
  148. so_summary_after_instock.last_year_so_sum 研发入库后近一年销售含税合计,
  149. batch_so_summary.so_count 批次销售次数,
  150. batch_so_summary.rd11_iquantity "批次出库数量(mg)",
  151. batch_so_summary.rd32_iSum 批次出库金额,
  152. batch_so_summary.cur_month_rd32_isum 批次入库当月出库金额,
  153. batch_so_summary.rd32_min_ddate 批次最早销售出库日期,
  154.  
  155. prd_rd32_summary_after_instock.total_count 研发入库后产品出库次数,
  156. prd_rd32_summary_after_instock.rd11_cost 研发入库后产品出库成本,
  157. prd_rd32_summary_after_instock.total_sum 研发入库后产品出库金额,
  158. prd_rd32_summary_after_instock.total_mg "研发入库后产品出库数量(mg)",
  159. prd_rd32_summary_after_instock.last_year_count 研发入库后近一年产品出库次数,
  160. prd_rd32_summary_after_instock.last_year_sum 研发入库后近一年产品出库金额,
  161. prd_rd32_summary_after_instock.last_year_mg_sum "研发入库后近一年产品出库数量(mg)",
  162.  
  163. po_summary.last_year_po_avg_unit_price 上一年采购平均单价,
  164. po_summary.po_avg_unit_price 采购平均单价,
  165.  
  166. case when inq_summary_after_instock.total_inq_count > 0 or so_summary_after_instock.so_count>0 then 1 else 0 end 是否触碰,
  167. case when so_summary_after_instock.so_count > 0 then 1 else 0 end 是否销售,
  168. case when batch_so_summary.rd32_iSum > 0 then 1 else 0 end 是否出库,
  169. case when batch_so_summary.cur_month_rd32_isum > 0 then 1 else 0 end 当月是否出库,
  170. history_so.cFree1 历史常卖规格,
  171. dbo.normalize_mg(history_so.cFree1) "历史常卖规格(mg)",
  172. history_so.iTaxUnitPrice 历史常卖规格含税单价,
  173. history_so.so_count_1year 入库前一年销售次数,
  174. history_so.so_count_1year * history_so.iTaxUnitPrice 预计一年销售金额,
  175. processed.predict_sale_year * history_so.so_count_1year 预计销售次数,
  176. processed.predict_sale_year * history_so.so_count_1year * history_so.iTaxUnitPrice "预计销售金额(旧)",
  177. coalesce(year_ratio.ratio, processed.predict_sale_year) * history_so.so_count_1year * history_so.iTaxUnitPrice 预计销售金额,
  178.  
  179. records.iPrice / records.iQuantity * batch_so_summary.rd11_iquantity 批次累计销售成本,
  180. records.total_fee / records.iQuantity * batch_so_summary.rd11_iquantity 批次累计销售研发成本,
  181. current_stock.iQuantity 剩余库存
  182.  
  183. FROM records
  184.  
  185. -- 产品线
  186. OUTER apply (
  187.     SELECT TOP 1
  188.     case inv.cInvCCode
  189.     when '01021005' then '药物线'
  190.     when '01011006' then '非药线'
  191.     when '01031001' then '中间体'
  192.     when '03031001' then '中间体'
  193.     when '0901' then '原料'
  194.     else '其他' END prd_line
  195.     FROM Inventory_U8_009 inv with(nolock)
  196.     WHERE inv.cInvCode = records.cInvCode
  197. )inv_cls
  198.  
  199. -- 询价统计
  200. OUTER apply (
  201.     SELECT
  202.     count(*) inq_count,
  203.     case count(*) when 0 then null else cast(count(inq_so.id) as float) / count(*) end so_inq_rate,
  204.     sum(case when inq.inq_created_at >= DATEADD(year, -1, getdate()) then 1 else 0 end) last_year_inq_count,
  205.     case sum(case when inq.inq_created_at >= DATEADD(year, -1, getdate()) then 1 else 0 end) when 0 then null
  206.     else cast(sum(case when inq_so.id is not null AND inq.inq_created_at >= DATEADD(year, -1, getdate()) then 1 else 0 end) as float) / sum(case when inq.inq_created_at >= DATEADD(year, -1, getdate()) then 1 else 0 end) end last_year_so_inq_rate
  207.  
  208.     FROM prod_inq_result inq with(nolock)
  209.     OUTER APPLY (
  210.         SELECT TOP 1
  211.         inq_sync.id,
  212.         inq_sync.update_time
  213.        
  214.         FROM cato_platform.t_inq_sync_crm_detail inq_sync with(nolock)
  215.         INNER JOIN SO_SODetails_U8_009 so_d with(nolock)
  216.         ON so_d.iSOsID = inq_sync.sal_order_detail_id
  217.         WHERE inq_sync.inq_order_detail_id = inq.inq_id
  218.     )inq_so
  219.     WHERE inq.new_cat_no = substring(records.cinvcode, 3, LEN(records.cinvcode))
  220. ) inq_summary
  221.  
  222. OUTER apply (
  223.     SELECT
  224.     count(*) total_inq_count,
  225.     case count(*) when 0 then null else cast(count(inq_so.id) as float) / count(*) end so_inq_rate,
  226.     sum(case when inq.inq_created_at >= DATEADD(year, -1, getdate()) then 1 else 0 end) last_year_inq_count,
  227.     case sum(case when inq.inq_created_at >= DATEADD(year, -1, getdate()) then 1 else 0 end) when 0 then null
  228.     else cast(sum(case when inq_so.id is not null AND inq.inq_created_at >= DATEADD(year, -1, getdate()) then 1 else 0 end) as float) / sum(case when inq.inq_created_at >= DATEADD(year, -1, getdate()) then 1 else 0 end) end last_year_so_inq_rate
  229.     FROM prod_inq_result inq with(nolock)
  230.    
  231.     OUTER APPLY (
  232.         SELECT TOP 1
  233.         inq_sync.id,
  234.         inq_sync.update_time
  235.        
  236.         FROM cato_platform.t_inq_sync_crm_detail inq_sync with(nolock)
  237.         INNER JOIN SO_SODetails_U8_009 so_d with(nolock)
  238.         ON so_d.iSOsID = inq_sync.sal_order_detail_id
  239.         WHERE inq_sync.inq_order_detail_id = inq.inq_id
  240.     )inq_so
  241.    
  242.     WHERE inq.new_cat_no = substring(records.cinvcode, 3, LEN(records.cinvcode))
  243.     AND inq.inq_created_at >= records.rd08_ddate
  244. ) inq_summary_after_instock
  245.  
  246. -- 销售订单统计
  247. OUTER apply (
  248.     SELECT
  249.     SUM(case when so_m.dverifysystime >= DATEADD(year, -1, getdate()) then 1 else 0 end) last_year_so_count
  250.     FROM SO_SODetails_U8_009 so_d with(nolock)
  251.     LEFT JOIN SO_SOMain_U8_009 so_m with(nolock)
  252.     ON so_d.ID = so_m.ID
  253.     WHERE 1=1
  254.     AND so_m.dverifysystime is not null
  255.     AND (so_d.cSCloser is null or so_d.cSCloser = 'asuser')
  256.     AND so_d.cInvCode = substring(records.cinvcode, 3, LEN(records.cinvcode))
  257. ) so_summary
  258.  
  259. OUTER apply (
  260.     SELECT
  261.     count(*) so_count,
  262.     SUM(so_d.iSum) so_sum,
  263.     SUM(case when so_m.dverifysystime >= DATEADD(year, -1, getdate()) then 1 else 0 end) last_year_so_count,
  264.     SUM(case when so_m.dverifysystime >= DATEADD(year, -1, getdate()) then so_d.iSum else 0 end) last_year_so_sum
  265.     FROM SO_SODetails_U8_009 so_d with(nolock)
  266.     LEFT JOIN SO_SOMain_U8_009 so_m with(nolock)
  267.     ON so_d.ID = so_m.ID
  268.     WHERE 1=1
  269.     AND (so_d.cSCloser is null or so_d.cSCloser = 'asuser')
  270.     AND so_d.cInvCode = substring(records.cinvcode, 3, LEN(records.cinvcode))
  271.     AND so_m.dverifysystime >= records.rd08_ddate
  272. ) so_summary_after_instock
  273.  
  274. -- 批次累计出库统计
  275. OUTER apply (
  276.     SELECT
  277.     count(distinct so_union.isosid) so_count,
  278.     SUM(rd32_iSum) rd32_iSum,
  279.     min(so_union.rd32_ddate) rd32_min_ddate,
  280.     SUM(so_union.cur_month_rd32_isum) cur_month_rd32_isum,
  281.     SUM(so_union.rd11_iquantity) rd11_iquantity
  282.    
  283.     FROM (
  284.     SELECT
  285.     so_d.isosid,
  286.     so_d.iSum / so_d.iQuantity * rd32_d.iQuantity rd32_iSum,
  287.     rd32_m.dDate rd32_ddate,
  288.     case
  289.     when datepart(year, rd32_m.dDate) = datepart(year, records.rd08_ddate) AND datepart(month, rd32_m.dDate) = datepart(month, records.rd08_ddate)
  290.     then so_d.iSum / so_d.iQuantity * rd32_d.iQuantity else 0 end cur_month_rd32_isum,
  291.     rd11_d.iQuantity rd11_iquantity
  292.    
  293.     FROM rdrecords11_U8_009 rd11_d with(nolock)
  294.  
  295.     LEFT JOIN rdrecord11_U8_009 rd11_m with(nolock)
  296.     ON rd11_d.ID = rd11_m.ID
  297.  
  298.     LEFT JOIN mom_moallocate_U8_009 mom_allo with(nolock)
  299.     ON mom_allo.AllocateId = rd11_d.iMPoIds
  300.  
  301.     LEFT JOIN rdrecords10_U8_009 rd10_d with(nolock)
  302.     ON rd10_d.iMPoIds = mom_allo.MoDId
  303.  
  304.     LEFT JOIN rdrecords32_U8_009 rd32_d with(nolock)
  305.     ON rd32_d.cInvCode = rd10_d.cInvCode
  306.     AND rd32_d.cBatch = rd10_d.cBatch
  307.     AND rd32_d.cFree1 = rd10_d.cFree1
  308.    
  309.     LEFT JOIN rdrecord32_U8_009 rd32_m with(nolock)
  310.     ON rd32_m.ID = rd32_d.ID
  311.  
  312.     LEFT JOIN SO_SODetails_U8_009 so_d with(nolock)
  313.     ON so_d.isosid = rd32_d.iorderdid
  314.  
  315.     WHERE rd11_m.dDate >= records.rd08_ddate
  316.     AND rd11_d.cInvCode = records.cinvcode
  317.     AND rd11_d.cBatch = records.cBatch
  318.    
  319.     UNION
  320.     -- 形态转换单后做的销售出库
  321.     SELECT
  322.     so_d.isosid,
  323.     so_d.iSum / so_d.iQuantity * rd32_d.iQuantity rd32_iSum,
  324.     rd32_m.dDate rd32_ddate,
  325.     case
  326.     when datepart(year, rd32_m.dDate) = datepart(year, records.rd08_ddate) AND datepart(month, rd32_m.dDate) = datepart(month, records.rd08_ddate)
  327.     then so_d.iSum / so_d.iQuantity * rd32_d.iQuantity else 0 end cur_month_rd32_isum,
  328.     rd11_d.iQuantity rd11_iquantity
  329.    
  330.     FROM rdrecords09_U8_009 rd09_d with(nolock)
  331.    
  332.     LEFT JOIN RdRecord09_U8_009 rd09_m with(nolock)
  333.     ON rd09_m.ID = rd09_d.ID
  334.  
  335.     LEFT JOIN RdRecord08_U8_009 rd08_m with(nolock)
  336.     ON rd08_m.cBusCode = rd09_m.cBusCode
  337.  
  338.     LEFT JOIN RdRecords08_U8_009 rd08_d with(nolock)
  339.     ON rd08_m.ID = rd08_d.ID
  340.     AND rd08_d.iGroupNO = rd09_d.iGroupNO
  341.    
  342.     LEFT JOIN rdrecords11_U8_009 rd11_d with(nolock)
  343.     ON rd08_d.cInvCode = rd11_d.cInvCode
  344.     AND rd08_d.cBatch = rd11_d.cBatch
  345.     AND rd08_d.cFree1 = rd11_d.cFree1
  346.  
  347.     LEFT JOIN rdrecord11_U8_009 rd11_m with(nolock)
  348.     ON rd11_d.ID = rd11_m.ID
  349.  
  350.     LEFT JOIN mom_moallocate_U8_009 mom_allo with(nolock)
  351.     ON mom_allo.AllocateId = rd11_d.iMPoIds
  352.  
  353.     LEFT JOIN rdrecords10_U8_009 rd10_d with(nolock)
  354.     ON rd10_d.iMPoIds = mom_allo.MoDId
  355.  
  356.     LEFT JOIN rdrecords32_U8_009 rd32_d with(nolock)
  357.     ON rd32_d.cInvCode = rd10_d.cInvCode
  358.     AND rd32_d.cBatch = rd10_d.cBatch
  359.     AND rd32_d.cFree1 = rd10_d.cFree1
  360.    
  361.     LEFT JOIN rdrecord32_U8_009 rd32_m with(nolock)
  362.     ON rd32_m.ID = rd32_d.ID
  363.  
  364.     LEFT JOIN SO_SODetails_U8_009 so_d with(nolock)
  365.     ON so_d.isosid = rd32_d.iorderdid
  366.  
  367.     WHERE rd09_m.cSource = '形态装换'
  368.     AND rd09_d.cInvCode = records.cInvCode
  369.     AND rd09_d.cBatch = records.cBatch
  370.    
  371.     ) so_union
  372. ) batch_so_summary
  373.  
  374. OUTER APPLY (
  375.     SELECT
  376.     count(*) total_count,
  377.     sum(so_d.iSum / so_d.iQuantity * rd32_d.iQuantity) total_sum,
  378.     sum(dbo.normalize_mg(rd32_d.CFree1) * rd32_d.iQuantity) total_mg,
  379.     sum(rd11_cost.iPrice) rd11_cost,
  380.     sum(case when rd32_m.dnverifytime >= DATEADD(year, -1, getdate()) then 1 else 0 end) last_year_count,
  381.     sum(case when rd32_m.dnverifytime >= DATEADD(year, -1, getdate()) then so_d.iSum / so_d.iQuantity * rd32_d.iQuantity else 0 end) last_year_sum,
  382.     sum(case when rd32_m.dnverifytime >= DATEADD(year, -1, getdate()) then dbo.normalize_mg(rd32_d.CFree1) * rd32_d.iQuantity else 0 end) last_year_mg_sum
  383.     FROM rdrecords32_U8_009 rd32_d with(nolock)
  384.    
  385.     LEFT JOIN rdrecord32_U8_009 rd32_m with(nolock)
  386.     ON rd32_d.ID = rd32_m.ID
  387.    
  388.     LEFT JOIN SO_SODetails_U8_009 so_d with(nolock)
  389.     ON so_d.iSOsID = rd32_d.iorderdid
  390.    
  391.     OUTER APPLY (
  392.         SELECT
  393.         sum(rd11_d.iPrice) iPrice
  394.         FROM rdrecords10_U8_009 rd10_d with(nolock)
  395.        
  396.         LEFT JOIN mom_moallocate_U8_009 mom_allo with(nolock)
  397.         ON rd10_d.iMPoIds = mom_allo.MoDId
  398.        
  399.         LEFT JOIN rdrecords11_U8_009 rd11_d with(nolock)
  400.         ON mom_allo.AllocateId = rd11_d.iMPoIds
  401.        
  402.         WHERE rd10_d.cInvCode = rd32_d.cInvCode
  403.         AND rd10_d.cBatch = rd32_d.cBatch
  404.     ) rd11_cost
  405.    
  406.     WHERE 1=1
  407.     AND rd32_m.dnverifytime >= records.rd08_ddate
  408.     AND rd32_d.cInvCode = SUBSTRING(records.cInvCode, 3, len(records.cInvCode))
  409. ) prd_rd32_summary_after_instock
  410.  
  411. OUTER apply (
  412.     SELECT TOP 1
  413.     tmp.cFree1,
  414.     tmp.iTaxUnitPrice,
  415.     tmp.so_count_1year
  416.     FROM (
  417.         SELECT
  418.         so_d.cFree1,
  419.         so_d.iTaxUnitPrice,
  420.         so_m.dverifysystime,
  421.         SUM(so_count.v) over(partition by so_d.cFree1) so_count_1year,  -- 入库前一年销售数量
  422.         count(*) over(partition by so_d.cFree1) c
  423.         FROM SO_SODetails_U8_009 so_d with(nolock)
  424.         LEFT JOIN SO_SOMain_U8_009 so_m with(nolock)
  425.         ON so_d.ID = so_m.ID
  426.        
  427.         OUTER APPLY(
  428.             SELECT CASE WHEN so_m.dverifysystime >= dateadd(year, -1, records.rd08_ddate) THEN 1 else 0 END v
  429.         )so_count
  430.        
  431.         WHERE so_m.dverifysystime <= records.rd08_ddate
  432.         AND (so_d.csCloser IS NULL OR so_d.csCloser='asuser')
  433.         AND so_d.cInvCode = SUBSTRING(records.cInvCode, 3, len(records.cInvCode))
  434.     ) tmp
  435.     ORDER BY tmp.c desc, tmp.dverifysystime desc
  436.  
  437. ) history_so
  438.  
  439. OUTER APPLY (
  440. SELECT
  441. avg(case when po_m.cAuditTime >= dateadd(year, -1, getdate()) then po_d.iSum / po_d.iQuantity else null end) last_year_po_avg_unit_price,
  442. avg(po_d.iSum / po_d.iQuantity) po_avg_unit_price
  443. FROM PO_Podetails_U8_009 po_d with(nolock)
  444. LEFT JOIN PO_Pomain_U8_009 po_m with(nolock)
  445. ON po_d.POID = po_m.POID
  446. WHERE 1=1
  447. AND po_m.cAuditTime is not null
  448. AND (po_d.cbCloser IS NULL OR po_d.cbCloser='asuser')
  449. AND po_d.cInvCode = records.cInvCode
  450.  
  451. )po_summary
  452.  
  453. -- 剩余库存(考虑形态转换) current_stock
  454. OUTER APPLY (
  455.    
  456.     SELECT
  457.     sum(iQuantity) iQuantity
  458.     FROM (
  459.         SELECT
  460.         stock.cInvCode,
  461.         stock.cBatch,
  462.         stock.iQuantity
  463.         FROM InvPositionSum_U8_009 stock WITH(nolock)
  464.         WHERE stock.cInvCode = records.cInvCode
  465.         AND stock.cBatch = records.cBatch
  466.        
  467.         UNION
  468.        
  469.         SELECT
  470.         stock.cInvCode,
  471.         stock.cBatch,
  472.         stock.iQuantity
  473.        
  474.         FROM rdrecords09_U8_009 rd09_d with(nolock)
  475.        
  476.         LEFT JOIN RdRecord09_U8_009 rd09_m with(nolock)
  477.         ON rd09_m.ID = rd09_d.ID
  478.  
  479.         LEFT JOIN RdRecord08_U8_009 rd08_m with(nolock)
  480.         ON rd08_m.cBusCode = rd09_m.cBusCode
  481.  
  482.         LEFT JOIN RdRecords08_U8_009 rd08_d with(nolock)
  483.         ON rd08_m.ID = rd08_d.ID
  484.         AND rd08_d.iGroupNO = rd09_d.iGroupNO
  485.        
  486.         INNER JOIN InvPositionSum_U8_009 stock WITH(nolock)
  487.         ON stock.cInvCode = rd08_d.cInvCode
  488.         AND stock.cBatch = rd08_d.cBatch
  489.         AND stock.cInvCode LIKE 'M-%'
  490.  
  491.         WHERE rd09_m.cSource = '形态装换'
  492.         AND rd09_d.cInvCode = records.cInvCode
  493.         AND rd09_d.cBatch = records.cBatch
  494.     )union_stock
  495.  
  496. ) current_stock
  497.  
  498. OUTER APPLY (
  499. SELECT
  500. case
  501. when dbo.normalize_mg(history_so.cFree1) = 0 OR history_so.so_count_1year = 0 then NULL
  502. when (records.iQuantity / dbo.normalize_mg(history_so.cFree1) / history_so.so_count_1year)>4 then 4
  503. else records.iQuantity / dbo.normalize_mg(history_so.cFree1) / history_so.so_count_1year
  504. end predict_sale_year  -- 预计销售年数
  505. )processed
  506.  
  507. OUTER APPLY (
  508. SELECT
  509. ratio
  510. FROM year_ratio
  511. WHERE year_ratio.n = floor(processed.predict_sale_year)
  512. )year_ratio
  513.  
  514.  
  515. ORDER BY records.rd08_ddate desc
  516.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement