Advertisement
Pandaaaa906

客户成交信息

Aug 20th, 2024
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. SELECT
  2. cus.code 客户编码,
  3. cus.full_name 客户名称,
  4. so_summary.近一年销售数量::float / NULLIF(GREATEST(so_summary.近一年销售数量, inq_summary.近一年询价数量), 0) 近一年成交率,
  5. inq_summary.近一年询价数量,
  6. so_summary.近一年销售数量,
  7. so_summary.近一年销售金额,
  8. rd32_summary.客户近一年毛利率
  9. FROM "public".t_crm_custom cus
  10.  
  11. LEFT JOIN LATERAL(
  12.     SELECT
  13. --  inq_m.customer_name,
  14.     count(distinct (inq_d.inq_order_id, inq_d.brand_no)) 近一年询价数量
  15.     FROM "public".t_inq_order inq_m
  16.    
  17.     LEFT JOIN "public".t_inq_order_detail inq_d
  18.     ON inq_d.inq_order_id = inq_m.id
  19.    
  20.     WHERE 1=1
  21. --  AND inq_m.customer_name = cus.full_name
  22.     AND inq_m.is_delete = 1
  23.     AND inq_d.is_delete = 1
  24.     AND inq_d.brand_name ilike 'cato'
  25.     AND inq_d.brand_no ~ any(array['^C4X-', '^C3D-', '^C\d+', '^CP\d+'])  -- 药物线
  26.     AND inq_m.create_time >= now() - '1 year'::interval
  27.    
  28.     AND inq_m.customer_name = cus.full_name
  29. ) inq_summary
  30. ON TRUE
  31.  
  32. LEFT JOIN LATERAL(
  33.     SELECT
  34.     count(*) 近一年销售数量,
  35.     sum(so_d."iSum" - coalesce(crm_so_d.loss_amount, 0)) 近一年销售金额
  36.     FROM u8_009."SO_SOMain" so_m
  37. --  ON so_m."cCusCode" = u8_cus."cCusCode"
  38.    
  39.     LEFT JOIN u8_009."SO_SODetails" so_d
  40.     ON so_d."ID" = so_m."ID"
  41.    
  42.     LEFT JOIN "public".t_inq_sync_crm_detail crm_so_d
  43.     ON crm_so_d.sal_order_detail_id = so_d."iSOsID"::text
  44.     AND crm_so_d.is_delete = 1
  45.    
  46.     LEFT JOIN u8_009."Inventory" inv
  47.     ON inv."cInvCode" = so_d."cInvCode"
  48.    
  49.     WHERE 1=1
  50.     AND so_m."cCusCode" = cus.code
  51.     AND so_m.dverifysystime is not null
  52.     AND inv."cInvCCode" in (
  53.     '03021005'  -- 药物线
  54.     )
  55.     AND so_m."dDate" >= now() - '1 year'::interval
  56.    
  57.     GROUP BY so_m."cCusCode"
  58. ) so_summary
  59. -- ON so_summary."cCusCode" = cus.code
  60. ON TRUE
  61.  
  62. LEFT JOIN LATERAL(
  63.     SELECT
  64.     avg(((so_d."iSum" - coalesce(crm_so_d.loss_amount, 0)) / so_d."iQuantity" * rd32_d."iQuantity" - rd32_d."iPrice") / nullif(((so_d."iSum" - coalesce(crm_so_d.loss_amount, 0)) / so_d."iQuantity" * rd32_d."iQuantity"), 0)) 客户近一年毛利率
  65.     FROM u8_009.rdrecord32 rd32_m
  66.    
  67.     INNER JOIN u8_009.rdrecords32 rd32_d
  68.     ON rd32_d."ID" = rd32_m."ID"
  69.    
  70.     INNER JOIN u8_009."SO_SODetails" so_d
  71.     ON so_d."iSOsID" = rd32_d.iorderdid
  72.    
  73.     LEFT JOIN "public".t_inq_sync_crm_detail crm_so_d
  74.     ON crm_so_d.sal_order_detail_id = so_d."iSOsID"::text
  75.     AND crm_so_d.is_delete = 1
  76.    
  77.     LEFT JOIN u8_009."Inventory" inv
  78.     ON inv."cInvCode"= rd32_d."cInvCode"
  79.    
  80.     WHERE 1=1
  81.     AND rd32_m."cCusCode" = cus.code
  82.     AND rd32_m."dDate" >= now() - '1 year'::interval
  83.     AND rd32_m.dnverifytime is not null
  84.     AND rd32_d."iPrice" is not null
  85.     AND rd32_d."iQuantity" > 0
  86.     AND inv."cInvCCode" in (
  87.     '03021005'  -- 药物线
  88.     )
  89. ) rd32_summary
  90. ON TRUE
  91.  
  92. WHERE cus.is_delete = 1
  93. AND cus.code = '12763'  -- 客户编码输入到这里
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement