编辑代码

-- 二级组件
with ads_inventory_now as(
select 
   mat_products_1_category_name,
  "在库" as inventory_type ,
  snpsht_date, -- 日期
  last_reciv_date, -- 上次收货日期
  case when 
  DATEDIFF(snpsht_date,last_reciv_date) <=30 then "0~1月"
  when DATEDIFF(snpsht_date,last_reciv_date) >30  and DATEDIFF(snpsht_date,last_reciv_date)<=90 then "1月~3月"
  when DATEDIFF(snpsht_date,last_reciv_date) >90  and DATEDIFF(snpsht_date,last_reciv_date)<=180 then "3月~6月"
  when DATEDIFF(snpsht_date,last_reciv_date) >180  and DATEDIFF(snpsht_date,last_reciv_date)<=365 then "6月~1年"
  when DATEDIFF(snpsht_date,last_reciv_date) >365  and DATEDIFF(snpsht_date,last_reciv_date)<=730 then "1年~2年"
  when DATEDIFF(snpsht_date,last_reciv_date) >730  and DATEDIFF(snpsht_date,last_reciv_date)<=1095 then "2年~3年"
  else "其他" end dt_dis, -- 库龄分布
    faty_code, -- 工厂编码
    warhs_lgort_code, -- 库位编码
    mat_code, -- 物料编码
    long_desc_txt, -- 物料长描述
    mat_batch_code, -- 批次号
    base_name,
    product_category_name, -- 停产待售/在产在售
    main_product_type_name, -- 主流/非主流
  is_regular_flag,
    home_or_abroad_txt, --  海内外
    region_dept_name, -- 地域部
    area_name, -- 区域
    utility_distributed_name, -- 集中式/分布式
    module_type_name, -- 组件类型(单双玻)
    module_type_code, -- 组件型号
    pattern_vulgo_name, -- 版型俗称
    is_oem_flag, -- 是否代工
    is_rd_flag, -- 是否研发
  is_shipped_flag, -- 是否可发库存
  is_shipp_domestic_overseas_flag, -- 是否可发海外
    SUBSTRING(module_size_code, 1, LOCATE('×',module_size_code, LOCATE('×', module_size_code) + 1) - 1) module_size_code, 
    module_levl_code, -- 组件等级
    watt_per_pic_amt, -- 组件功率
    module_pattern_code, -- 组件版型  
    inventory_age_qty, -- 库龄
  case when '${单位}' = 'WPCS' then (umlme_pc_qty+bwesb_pc_qty)/10000
    when '${单位}' = 'MW' then ifnull(watt_per_pic_amt,0)*(umlme_pc_qty+bwesb_pc_qty)/1000000
    else (umlme_pc_qty+bwesb_pc_qty) end transit, -- 在运 + 评估的收货锁定
  case when '${单位}' = 'WPCS' then full_pc_qty/10000
    when '${单位}' = 'MW' then ifnull(watt_per_pic_amt,0)*full_pc_qty/1000000
    else full_pc_qty end full_num, -- 单位判断
    full_amt, -- 全库存(金额),
    material_cost_amt, -- 全库存材料成本
    case when '${单位}' = 'WPCS' then last_year_full_pc_qty/10000
    when '${单位}' = 'MW' then ifnull(watt_per_pic_amt,0)*last_year_full_pc_qty/1000000
    else last_year_full_pc_qty end last_year_full_pc_qty,-- 去年数量/金额
  last_year_full_amt,
  last_year_dead_amt,
  last_year_inventory_turnover_days_qty,
  last_year_material_cost_amt,
  -- last_half_year_full_pc_qty,
    case when '${单位}' = 'WPCS' then last_half_year_full_pc_qty/10000
    when '${单位}' = 'MW' then ifnull(watt_per_pic_amt,0)*last_half_year_full_pc_qty/1000000
    else last_half_year_full_pc_qty end last_half_year_full_pc_qty,-- 上半年数量/金额  
  last_half_year_full_amt,
  last_half_year_dead_amt,
  last_half_year_inventory_turnover_days_qty,
  last_half_year_material_cost_amt,
  -- before_last_m_full_pc_qty,
    case when '${单位}' = 'WPCS' then before_last_m_full_pc_qty/10000
    when '${单位}' = 'MW' then ifnull(watt_per_pic_amt,0)*before_last_m_full_pc_qty/1000000
    else before_last_m_full_pc_qty end before_last_m_full_pc_qty,-- 上上月数量/金额    
  before_last_m_full_amt,
  before_last_m_dead_amt,
  before_last_m_inventory_turnover_days_qty,
  before_last_m_material_cost_amt,
  -- last_m_full_pc_qty,
    case when '${单位}' = 'WPCS' then last_m_full_pc_qty/10000
    when '${单位}' = 'MW' then ifnull(watt_per_pic_amt,0)*last_m_full_pc_qty/1000000
    else last_m_full_pc_qty end last_m_full_pc_qty,-- 上月数量/金额   
  last_m_full_amt,
  last_m_dead_amt,
  last_m_inventory_turnover_days_qty,
  last_m_material_cost_amt,
  -- this_m_full_pc_qty,
    case when '${单位}' = 'WPCS' then this_m_full_pc_qty/10000
    when '${单位}' = 'MW' then ifnull(watt_per_pic_amt,0)*this_m_full_pc_qty/1000000
    else this_m_full_pc_qty end this_m_full_pc_qty,-- 当月数量/金额   
  this_m_full_amt,
  this_m_dead_amt,
  this_m_inventory_turnover_days_qty,
  this_m_material_cost_amt,
  -- last_7_d_full_pc_qty,
    case when '${单位}' = 'WPCS' then last_7_d_full_pc_qty/10000
    when '${单位}' = 'MW' then ifnull(watt_per_pic_amt,0)*last_7_d_full_pc_qty/1000000
    else last_7_d_full_pc_qty end last_7_d_full_pc_qty,-- 第四周数量   
  last_7_d_full_amt,
  last_7_d_dead_amt,
  last_7_d_inventory_turnover_days_qty,
  last_7_d_material_cost_amt,
    case when '${单位}' = 'WPCS' then last_2_w_full_pc_qty/10000
    when '${单位}' = 'MW' then ifnull(watt_per_pic_amt,0)*last_2_w_full_pc_qty/1000000
    else last_2_w_full_pc_qty end last_2_w_full_pc_qty,-- 第三周数量   
  last_2_w_full_amt,
  last_2_w_dead_amt,
  last_2_w_inventory_turnover_days_qty,
  last_2_w_material_cost_amt,
    case when '${单位}' = 'WPCS' then last_3_w_full_pc_qty/10000
    when '${单位}' = 'MW' then ifnull(watt_per_pic_amt,0)*last_3_w_full_pc_qty/1000000
    else last_3_w_full_pc_qty end last_3_w_full_pc_qty,-- 第二周数量   
  last_3_w_full_amt,
  last_3_w_dead_amt,
  last_3_w_inventory_turnover_days_qty,
  last_3_w_material_cost_amt,
    case when '${单位}' = 'WPCS' then last_4_w_full_pc_qty/10000
    when '${单位}' = 'MW' then ifnull(watt_per_pic_amt,0)*last_4_w_full_pc_qty/1000000
    else last_4_w_full_pc_qty end last_4_w_full_pc_qty,-- 第一周数
  last_4_w_full_amt,
  last_4_w_dead_amt,
  last_4_w_inventory_turnover_days_qty,
  last_4_w_material_cost_amt
from 
    dws_full_inventory_rgalfudy 
where 
  snpsht_date between date_add(CURDATE(),interval -6 day) and CURDATE()
and  mat_products_1_category_name in ("组件成品","电池片")
union all 
select 
    mat_group_category_name,
    "在途至客户" as inventory_type ,
    dt, -- 日期
    "", -- 上次收货日期
    0, -- 库龄分布
    faty_code, -- 工厂编码
    warhs_lgort_code, -- 库位
    mat_code, -- 物料
    long_desc_txt, -- 物料描述
    "", -- 批次号
    base_name, -- 基地名称
    product_category_name,
    "主流" as main_product_type_name,
    is_regular_flag,-- main_product_type_name
    home_or_abroad_txt, --  海内外
    region_dept_name, -- 地域部
    area_name, -- 区域
    utility_distributed_name, -- 集中式/分布式
    module_type_name, -- 组件类型(单双玻)
    module_type_code, -- 组件型号
    pattern_vulgo_name, -- 版型俗称
    is_oem_flag, -- 是否代工
    is_rd_flag, -- 是否研发
  "在途至客户", -- 是否可发库存
  "正常订单(在途)", -- 国内库存备注
    SUBSTRING(replace(module_size_code,"*","×"), 1, LOCATE('×',replace(module_size_code,"*","×"), LOCATE('×', replace(module_size_code,"*","×")) + 1) - 1) module_size_code, 
    module_levl_code, -- 组件等级
    ifnull(in_transit_inventory_watt_qty/in_transit_inventory_pc_qty,""), -- 功率
    module_pattern_code, -- 组件版型  
    0, -- 库龄"
  	0 , -- 在运 + 评估的收货锁定
  	case when '${单位}' = 'WPCS' then in_transit_inventory_pc_qty/10000
    when '${单位}' = 'MW' then ifnull(in_transit_inventory_watt_qty,0)/1000000
    else in_transit_inventory_pc_qty end full_num, -- 单位判断
    in_transit_inventory_amt, -- 全库存(金额),
    0, -- 全库存材料成本
    case when '${单位}' = 'PCS' then last_year_in_transit_inventory_pc_qty/10000
    when '${单位}' = 'MW' then last_year_in_transit_inventory_watt_qty/1000000
    else last_year_in_transit_inventory_pc_qty/10000 end last_year_in_transit_inventory_pc_qty, -- 去年总库存
    last_year_in_transit_inventory_amt, -- 去年总金额
    0 , -- 呆滞金额
    0, -- 存货周转天数
    0, -- 去年物料成本
    case when '${单位}' = 'PCS' then last_half_year_in_transit_inventory_pc_qty/10000
    when '${单位}' = 'MW' then last_half_year_in_transit_inventory_watt_qty/1000000
    else last_half_year_in_transit_inventory_pc_qty/10000 end last_half_year_in_transit_inventory_pc_qty, -- -- 上半年全库存
    last_half_year_in_transit_inventory_amt, -- 上半年总金额
    0, -- 上半年呆滞金额
    0, -- 上半年存货周转
    0, -- 上半年物料成本
    case when '${单位}' = 'PCS' then before_last_m_in_transit_inventory_pc_qty/10000
    when '${单位}' = 'MW' then before_last_m_in_transit_inventory_watt_qty/1000000
    else before_last_m_in_transit_inventory_pc_qty/10000 end before_last_m_in_transit_inventory_pc_qty, -- -- 上上月总库存
    before_last_m_in_transit_inventory_amt, -- 上上月总金额
    0, -- 上上月呆滞
    0, -- 上上月存货
    0, -- 上上月物料
    case when '${单位}' = 'PCS' then last_m_in_transit_inventory_pc_qty/10000
    when '${单位}' = 'MW' then last_m_in_transit_inventory_watt_qty/1000000
    else last_m_in_transit_inventory_pc_qty/10000 end last_m_in_transit_inventory_pc_qty, -- -- 上月全库存
    last_m_in_transit_inventory_amt, -- 上月总金额
    0, -- 上月呆滞
    0, -- 上月存货
    0, -- 上上月物料
    case when '${单位}' = 'PCS' then this_m_in_transit_inventory_pc_qty/10000
    when '${单位}' = 'MW' then this_m_in_transit_inventory_watt_qty/1000000
    else this_m_in_transit_inventory_pc_qty/10000 end this_m_in_transit_inventory_pc_qty, -- -- 当月全库存
    this_m_in_transit_inventory_amt, -- 当月总金额
    0, -- 当月呆滞
    0, -- 当月存货
    0, -- 当月物料
    case when '${单位}' = 'PCS' then last_7_d_in_transit_inventory_pc_qty/10000
    when '${单位}' = 'MW' then last_7_d_in_transit_inventory_watt_qty/1000000
    else last_7_d_in_transit_inventory_pc_qty/10000 end last_7_d_in_transit_inventory_pc_qty, -- -- 近七天全库存
    last_7_d_in_transit_inventory_amt, -- 近七天总金额
    0, -- 第四周呆滞
    0, -- 第四周存货
    0, -- 第四周物料
    case when '${单位}' = 'PCS' then last_2_w_in_transit_inventory_pc_qty/10000
    when '${单位}' = 'MW' then last_2_w_in_transit_inventory_watt_qty/1000000
    else last_2_w_in_transit_inventory_pc_qty/10000 end last_2_w_in_transit_inventory_pc_qty, -- -- 近两周总库存
    last_2_w_in_transit_inventory_amt, -- 近两周总金额
    0, -- 第三周呆滞
    0, -- 第三周存货
    0, -- 第三周物料
    case when '${单位}' = 'PCS' then last_3_w_in_transit_inventory_pc_qty/10000
    when '${单位}' = 'MW' then last_3_w_in_transit_inventory_watt_qty/1000000
    else last_3_w_in_transit_inventory_pc_qty/10000 end last_3_w_in_transit_inventory_pc_qty, -- -- 近三周总库存
    last_3_w_in_transit_inventory_amt, -- 近三周总金额
    0, -- 第二周呆滞
    0, -- 第二周存货
    0, -- 第二周物料
    case when '${单位}' = 'PCS' then last_4_w_in_transit_inventory_pc_qty/10000
    when '${单位}' = 'MW' then last_4_w_in_transit_inventory_watt_qty/1000000
    else last_4_w_in_transit_inventory_pc_qty/10000 end last_4_w_in_transit_inventory_pc_qty, -- -- 近四周总库存
    last_4_w_in_transit_inventory_amt, -- 近四周总金额
    0, -- 第一周呆滞
    0, -- 第一周存货
    0 -- 第一周物料
from 
    aiko_dm_isc.dwd_in_transit_inventory_rgalfudy
where 
  dt between date_add(CURDATE(),interval -6 day) and CURDATE()
and  mat_group_category_name in ("组件成品") -- 计算资金占用天数,组件销售成本+PERC/TOPCON电池片的销售成本
union all 
select 
	mat_group_category_name,
    "在制" as inventory_type ,
    dt, -- 日期
    "", -- 上次收货日期
    0, -- 库龄分布
    faty_code, -- 工厂编码
    0, -- 库位
    mat_code, -- 物料
    "", -- 物料描述
    "", -- 批次号
    case when faty_code = 2000 then "义乌七厂" 
    when faty_code = 3000 then "天津"
    when faty_code = 1000 then "佛山"
    when faty_code = 8020 then "滁州"
    when faty_code = 5000 then "珠海"
    when faty_code = 8010 then "济南" else "其他" end base_name, -- 基地名称
    "在产在售",-- main_product_type_name
    "主流" ,-- 主流/非主流
    "常规" , -- 常规/非常规
    "在制", --  海内外
    "在制", -- 地域部
    "在制", -- 区域
    utility_distributed_name, -- 集中式/分布式
    module_type_name, -- 组件类型(单双玻)
    module_type_code, -- 组件型号
    pattern_vulgo_name, -- 版型俗称
    is_oem_flag, -- 是否代工
    is_rd_flag, -- 是否研发
  "在制", -- 是否可发库存
  "在制", -- 国内库存备注
    "在制", -- 组件尺寸 
    "在制", -- 组件等级
    "", -- 功率
    module_pattern_code, -- 组件版型  
    0, -- 库龄"
    0 , -- 在运 + 评估的收货锁定
    case when '${单位}' = 'WPCS' then work_in_progress_pc_qty/10000
    when '${单位}' = 'MW' then 0
    else work_in_progress_pc_qty end full_num, -- 在制品数量
    work_in_progress_amt, -- 全库存(金额),
    case when '${库存类型}' = "在制" then raw_material_cost_amt else 0 end raw_material_cost_amt, -- 全库存材料成本,增加控件筛选在制
    case when '${单位}' = 'PCS' then last_year_work_in_progress_pc_qty/10000
    when '${单位}' = 'MW' then 0
    else last_year_work_in_progress_pc_qty/10000 end last_year_work_in_progress_pc_qty,-- 去年总库存
    last_year_work_in_progress_amt, -- 去年总金额
    0 , -- 呆滞金额
    0, -- 存货周转天数
    case when '${库存类型}' = "在制" then last_year_raw_material_cost_amt else 0 end last_year_raw_material_cost_amt, -- 去年物料成本
    case when '${单位}' = 'PCS' then last_half_year_work_in_progress_pc_qty/10000
    when '${单位}' = 'MW' then 0
    else last_half_year_work_in_progress_pc_qty/10000 end last_half_year_work_in_progress_pc_qty,-- 上半年全库存
    last_half_year_work_in_progress_amt, -- 上半年总金额
    0, -- 上半年呆滞金额
    0, -- 上半年存货周转
    case when '${库存类型}' = "在制" then last_half_year_raw_material_cost_amt else 0 end last_half_year_raw_material_cost_amt, -- 上半年物料成本
    case when '${单位}' = 'PCS' then before_last_m_work_in_progress_pc_qty/10000
    when '${单位}' = 'MW' then 0
    else before_last_m_work_in_progress_pc_qty/10000 end before_last_m_work_in_progress_pc_qty,-- 上上月总库存
    before_last_m_work_in_progress_amt, -- 上上月总金额
    0, -- 上上月呆滞
    0, -- 上上月存货
    case when '${库存类型}' = "在制" then before_last_m_raw_material_cost_amt else 0 end before_last_m_raw_material_cost_amt, -- 上上月物料
    case when '${单位}' = 'PCS' then last_m_work_in_progress_pc_qty/10000
    when '${单位}' = 'MW' then 0
    else last_m_work_in_progress_pc_qty/10000 end last_m_work_in_progress_pc_qty,-- 上月全库存
    last_m_work_in_progress_amt, -- 上月总金额
    0, -- 上月呆滞
    0, -- 上月存货
    case when '${库存类型}' = "在制" then last_m_raw_material_cost_amt else 0 end last_m_raw_material_cost_amt, -- 上上月物料
    case when '${单位}' = 'PCS' then this_m_work_in_progress_pc_qty/10000
    when '${单位}' = 'MW' then 0
    else this_m_work_in_progress_pc_qty/10000 end this_m_work_in_progress_pc_qty,-- 当月全库存
    this_m_work_in_progress_amt, -- 当月总金额
    0, -- 当月呆滞
    0, -- 当月存货
    case when '${库存类型}' = "在制" then this_m_raw_material_cost_amt else 0 end this_m_raw_material_cost_amt, -- 当月物料
    case when '${单位}' = 'PCS' then last_7_d_work_in_progress_pc_qty/10000
    when '${单位}' = 'MW' then 0
    else last_7_d_work_in_progress_pc_qty/10000 end last_7_d_work_in_progress_pc_qty,-- 近七天总库存
    last_7_d_work_in_progress_amt, -- 近七天总金额
    0, -- 第四周呆滞
    0, -- 第四周存货
    case when '${库存类型}' = "在制" then last_7_d_raw_material_cost_amt else 0 end last_7_d_raw_material_cost_amt, -- 第四周物料
    case when '${单位}' = 'PCS' then last_2_w_work_in_progress_pc_qty/10000
    when '${单位}' = 'MW' then 0
    else last_2_w_work_in_progress_pc_qty/10000 end last_2_w_work_in_progress_pc_qty,-- 近两周总库存
    last_2_w_work_in_progress_amt, -- 近两周总金额
    0, -- 第三周呆滞
    0, -- 第三周存货
    case when '${库存类型}' = "在制" then last_2_w_raw_material_cost_amt else 0 end last_2_w_raw_material_cost_amt, -- 第三周物料
    case when '${单位}' = 'PCS' then last_3_w_work_in_progress_pc_qty/10000
    when '${单位}' = 'MW' then 0
    else last_3_w_work_in_progress_pc_qty/10000 end last_3_w_work_in_progress_pc_qty,-- 近三周总库存
    last_3_w_work_in_progress_amt, -- 近三周总金额
    0, -- 第二周呆滞
    0, -- 第二周存货
    case when '${库存类型}' = "在制" then last_3_w_raw_material_cost_amt else 0 end last_3_w_raw_material_cost_amt, -- 第二周物料
    case when '${单位}' = 'PCS' then last_4_w_work_in_progress_pc_qty/10000
    when '${单位}' = 'MW' then 0
    else last_4_w_work_in_progress_pc_qty/10000 end last_4_w_work_in_progress_pc_qty,-- 近四周总库存
    last_4_w_work_in_progress_amt, -- 近四周总金额
    0, -- 第一周呆滞
    0, -- 第一周存货
    case when '${库存类型}' = "在制" then last_4_w_raw_material_cost_amt else 0 end last_4_w_raw_material_cost_amt -- 第一周物料
from 
    aiko_dm_isc.dws_work_in_progress_rgalfudy
where 
  dt between date_add(CURDATE(),interval -6 day) and CURDATE()
and  mat_group_category_name = "组件成品"
),
aiko_wk as -- 爱旭周
(select aiko_week,CAST(SUBSTRING(aiko_week,3,4) as int) num  from aiko_dm_qms.dim_date_dimension
where date_all between STR_TO_DATE(CONCAT(YEAR(CURDATE()), '-01-01'), '%Y-%m-%d') and  CURRENT_DATE() 
group by 
aiko_week 
order by 
num desc
limit 4),
ads_inventory_trend as (
  select 
  	mat_products_1_category_name,
    last_reciv_date, -- 上次收货日期
    dt_dis,
    inventory_type ,
    "近七天" trend_lable,
    DATE_FORMAT(snpsht_date, '%m/%d') trend_tm,
    cast(concat(200,DATE_FORMAT(snpsht_date, '%m%d')) as int) trend_num,
    product_category_name,
    pattern_vulgo_name,
      faty_code, -- 工厂编码
      warhs_lgort_code, -- 库位编码
      mat_code, -- 物料编码
      long_desc_txt,
      mat_batch_code, -- 批次号
    base_name,
    main_product_type_name,
    is_regular_flag,
      home_or_abroad_txt, 
      region_dept_name, -- 地域部
      area_name, -- 区域
      utility_distributed_name, 
      module_type_name, 
      module_type_code, 
      module_size_code,  
      module_levl_code, 
      module_pattern_code, 
      watt_per_pic_amt,
      is_oem_flag,-- 是否代工
      is_rd_flag,
    is_shipped_flag, -- 是否可发库存
    is_shipp_domestic_overseas_flag, -- 是否可发海外
    inventory_age_qty,
    transit,
    full_num,
    full_amt,
    material_cost_amt -- 当天
--    this_m_material_cost_amt as material_cost_amt -- 当月成本
  from ads_inventory_now
  union all 
  select 
  	mat_products_1_category_name,
    last_reciv_date,
    dt_dis,
    inventory_type ,
    "近四周" trend_lable,
    (select aiko_week from aiko_wk order by num desc limit 1 offset 3),
    121,
    product_category_name,
    pattern_vulgo_name,
    faty_code, -- 工厂编码
      warhs_lgort_code, -- 库位编码
      mat_code, -- 物料编码
      long_desc_txt,
      mat_batch_code, -- 批次号
    base_name,
    main_product_type_name,
    is_regular_flag,
      home_or_abroad_txt, 
        region_dept_name, -- 地域部
      area_name, -- 区域
      utility_distributed_name, 
      module_type_name, 
      module_type_code, 
      module_size_code,  
      module_levl_code, 
      module_pattern_code, 
      watt_per_pic_amt,
      is_oem_flag,-- 是否代工
      is_rd_flag,
    is_shipped_flag, -- 是否可发库存
    is_shipp_domestic_overseas_flag, -- 是否可发海外
    inventory_age_qty,
    transit,
    last_4_w_full_pc_qty,
    last_4_w_full_amt,
    last_4_w_material_cost_amt
  from ads_inventory_now
  where snpsht_date = DATE_ADD(CURRENT_DATE(),interval -0 day)
  union all 
  select 
  	mat_products_1_category_name,
    last_reciv_date,
    dt_dis,
    inventory_type ,
    "近四周" trend_lable,
    (select aiko_week from aiko_wk order by num desc limit 1 offset 2),
    122,
    product_category_name,
    pattern_vulgo_name,
    faty_code, -- 工厂编码
      warhs_lgort_code, -- 库位编码
      mat_code, -- 物料编码
      long_desc_txt,
      mat_batch_code, -- 批次号
    base_name,
    main_product_type_name,
    is_regular_flag,
      home_or_abroad_txt, 
      region_dept_name, -- 地域部
      area_name, -- 区域
      utility_distributed_name, 
      module_type_name, 
      module_type_code, 
      module_size_code,  
      module_levl_code, 
      module_pattern_code, 
      watt_per_pic_amt,
        is_oem_flag,-- 是否代工
      is_rd_flag,
    is_shipped_flag, -- 是否可发库存
    is_shipp_domestic_overseas_flag, -- 是否可发海外
    inventory_age_qty,
    transit,
    last_3_w_full_pc_qty,
    last_3_w_full_amt,
    last_3_w_material_cost_amt
  from ads_inventory_now
  where snpsht_date = DATE_ADD(CURRENT_DATE(),interval -0 day)
  union all 
  select 
  	mat_products_1_category_name,
    last_reciv_date,
    dt_dis,
    inventory_type ,
    "近四周" trend_lable,
    (select aiko_week from aiko_wk order by num desc limit 1 offset 1),
    123,
    product_category_name,
    pattern_vulgo_name,
      faty_code, -- 工厂编码
      warhs_lgort_code, -- 库位编码
      mat_code, -- 物料编码
      long_desc_txt,
      mat_batch_code, -- 批次号
    base_name,
    main_product_type_name,
    is_regular_flag,
      home_or_abroad_txt, 
        region_dept_name, -- 地域部
      area_name, -- 区域
      utility_distributed_name, 
      module_type_name, 
      module_type_code, 
      module_size_code,  
      module_levl_code, 
      module_pattern_code, 
      watt_per_pic_amt,
        is_oem_flag,-- 是否代工
      is_rd_flag,
    is_shipped_flag, -- 是否可发库存
    is_shipp_domestic_overseas_flag, -- 是否可发海外
    inventory_age_qty,
    transit,
    last_2_w_full_pc_qty,
    last_2_w_full_amt,
    last_2_w_material_cost_amt
  from ads_inventory_now  
  where snpsht_date = DATE_ADD(CURRENT_DATE(),interval -0 day)
  union all 
  select 
 	 mat_products_1_category_name,
    last_reciv_date,
    dt_dis,
    inventory_type ,
    "近四周" trend_lable,
    (select aiko_week from aiko_wk order by num desc limit 1 ),
    124,
    product_category_name,
    pattern_vulgo_name,
      faty_code, -- 工厂编码
      warhs_lgort_code, -- 库位编码
      mat_code, -- 物料编码
      long_desc_txt,
      mat_batch_code, -- 批次号
    base_name,
    main_product_type_name,
    is_regular_flag,
      home_or_abroad_txt, 
        region_dept_name, -- 地域部
      area_name, -- 区域
      utility_distributed_name, 
      module_type_name, 
      module_type_code, 
      module_size_code,  
      module_levl_code, 
      module_pattern_code, 
      watt_per_pic_amt,
        is_oem_flag,-- 是否代工
      is_rd_flag,
    is_shipped_flag, -- 是否可发库存
    is_shipp_domestic_overseas_flag, -- 是否可发海外
    inventory_age_qty,
    transit,
    last_7_d_full_pc_qty,
    last_7_d_full_amt,
    last_7_d_material_cost_amt
  from ads_inventory_now
  where snpsht_date = DATE_ADD(CURRENT_DATE(),interval -0 day)
  union all 
  select 
  	mat_products_1_category_name,
    last_reciv_date,
    dt_dis,
    inventory_type ,
    "近三月" trend_lable,
    CONCAT(MONTH(CURRENT_DATE()),"月"), -- 当月
    4,
    product_category_name,
    pattern_vulgo_name,
      faty_code, -- 工厂编码
      warhs_lgort_code, -- 库位编码
      mat_code, -- 物料编码
      long_desc_txt,
      mat_batch_code, -- 批次号
    base_name,
    main_product_type_name,
    is_regular_flag,
      home_or_abroad_txt, 
        region_dept_name, -- 地域部
      area_name, -- 区域
      utility_distributed_name, 
      module_type_name, 
      module_type_code, 
      module_size_code,  
      module_levl_code, 
      module_pattern_code, 
      watt_per_pic_amt,
        is_oem_flag,-- 是否代工
     is_rd_flag,
    is_shipped_flag, -- 是否可发库存
    is_shipp_domestic_overseas_flag, -- 是否可发海外
    inventory_age_qty,
    transit,
    this_m_full_pc_qty,
    this_m_full_amt,
    this_m_material_cost_amt
  from ads_inventory_now  
  where snpsht_date = DATE_ADD(CURRENT_DATE(),interval -0 day)
  union all 
  select 
  	mat_products_1_category_name,
    last_reciv_date,
    dt_dis,
    inventory_type ,
    "近三月" trend_lable,
    CONCAT(MONTH(DATE_SUB(CURDATE(), INTERVAL 1 MONTH)),"月"), -- 上月
    3,
    product_category_name,
    pattern_vulgo_name,
      faty_code, -- 工厂编码
      warhs_lgort_code, -- 库位编码
      mat_code, -- 物料编码
      long_desc_txt,
      mat_batch_code, -- 批次号
    base_name,
    main_product_type_name,
    is_regular_flag,
      home_or_abroad_txt, 
        region_dept_name, -- 地域部
      area_name, -- 区域
      utility_distributed_name, 
      module_type_name, 
      module_type_code, 
      module_size_code,  
      module_levl_code, 
      module_pattern_code, 
      watt_per_pic_amt,
        is_oem_flag,-- 是否代工
      is_rd_flag,
    is_shipped_flag, -- 是否可发库存
    is_shipp_domestic_overseas_flag, -- 是否可发海外
    inventory_age_qty,
    transit,
    last_m_full_pc_qty,
    last_m_full_amt,
    last_m_material_cost_amt
  from ads_inventory_now    
  where snpsht_date = DATE_ADD(CURRENT_DATE(),interval -0 day)
  union all 
  select 
  	mat_products_1_category_name,
    last_reciv_date,
    dt_dis,
    inventory_type ,
    "近三月" trend_lable,
    CONCAT(MONTH(DATE_SUB(CURDATE(), INTERVAL 2 MONTH)),"月"), -- 上上月
    2,
    product_category_name,
    pattern_vulgo_name,
      faty_code, -- 工厂编码
      warhs_lgort_code, -- 库位编码
      mat_code, -- 物料编码
      long_desc_txt,
      mat_batch_code, -- 批次号
    base_name,
    main_product_type_name,
    is_regular_flag,
      home_or_abroad_txt, 
        region_dept_name, -- 地域部
      area_name, -- 区域
      utility_distributed_name, 
      module_type_name, 
      module_type_code, 
      module_size_code,  
      module_levl_code, 
      module_pattern_code, 
      watt_per_pic_amt,
        is_oem_flag,-- 是否代工
      is_rd_flag,
    is_shipped_flag, -- 是否可发库存
    is_shipp_domestic_overseas_flag, -- 是否可发海外
    inventory_age_qty,
    transit,
    before_last_m_full_pc_qty,
    before_last_m_full_amt,
    before_last_m_material_cost_amt
  from ads_inventory_now  
  where snpsht_date = DATE_ADD(CURRENT_DATE(),interval -0 day)
  union all 
  select 
  	mat_products_1_category_name,
    last_reciv_date,
    dt_dis,
    inventory_type ,
    "近半年" trend_lable,
    CONCAT(YEAR(CURRENT_DATE()), "上半年"), 
    1,
    product_category_name,
    pattern_vulgo_name,
      faty_code, -- 工厂编码
      warhs_lgort_code, -- 库位编码
      mat_code, -- 物料编码
      long_desc_txt,
      mat_batch_code, -- 批次号
    base_name,
    main_product_type_name,
    is_regular_flag,
      home_or_abroad_txt, 
        region_dept_name, -- 地域部
      area_name, -- 区域
      utility_distributed_name, 
      module_type_name, 
      module_type_code, 
      module_size_code,  
      module_levl_code, 
      module_pattern_code, 
      watt_per_pic_amt,
        is_oem_flag,-- 是否代工
      is_rd_flag,
    is_shipped_flag, -- 是否可发库存
    is_shipp_domestic_overseas_flag, -- 是否可发海外
    inventory_age_qty,
    transit,
    last_half_year_full_pc_qty,
    last_half_year_full_amt,
    last_half_year_material_cost_amt
  from ads_inventory_now    
  where snpsht_date = DATE_ADD(CURRENT_DATE(),interval -0 day)
  union all 
  select 
 	 mat_products_1_category_name,
    last_reciv_date,
    dt_dis,
    inventory_type ,
    "近一年" trend_lable,
    CONCAT(YEAR(CURRENT_DATE())-1, "年"), 
    0,
    product_category_name,
    pattern_vulgo_name,
      faty_code, -- 工厂编码
      warhs_lgort_code, -- 库位编码
      mat_code, -- 物料编码
      long_desc_txt,
      mat_batch_code, -- 批次号
    base_name,
    main_product_type_name,
    is_regular_flag,
      home_or_abroad_txt, 
        region_dept_name, -- 地域部
        area_name, -- 区域
      utility_distributed_name, 
      module_type_name, 
      module_type_code, 
      module_size_code,  
      module_levl_code, 
      module_pattern_code, 
      watt_per_pic_amt,
        is_oem_flag,-- 是否代工
      is_rd_flag,
    is_shipped_flag, -- 是否可发库存
    is_shipp_domestic_overseas_flag, -- 是否可发海外
    inventory_age_qty,
    transit,
    last_year_full_pc_qty,
    last_year_full_amt,
    last_year_material_cost_amt
  from ads_inventory_now
  where snpsht_date = DATE_ADD(CURRENT_DATE(),interval -0 day)
  )
select 
  mat_products_1_category_name as "资材类型",
  last_reciv_date as "上次收货时间",
  dt_dis as "库龄分布",
  inventory_type as "在库/在途/在制",
  product_category_name as "主推/库存",
  pattern_vulgo_name as "版型俗称", 
    faty_code as "工厂编码",
    warhs_lgort_code as "库位编码",
    mat_code as "物料编码",
    long_desc_txt as "物料描述",
    mat_batch_code as "批次号",
  base_name as "基地",
  trend_lable as "时间趋势",
  trend_tm as "趋势时间",
  trend_num as "趋势排序",
  main_product_type_name as "主流/非主流",
  is_regular_flag as "常规/非常规",
  case when is_regular_flag = "常规" then 25 -- 后续切为关联
  when is_regular_flag = "非常规" then 8 -- 后续切为关联
  else 0 end "组件周转目标", -- 单独新增写死,新增超目标分析
  home_or_abroad_txt as "海内外",
  region_dept_name as "地域部",
  area_name as "区域",
  utility_distributed_name as "集中式/分布式",
  module_type_name as "组件类型(单双玻)",
  module_type_code as "组件型号",
  module_size_code as "组件尺寸",
  module_levl_code as "组件等级",
  module_pattern_code as "组件版型",
  watt_per_pic_amt as "组件功率",
  is_oem_flag as "是否代工",
  -- 是否代工
    is_rd_flag as "是否研发",
  is_shipped_flag as "是否可发库存",
  -- 是否可发库存
  is_shipp_domestic_overseas_flag as "是否可发海外",
  -- 是否可发海外
  inventory_age_qty as "库龄",
  transit as "在途(单位)",
  full_num as "全库存(单位)",
  full_amt as "全库存(金额)",
  material_cost_amt as "物料成本",
  sum(case when mat_products_1_category_name = "组件成品" then full_amt else 0 end ) over (partition by inventory_type,trend_tm) as "单位时间总金额",
  sum(material_cost_amt) over (partition by trend_tm) as "单位时间总成本",
  sum(case when mat_products_1_category_name = "组件成品" then full_num else 0 end ) over (partition by inventory_type,trend_tm) as "单位时间总数量"
from
  ads_inventory_trend