Advertisement
Pandaaaa906

Untitled

Sep 1st, 2023 (edited)
1,112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 13.87 KB | None | 0 0
  1. with tmp_invpositionsum_009 as (
  2.         -- 匹配最大库存量 批次 数量
  3.         select
  4.         row_number() over (partition by stock.cInvCode order by stock.iQuantity desc) as r,
  5.         stock.Autoid,
  6.         stock.cInvCode,
  7.         stock.cBatch,
  8.         stock.iQuantity,
  9.         stock.cFree1,
  10.         batch.cBatchProperty7,
  11.         batch.cBatchProperty10,
  12.         case
  13.          when extra.dw = 'g' and extra.isnum='1' then cast(extra.num as numeric(10,4))*1000  --1g=1000mg
  14.          when extra.dw = 'μg' and extra.isnum='1'  then cast(extra.num as numeric(10,4))*0.001  --1μg=0.001mg
  15.          when extra.dw = 'kg' and extra.isnum='1'  then cast(extra.num as numeric(10,4))*1000000  --1kg=1000000mg
  16.          when extra.dw = 'ml' and extra.isnum='1'  then cast(extra.num as numeric(10,4))*1000  --1ml=1000mg
  17.          when extra.dw = 'l' and extra.isnum='1'  then cast(extra.num as numeric(10,4))*1000000  --1L=1000000mg
  18.          when extra.dw = 'ng' and extra.isnum='1'   then cast(extra.num as numeric(10,4))*0.001 --1ng=0.001mg
  19.          when extra.dw = 'mg' and extra.isnum='1'  then cast(extra.num as numeric(10,4))
  20.          else null
  21.         end as cFree1_tomg,
  22.         count(stock.autoid) over (partition by stock.cInvCode) as "U8总批次数",
  23.         sum(stock.iQuantity) over (partition by stock.cInvCode) as  "U8总库存量",
  24.  
  25.     count(case when  DateAdd(yy,1,getdate()) <  CONVERT(TIMESTAMP, batch.cBatchProperty10) and stock.cBatch is not null then autoid end ) over (partition by stock.cInvCode) as "U8有效批次",
  26.    
  27.     sum(case when DateAdd(yy,1,getdate()) <  CONVERT(TIMESTAMP, batch.cBatchProperty10) and stock.cBatch is not null then iQuantity end ) over (partition by stock.cInvCode) as  "U8有效库存量"
  28.        
  29.         from InvPositionSum_U8_009 stock with(nolock)
  30.        
  31.         left join AA_BatchProperty_U8_009 batch with(nolock)
  32.         on stock.cInvCode = batch.cInvCode
  33.         and stock.cbatch = batch.cBatch
  34.        
  35.         OUTER apply (
  36.             SELECT
  37.             case
  38.             when LEN(stock.cFree1)-PATINDEX('%[0-9]%',reverse(stock.cFree1))+1 <0 then ''
  39.             else ISNUMERIC(substring(stock.cFree1,1,LEN(stock.cFree1)-PATINDEX('%[0-9]%',reverse(stock.cFree1))+1))
  40.         end as isnum,
  41.  
  42.         case
  43.             when LEN(stock.cFree1)-PATINDEX('%[0-9]%',reverse(stock.cFree1))+1 <0 then stock.cFree1
  44.             else replace(substring(stock.cFree1,1,LEN(stock.cFree1)-PATINDEX('%[0-9]%',reverse(stock.cFree1))+1),',','.')
  45.         end as num,
  46.  
  47.         case
  48.             when PATINDEX('%[0-9]%',reverse(stock.cFree1))-1<1 then stock.cFree1
  49.             else LTRIM(reverse(substring(reverse(stock.cFree1),1,PATINDEX('%[0-9]%',reverse(stock.cFree1))-1)))
  50.         end as dw
  51.         )extra
  52.          
  53. )
  54. , tmp_drug_line as (
  55.  
  56. -- 药物线
  57.  
  58. select
  59.  
  60. inv_009.cInvCCode,
  61. LIST_PROD_SO_PO_COLLECT.new_cat_no as prd_cat_no,
  62. LIST_PROD_SO_PO_COLLECT.prd_cas,
  63. LIST_PROD_SO_PO_COLLECT.prd_en_name,
  64. LIST_PROD_SO_PO_COLLECT.prd_cn_name,
  65. LIST_PROD_SO_PO_COLLECT.prd_api,
  66. LIST_PROD_SO_PO_COLLECT.prd_appearance,
  67. LIST_PROD_SO_PO_COLLECT.prd_storage,
  68. LIST_PROD_SO_PO_COLLECT.prd_remark,
  69.  
  70. case
  71.     when inv_009.cInvCCode like '0301%' then '非药线'
  72.     when inv_009.cInvCCode like '0302%' then '药物线'
  73.     when inv_009.cInvCCode like '0303%' then '中间体'
  74. else null
  75. end "产品线",
  76.  
  77. coalesce(so_count_2yearago, 0) "前两年销售次数",  --前两年销售次数
  78. coalesce(so_count_1yearago, 0) "前一年销售次数",  --前一年销售次数
  79.  
  80. coalesce(so_count_12month, 0) "近一年销售次数", --近一年销售次数
  81. coalesce(so_iqu_12month, 0) "近一年销售瓶数", --近一年销售瓶数
  82. coalesce(inq_cus_12monthago, 0) "近一年询价客户数", --近一年询价客户数
  83. coalesce(inq_12monthago, 0) "近一年询价次数", --近一年询价次数,
  84. coalesce(so_tomg_iqu_12month, 0) "近一年销售量(mg)", --近一年销售量(mg)
  85. coalesce(so_iSum_12month, 0) "近一年销售金额",
  86. coalesce(po_iNatSum_12month, 0) "近一年采购价税合计",
  87. coalesce(po_iqu_12month, 0) "近一年采购mg",
  88. coalesce(quo_iqu_12monthago, 0) "近一年询价mg",
  89.  
  90.  
  91. coalesce(so_tomg_iqu_3month, 0) "近三个月销售量(mg)", --近三个月销售量(mg)
  92. coalesce(so_tomg_iqu_2month, 0) "近两个月销售量(mg)", --近两个月销售量(mg)
  93.  
  94.  
  95. coalesce(so_tomg_iqu_6month, 0) "近半年销售量(mg)", --近半年销售量(mg)
  96. coalesce(so_iSum_6month, 0) "近半年销售金额",--近半年销售金额
  97. coalesce(po_iNatSum_6month, 0) "近半年采购价税合计",
  98. coalesce(po_iqu_6month, 0) "近半年采购mg",
  99. coalesce(inq_cus_6monthago, 0) "近半年询价客户数",
  100. coalesce(inq_6monthago, 0) "近半年询价次数",
  101. coalesce(quo_iqu_6monthago, 0) "近半年询价mg",
  102.  
  103. coalesce(inq_cus_3monthago, 0) "近三个月询价客户数",
  104.  
  105. coalesce(so_iSum_1month, 0) "近一个月销售金额",
  106. coalesce(so_tomg_iqu_1month, 0) "近一个月销售量(mg)",
  107. coalesce(inq_cus_1monthago, 0) "近一个月询价客户数",
  108. coalesce(inq_1monthago, 0) "近一个月询价次数",
  109. coalesce(po_iNatSum_1month, 0) "近一个月采购价税合计",
  110. coalesce(po_iqu_1month, 0) "近一个月采购mg",
  111. coalesce(quo_iqu_1monthago, 0) "近一个月询价mg",
  112.  
  113. ischeck 是否免检,
  114.  
  115. case when coalesce(tmp_invpositionsum_009.U8总库存量,0) < (so_tomg_iqu_6month/3) then '是' else '否' end 现存数量是否小于近两个月销售数量,
  116. case when coalesce(tmp_invpositionsum_009.U8总库存量,0) < (so_tomg_iqu_3month) then '是' else '否' end 现存数量是否小于近三个月销售数量,
  117. coalesce(tmp_invpositionsum_009.U8总库存量,0) + coalesce(LIST_PROD_SO_PO_COLLECT.po_iqu_own_afloat, 0) + COALESCE(onprogress_synth_prj.[在途研发量mg],0) 展望库存数量,
  118. case when coalesce(tmp_invpositionsum_009.U8总库存量,0) + coalesce(LIST_PROD_SO_PO_COLLECT.po_iqu_own_afloat, 0) + COALESCE(onprogress_synth_prj.[在途研发量mg],0)< (so_tomg_iqu_3month) then '是' else '否' end 展望数量是否小于近三个月销售数量,
  119.  
  120. case when DateAdd(yy,1,getdate()) <= tmp_invpositionsum_009.cBatchProperty10 then '是' else '否'  end 证书有效期是否大于等于1年,
  121.  
  122. cast(tmp_invpositionsum_009.U8有效库存量 as float) U8有效库存量,
  123. tmp_invpositionsum_009.U8有效批次,
  124.  
  125. cast(tmp_invpositionsum_009.U8总库存量 as float) U8总库存量,
  126. tmp_invpositionsum_009.U8总批次数,
  127.  
  128. cast(tmp_invpositionsum_009.iquantity as float)最大库存量,
  129. tmp_invpositionsum_009.cbatch  最大库存量批号,
  130.  
  131.  
  132. tmp_invpositionsum_009.cBatchProperty10 最大库存量证书过期日期,
  133.  
  134. tmp_invpositionsum_009.cBatchProperty7 纯度,
  135.  
  136. so_tomg_iqu_6month "目标备货量",
  137. LIST_PROD_SO_PO_COLLECT.po_iqu_own_afloat "采购在途量",
  138. inv_009.cInvDefine2 负责人,
  139. ST_iQuantity 原料可用库存,
  140. onprogress_synth_prj.[在途研发量mg],
  141. tmp_rd08.[研发入库数量]
  142.  
  143. from LIST_PROD_SO_PO_COLLECT with(nolock)
  144.  
  145. left join Inventory_U8_009 inv_009 with(nolock)
  146. on LIST_PROD_SO_PO_COLLECT.new_cat_no = inv_009.cInvCode
  147.  
  148. left join tmp_invpositionsum_009 with(nolock)
  149. on tmp_invpositionsum_009.cInvCode = 'M-' + LIST_PROD_SO_PO_COLLECT.new_cat_no
  150. AND tmp_invpositionsum_009.r = 1
  151.  
  152. OUTER apply (
  153.     SELECT sum(ST_iQuantity) ST_iQuantity
  154.     FROM LIST_Stock_Surplus_Num_u8_009 with(nolock)
  155.     where cInvCName='cato'
  156.     AND ST_cInvCode='M-'+prd_cat_no
  157. ) st  --可用原料库存
  158.    
  159. OUTER apply (
  160.     -- 研发入库
  161.         select
  162.         sum(rd08_d.iQuantity) 研发入库数量
  163.         from RdRecords08_U8_009 rd08_d with(nolock)
  164.  
  165.         left join RdRecord08_U8_009 rd08_m
  166.         on rd08_d.ID = rd08_m.ID
  167.  
  168.         where rd08_m.cRdCode = '0110'
  169.         AND rd08_d.cInvCode = 'M-' + prd_cat_no
  170. ) tmp_rd08
  171.    
  172. OUTER APPLY (
  173.         select
  174.         sum(quantity) as 在途研发量mg
  175.        
  176.         from Lims_bv_po_synthetic_u8_009 bv_po_synthetic  with(nolock)
  177.        
  178.         left join Lims_bv_product_u8_009 bv_product with(nolock)
  179.         on bv_po_synthetic.product_id = bv_product.id
  180.        
  181.         where bv_po_synthetic.synthetic_status in (1,2)  -- 未开始 在研
  182.         AND bv_product.material_article_no = 'M-' + prd_cat_no
  183.         AND bv_product.brand = 'cato'
  184.  
  185. ) onprogress_synth_prj
  186.  
  187. where 1=1
  188. AND LIST_PROD_SO_PO_COLLECT.prd_cat_no not in ('1','0','2','3','4','5','6','7','8','9','10')
  189.  
  190. )
  191. -- SELECT * FROM tmp_drug_line;
  192. , drug_line_catalog as (
  193.  
  194.     select
  195.     tmp_drug_line.* ,
  196.     cato_prd.img_url,
  197.     product_pakcage.cost as 最大规格成本,
  198.     product_pakcage.package_value as 最大规格,
  199.     product_pakcage.每毫克成本单价,
  200.     stockup_type."备货潜力标识",
  201.     stockup_type."核心备货标识",
  202.     case
  203.     when stockup_type."核心备货标识" = '是' then '常规备货'
  204.     when stockup_type."备货潜力标识" = '是' then '潜力备货'
  205.     end "备货类型",
  206.  
  207.     case
  208.         when COALESCE("近一年销售量(mg)", 0) < 200 then 200
  209.         when COALESCE("近一年销售量(mg)", 0) BETWEEN 200 and 500 then "近一年销售量(mg)"
  210.         when COALESCE("近一年销售量(mg)", 0) > 500 then 500
  211.     end '潜力备货规格',
  212.    
  213.     blacklist.*,
  214.     coalesce(CASE WHEN blacklist.黑名单结束日期 > getdate() THEN '是' ELSE '否' END, '否') 是否黑名单
  215.  
  216.     from tmp_drug_line
  217.    
  218.     OUTER apply (
  219.    
  220.         select TOP 1
  221.         cost,
  222.         package_value,
  223.         cost / COALESCE(cfree1_tomg, 1) 每毫克成本单价
  224.         from t_product_package pkg with(nolock)
  225.         where pkg.brand_name= 'cato'
  226.         AND pkg.is_delete = 1
  227.         AND pkg.cat_no = prd_cat_no
  228.         ORDER BY pkg.cfree1_tomg desc
  229.     )product_pakcage
  230.    
  231.     OUTER apply (
  232.         SELECT TOP 1
  233.         'https://web.cato-chem.com' + img_url img_url
  234.         FROM t_product prd with(nolock)
  235.         WHERE brand_name = 'cato'
  236.         AND is_delete = 1
  237.         AND prd.cat_no = prd_cat_no
  238.         ORDER BY create_time desc
  239.     ) cato_prd
  240.    
  241.     OUTER apply (
  242.         SELECT TOP 1
  243.         bl.blacklist_endtime 黑名单结束日期,
  244.         bl.remark 黑名单原因
  245.         FROM stock_up_blacklist bl with(nolock)
  246.         WHERE bl.cat_no = prd_cat_no
  247.         ORDER BY created_at desc
  248.     ) blacklist
  249.    
  250.     OUTER APPLY (
  251.         SELECT
  252.         case
  253.             when 近一年询价客户数 >= 10
  254.             or 近半年询价客户数 >= 8
  255.             or 近三个月询价客户数 >= 6
  256.             or 近一个月询价客户数 >= 4
  257.             then '是'
  258. --          when ${if(len(潜在_近一年询价客户数)=0,"1=0","近一年询价客户数 >= ('"+潜在_近一年询价客户数+"')")}
  259. --          or ${if(len(潜在_近半年询价客户数)=0,"1=0","近半年询价客户数 >= ('"+潜在_近半年询价客户数+"')")}
  260. --          or ${if(len(潜在_近三个月询价客户数)=0,"1=0","近三个月询价客户数 >= ('"+潜在_近三个月询价客户数+"')")}
  261. --          or ${if(len(潜在_近一个月询价客户数)=0,"1=0","近一个月询价客户数 >= ('"+潜在_近一个月询价客户数+"')")}
  262. --      then '是'
  263.     else '否'
  264.     end '备货潜力标识',
  265.  
  266.     case
  267.         when 近一年询价客户数 >= 4
  268.         and 近一年询价次数 >= 5
  269.         AND 近一年销售次数 >= 3
  270.         and 近一年销售瓶数 >= 4
  271.         then '是'
  272. --      when ${if(len(近一年销售次数)=0,"1=1","近一年销售次数 >= ('"+近一年销售次数+"')")}
  273. --      and ${if(len(近一年销售瓶数)=0,"1=1","近一年销售瓶数 >= ('"+近一年销售瓶数+"')")}
  274. --      and ${if(len(近一年询价客户数)=0,"1=1","近一年询价客户数 >= ('"+近一年询价客户数+"')")}
  275. --      and ${if(len(近一年询价次数)=0,"1=1","近一年询价次数 >= ('"+近一年询价次数+"')")}
  276. --      then '是'
  277.     else '否'
  278.     end '核心备货标识'
  279.     )stockup_type
  280.  
  281. )
  282.  
  283. select
  284. drug_line_catalog.*,
  285. last_po.last_po_cfree1,
  286. last_po.last_po_iQuantity,
  287. last_po.last_po_isum
  288.  
  289. from drug_line_catalog
  290.  
  291. OUTER APPLY(
  292.  
  293.     SELECT TOP 1
  294.     po_d.cFree1 last_po_cfree1,
  295.     po_d.iQuantity last_po_iQuantity,
  296.     po_d.iSum last_po_isum
  297.     FROM PO_Podetails_U8_009 po_d
  298.     LEFT JOIN PO_Pomain_U8_009 po_m
  299.     ON po_d.POID = po_m.POID
  300.    
  301.     WHERE po_d.cInvCode = 'M-' + drug_line_catalog.prd_cat_no
  302.     ORDER BY po_m.cAuditTime desc
  303. )last_po
  304.  
  305.  
  306. where 1=1
  307. and 产品线 in ('药物线')
  308. and 备货类型 in ('常规备货', '潜力备货')
  309. -- and 是否黑名单 = '否'
  310. -- and (
  311. --  (备货类型 = '常规备货' and 展望数量是否小于近三个月销售数量 = '是')
  312. --  or (备货类型 = '潜力备货' and (展望库存数量 < case when [近三个月销售量(mg)] > 潜力备货规格 then [近三个月销售量(mg)] else 潜力备货规格 -- -- end))
  313. -- )
  314. --------
  315. -- ${if(len(货号)=0,"","and prd_cat_no  like ('%"+trim(货号)+"%')")}
  316. -- ${if(len(中文名)=0,"","and prd_cn_name like ('%"+trim(中文名)+"%')")}
  317. -- ${if(len(cas)=0,"","and prd_cas like ('%"+trim(cas)+"%')")}
  318. -- ${if(len(多货号)=0,"","and prd_cat_no in ('"+Replace(多货号,"\n","','")+"')")}
  319. -- ${if(len(备货类型) = 0,"","and 备货类型 in ('" + replace(备货类型,",","','") + "')")}
  320. -- ${if(len(产品线) = 0,"","and 产品线 in ('" + replace(产品线,",","','") + "')")}
  321. -- ${if(剔除黑名单 = "是","and 是否黑名单 = '否'", "")}
  322. -- ${if(只显示备货 = "是","and ((备货类型 = '常规备货' and 展望数量是否小于近三个月销售数量 = '是') or (备货类型 = '潜力备货' and (展望库存数量 < case when [近三个月销售量(mg)] > 潜力备货规格 then [近三个月销售量(mg)] else 潜力备货规格 end)))", "")}
  323. --------
  324. -- and ${if(len(前一年销售次数)=0,"1=1","前一年销售次数 >= ('"+前一年销售次数+"')")}
  325. -- and ${if(len(前两年销售次数)=0,"1=1","前两年销售次数 >= ('"+前两年销售次数+"')")}
  326. -- and ${if(len(近一年销售次数)=0,"1=1","近一年销售次数 >= ('"+近一年销售次数+"')")}
  327. -- and ${if(len(近一年销售瓶数)=0,"1=1","近一年销售瓶数 >= ('"+近一年销售瓶数+"')")}
  328. -- and ${if(len(近一年询价客户数)=0,"1=1","近一年询价客户数 >= ('"+近一年询价客户数+"')")}
  329. -- and ${if(len(近一年询价次数)=0,"1=1","近一年询价次数 >= ('"+近一年询价次数+"')")}
  330. -- and ${if(len(现存数量是否小于近两个月销售数量)=0,"1=1","现存数量是否小于近两个月销售数量 in ('"+现存数量是否小于近两个月销售数量+"')")}
  331. --
  332. -- and ${if(len(近一个月询价次数)=0,"1=1","近一个月询价次数 > ('"+近一个月询价次数+"')")}
  333. -- and ${if(len(近一个月询价客户数)=0,"1=1","近一个月询价客户数 > ('"+近一个月询价客户数+"')")}
  334.  
  335. --and ${if(len(api)=0,"1=1","API in ('"+api+"')")}
  336. order by prd_cat_no
  337.  
  338.  
  339.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement