Advertisement
Guest User

Untitled

a guest
Apr 8th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 6.14 KB | None | 0 0
  1. DECLARE @start_date AS SMALLDATETIME = '04/01/2019 06:00:00'
  2. , @end_date AS SMALLDATETIME = '04/08/2019 05:59:00'
  3.  
  4. SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
  5.     --DECLARE @start_date AS SMALLDATETIME = '04/02/19 06:00:00'
  6.     --      ,@end_date AS SMALLDATETIME = '04/03/19 05:59:00'
  7.  
  8.     DECLARE @shots TABLE(equipment_location_key NVARCHAR(50), equipment_location NVARCHAR(50), operation NVARCHAR(50), shif_shift_key NVARCHAR(50), shift NVARCHAR(50), scoe_equi_equipment_key NVARCHAR(50), wheel_style NVARCHAR(50)
  9.                         , scoe_whst_wheel_style_part_number NVARCHAR(50), --Mold_Component NVARCHAR(50),
  10.                          Samples NVARCHAR(50)--, Rejected NVARCHAR(50)
  11.                          )
  12.  
  13.     ;WITH Shots_Scrap_cte AS (
  14.  
  15.     SELECT equipment_location_key, equipment_location, operation , shif_shift_key, shift,scoe_equi_equipment_key, ISNULL(wheel_style, 'ND') as 'wheel_style',scoe_whst_wheel_style_part_number, --ISNULL(string1, 'N/A') AS 'Mold_Component',
  16.     CASE WHEN opre_result_id = 'PRO' THEN SUM(quantity) ELSE 0 END AS 'Samples'
  17.     --,CASE WHEN opre_result_id = 'SCR' THEN SUM(quantity) ELSE 0 END AS 'Rejected'
  18.     FROM collect_schemas_operations_equipments LEFT JOIN equipments ON scoe_equi_equipment_key = equipment_key
  19.     LEFT JOIN equipment_locations ON eqlo_equipment_location_key = equipment_location_key
  20.     LEFT JOIN operations ON opre_operation_key = operation_key
  21.     LEFT JOIN shifts ON shif_shift_key = shift_key
  22.     LEFT JOIN wheel_styles ON scoe_whst_wheel_style_part_number = wheel_style_part_number
  23.     WHERE effective_date BETWEEN @start_date AND @end_date
  24.     AND record_status = 'ACT'
  25.     AND scoe_operation_key = '015'
  26.     AND scoe_equi_equipment_key <> 'MRB01'
  27.     AND opre_result_id = 'PRO'
  28.     --AND shif_shift_key
  29.     --AND eqlo_equipment_location_key
  30.     --AND scoe_equi_equipment_key
  31.     GROUP BY equipment_location_key, equipment_location, operation , shif_shift_key, shift,scoe_equi_equipment_key, ISNULL(wheel_style, 'ND'),scoe_whst_wheel_style_part_number, string1, opre_result_id
  32.  
  33.     )
  34.  
  35.     INSERT INTO @shots
  36.     SELECT equipment_location_key, equipment_location, operation, shif_shift_key, shift, scoe_equi_equipment_key, wheel_style, scoe_whst_wheel_style_part_number, --Mold_Component,
  37.      SUM(Samples) AS 'Samples'--, SUM(Rejected) AS 'Rejected'
  38.     FROM Shots_Scrap_cte
  39.     GROUP BY equipment_location_key, equipment_location, operation, shif_shift_key, shift, scoe_equi_equipment_key, wheel_style, scoe_whst_wheel_style_part_number--, Mold_Component
  40.  
  41.  
  42.  
  43.  
  44.     DECLARE @defects TABLE(equipment_location NVARCHAR(50), equipment_key NVARCHAR(50), wheel_style NVARCHAR(50), wheel_style_part_number NVARCHAR(50),
  45.                                 mold_component NVARCHAR(50),
  46.                                 picker_status NVARCHAR(50), defect_key NVARCHAR(50), subdefect_key NVARCHAR(50), defect_location NVARCHAR(50), operation_key NVARCHAR(50), operation NVARCHAR(50),
  47.                                 shift_key NVARCHAR(50), shift NVARCHAR(50), Quantity INT)
  48.    
  49.     INSERT INTO @defects
  50.     SELECT  
  51.         EL.equipment_location as equipment_location, CC.scoe_equi_equipment_key as equipment_key, WS.wheel_style, CC.scoe_whst_wheel_style_part_number as wheel_style_part_number, CC.string1 as mold_component,
  52.         CC.opre_result_id AS picker_status, CC.defe_defect_key as defect_key, CC.deds_desu_defect_subcode_key, CC.dedl_delo_defect_location_key, CC.scoe_operation_key AS operation_key
  53.         , O.operation, CC.shif_shift_key, S.shift, SUM(CC.quantity) AS 'Quantity' --, CCC.shots AS shots
  54.     FROM collect_schemas_operations_equipments CC
  55.         LEFT JOIN racks_collect R ON R.csoe_collect_number = CC.collect_number
  56.         LEFT JOIN equipments E ON E.equipment_key=CC.scoe_equi_equipment_key
  57.         LEFT JOIN equipment_locations EL ON EL.equipment_location_key=E.eqlo_equipment_location_key
  58.         LEFT JOIN wheel_styles WS ON WS.wheel_style_part_number=CC.scoe_whst_wheel_style_part_number
  59.         LEFT JOIN operations O ON O.operation_key=CC.scoe_operation_key
  60.         LEFT JOIN shifts S ON S.shift_key=CC.shif_shift_key
  61.     WHERE
  62.         CC.opre_result_id='SCR'
  63.         AND CC.record_status='ACT'
  64.         AND CC.effective_date BETWEEN @start_date AND @end_date
  65.         AND CC.scoe_equi_equipment_key<>'MRB01' AND CC.scoe_operation_key='015'
  66.     GROUP BY EL.equipment_location, CC.scoe_equi_equipment_key, WS.wheel_style, CC.scoe_whst_wheel_style_part_number, CC.string1,
  67.         CC.opre_result_id, CC.defe_defect_key, CC.deds_desu_defect_subcode_key, CC.dedl_delo_defect_location_key, CC.scoe_operation_key
  68.         , O.operation, CC.shif_shift_key, S.shift
  69.  
  70.     --select * from @pickers_table WHERE equipment_key='CM01';
  71.  
  72.  
  73.     ;WITH cte_defects_by_wheel AS (
  74.         SELECT DISTINCT D.equipment_location, D.operation, D.equipment_key, D.wheel_style, D.wheel_style_part_number, D.mold_component, D.quantity,-- D.shots,
  75.                 D.defect_key, D.subdefect_key, D.defect_location, D.shift_key,
  76.             STUFF(
  77.             (SELECT DISTINCT  ', ' + D1.defect_key
  78.                     FROM @defects D1
  79.                     WHERE D.wheel_style_part_number = D1.wheel_style_part_number
  80.                         AND D.equipment_key = D1.equipment_key
  81.                         AND D.operation_key = D1.operation_key
  82.                         AND D.shift_key = D1.shift_key
  83.                         AND D.mold_component = D1.mold_component
  84.                     FOR XML PATH(''), TYPE
  85.                     ).value('.', 'NVARCHAR(MAX)')
  86.                 ,1,1,'') defects
  87.         FROM @defects D
  88.     )
  89.  
  90.  
  91.     SELECT --S.*, D.defects
  92.         S.equipment_location_key, S.equipment_location, S.operation, S.shif_shift_key AS shift_key, S.shift, S.scoe_equi_equipment_key AS equipment_key,
  93.         S.wheel_style, S.scoe_whst_wheel_style_part_number AS wheel_style_part_number,-- S.mold_component,
  94.         S.samples, ISNULL(D.Quantity, 0) as 'rejected', ISNULL(D.mold_component, 'N/A') AS 'mold_component', D.defects AS found_defects
  95.     FROM @shots S
  96.     LEFT JOIN (
  97.         SELECT equipment_location, operation, equipment_key, wheel_style_part_number, defects, shift_key, Quantity, mold_component--, ISNULL(mold_component, 'N/A') AS mold_component
  98.         FROM cte_defects_by_wheel
  99.         --WHERE equipment_key='CM01'
  100.         ) D
  101.             ON S.equipment_location=D.equipment_location AND S.operation=D.operation AND S.scoe_equi_equipment_key=D.equipment_key
  102.                 AND S.scoe_whst_wheel_style_part_number=D.wheel_style_part_number
  103.                 AND S.shif_shift_key=D.shift_key /*AND S.Mold_Component=D.mold_component*//*AND S.wheel_style=D.wheel_style */
  104.     --WHERE --scoe_equi_equipment_key='CM09'
  105.     --equipment_location_key='DK1'
  106.  
  107.     SET TRANSACTION ISOLATION LEVEL READ COMMITTED
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement