Advertisement
bluebunny72

Recursive Bill of Materials

Aug 29th, 2019
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 4.44 KB | None | 0 0
  1.    ;WITH
  2.    BOM (
  3.          job_a
  4.         ,suffix_a
  5.         ,job_c
  6.         ,suffix_c
  7.         ,opernum
  8.         ,[SEQUENCE]
  9.         ,anchoritem
  10.         ,item
  11.         ,component     
  12.         ,[level]
  13.         ,SortPath
  14.         ,total_matl_qty
  15.         ,total_matl_qty_conv
  16.         ,matl_qty
  17.         ,u_m
  18.         ,units --U = per unit L = per lot (Basically if U then you need that matl_qty for every 1 unit you make.  If L then it doesn't matter how many you make it will always cost the same on time qty of matl_qty)
  19.         ,item_path
  20.   )
  21.   AS (
  22.         SELECT
  23.              j.job      AS job_a--job
  24.             ,j.suffix   AS suffix_a --suffix
  25.             ,j.job AS job_c--job
  26.             ,j.suffix AS suffix_c--suffix
  27.             ,jr.oper_num--opernum
  28.             ,jm.[SEQUENCE]--sequence
  29.             ,j.item--anchoritem
  30.             ,j.item--item
  31.             ,jm.item--component    
  32.             ,0 AS [level]--level
  33.             ,CAST('\' + RIGHT('000'+CONVERT(VARCHAR(3),jm.[sequence]),3) AS NVARCHAR(254)) --sortpath
  34.             ,jm.matl_qty as total_matl_qty
  35.             ,jm.matl_qty_conv as total_matl_qty_conv
  36.             ,jm.matl_qty
  37.             ,jm.u_m
  38.             ,jm.units
  39.             ,cast(j.item as varchar) as item_path
  40.         FROM dbo.job j
  41.         JOIN dbo.jobmatl jm ON jm.job = j.job
  42.             AND jm.suffix = j.suffix
  43.         JOIN dbo.jobroute jr ON jr.job = j.job
  44.             AND jr.suffix = j.suffix
  45.             AND jr.oper_num = jm.oper_num
  46.  
  47.         WHERE j.stat = 'R'
  48.  
  49.         UNION ALL
  50.    
  51.         SELECT
  52.              b.job_a
  53.             ,b.suffix_a
  54.             ,jm.job    as job_c--job
  55.             ,jm.suffix as suffix_c  --suffix
  56.             ,jr.oper_num--opernum
  57.             ,jm.[Sequence]--sequence
  58.             ,b.anchoritem--anchoritem
  59.             ,i.item--item
  60.             ,jm.item--component
  61.             ,(b.[level] + 1) AS [level]--level
  62.             ,CAST(b.SortPath + '\' + RIGHT('000'+CONVERT(VARCHAR(3),jm.[sequence]),3) AS NVARCHAR(254))--sortpath
  63.             ,CAST((b.total_matl_qty * jm.matl_qty) as decimal(18,8)) as total_matl_qty
  64.             ,CAST((b.total_matl_qty_conv * jm.matl_qty) as decimal(18,8)) as total_matl_qty_conv
  65.             ,jm.matl_qty
  66.             ,jm.u_m
  67.             ,jm.units
  68.             ,Cast((ISNULL(b.item_path,'') + '>' + i.item) as varchar) as item_path
  69.         FROM BOM b
  70.         JOIN dbo.item i ON i.item = b.component
  71.         JOIN dbo.jobmatl jm ON jm.job = i.job
  72.             AND jm.suffix = i.suffix
  73.         JOIN dbo.jobroute jr ON jr.job = jm.job
  74.             AND jr.suffix = jm.suffix
  75.             AND jr.oper_num = jm.oper_num
  76.        
  77.     )
  78.     SELECT
  79.          b.job_a AS job
  80.         ,b.suffix_a AS suffix
  81.         ,b.anchoritem as top_level_item
  82.         ,b.item
  83.         ,b.[level]
  84.         ,b.item_path
  85.     FROM
  86.         BOM b
  87.     left join
  88.         job j
  89.     on 
  90.         b.job_a = j.job
  91.     and
  92.         b.suffix_a = j.suffix
  93.     where
  94.         b.anchoritem = b.component and (j.rework = 1 and level > 0 or j.rework = 0)
  95.  
  96.  
  97.   ;WITH
  98.   BOM (
  99.          job_c
  100.         ,suffix_c
  101.         ,opernum
  102.         ,[sequence]
  103.         ,anchoritem
  104.         ,item
  105.         ,component     
  106.         ,[level]
  107.         ,total_matl_qty
  108.         ,total_matl_qty_conv
  109.         ,matl_qty
  110.         ,u_m
  111.         ,units --U = per unit L = per lot (Basically if U then you need that matl_qty for every 1 unit you make.  If L then it doesn't matter how many you make it will always cost the same ON TIME qty OF matl_qty)
  112.         ,item_path
  113.   )
  114.   AS (
  115.         SELECT
  116.              i.job AS job_c--job
  117.             ,i.suffix AS suffix_c--suffix
  118.             ,jr.oper_num--opernum
  119.             ,jm.[SEQUENCE]--sequence
  120.             ,i.item--anchoritem
  121.             ,i.item--item
  122.             ,jm.item--component    
  123.             ,0 AS [level]--level
  124.             ,jm.matl_qty AS total_matl_qty
  125.             ,jm.matl_qty_conv AS total_matl_qty_conv
  126.             ,jm.matl_qty
  127.             ,jm.u_m
  128.             ,jm.units
  129.             ,CAST(i.item AS VARCHAR) AS item_path
  130.         FROM dbo.item i
  131.         JOIN dbo.jobmatl jm ON jm.job = i.job
  132.             AND jm.suffix = i.suffix
  133.         JOIN dbo.jobroute jr ON jr.job = i.job
  134.             AND jr.suffix = i.suffix
  135.             AND jr.oper_num = jm.oper_num
  136.  
  137.         WHERE i.stat IN ('A','S')
  138.  
  139.         UNION ALL
  140.    
  141.         SELECT
  142.              jm.job    AS job_c--job
  143.             ,jm.suffix AS suffix_c  --suffix
  144.             ,jr.oper_num--opernum
  145.             ,jm.[SEQUENCE]--sequence
  146.             ,b.anchoritem--anchoritem
  147.             ,i.item--item
  148.             ,jm.item--component
  149.             ,(b.[level] + 1) AS [level]--level
  150.             ,CAST((b.total_matl_qty * jm.matl_qty) AS DECIMAL(18,8)) AS total_matl_qty
  151.             ,CAST((b.total_matl_qty_conv * jm.matl_qty) AS DECIMAL(18,8)) AS total_matl_qty_conv
  152.             ,jm.matl_qty
  153.             ,jm.u_m
  154.             ,jm.units
  155.             ,CAST((ISNULL(b.item_path,'') + '>' + i.item) AS VARCHAR) AS item_path
  156.         FROM BOM b
  157.         JOIN dbo.item i ON i.item = b.component
  158.         JOIN dbo.jobmatl jm ON jm.job = i.job
  159.             AND jm.suffix = i.suffix
  160.         JOIN dbo.jobroute jr ON jr.job = jm.job
  161.             AND jr.suffix = jm.suffix
  162.             AND jr.oper_num = jm.oper_num
  163.        
  164.     )
  165.     SELECT
  166.          b.anchoritem AS top_level_item
  167.         ,b.item
  168.         ,b.[level]
  169.         ,b.item_path
  170.     FROM
  171.         BOM b
  172.     WHERE
  173.         b.anchoritem = b.component
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement