我的代码-sql query
# coding: utf-8
# In[ ]:
WITH List AS (
SELECT e.*,f.* FROM
( SELECT DISTINCT c.lot_id, c.wafer_key,
LEFT(c.layer_id, CHARINDEX('_', c.layer_id) -1) as Layer,c.layer_id,
COUNT(0) AS defect_count,d.class_number, REPLACE(LTRIM(REPLACE(c.wafer_id, '0', ' ')), ' ', '0') as waferid
FROM [SSMC_IEDA].[dbo].[insp_wafer_summary] AS c, [SSMC_IEDA].[dbo].[insp_defect] AS d
where c.wafer_key = d.wafer_key and c.layer_id like '[CTM-M]%_ASI' and c.inspection_date BETWEEN '2018-11-01' AND '2018-11-20'
AND d.class_number in (167, 67)
GROUP BY c.lot_id,c.wafer_id,d.class_number,c.layer_id, c.wafer_key) AS e INNER JOIN
(select distinct a.recpid, a.stage, a.lotid, b.automatedrecipeid, LEFT(a.stage, CHARINDEX('_', a.stage) -1) as Stg from mes_tbl_hist AS a
INNER JOIN mes_tbl_recp AS b ON a.recpid = b.recpid WHERE a.eqpid LIKE 'AEM2%' AND a.stage NOT LIKE 'REWORK') AS f ON e.lot_id = f.lotid
AND (((e.Layer = 'CTM' OR e.Layer LIKE 'CTM%') AND (f.Stg = 'CTM' OR f.Stg LIKE 'CTM%'))
OR
(e.Layer LIKE 'M%' AND f.Stg LIKE CONCAT(substring(e.Layer, PatIndex('%[a-z]%',LOWER(e.Layer)), case PatIndex('%[a-z]%',LOWER(e.Layer)) when 0 then 2 else 1 end),'E',substring(e.Layer, PatIndex('%[0-9]%', e.Layer),case PatIndex('%[0-9]%',e.Layer) when 0 then 2 else 1 end),'%')
))
AND f.Stg NOT LIKE 'MW%'
AND f.Stg NOT LIKE 'MT%'
AND e.Layer NOT LIKE 'MT%'
AND e.Layer NOT LIKE 'MW%' )
SELECT DISTINCT List.layer_id,List.defect_count,b.eqpid, SUBSTRING(b.ParameterName, CHARINDEX('-', b.parametername) +1,1) as Chamber,
b.lotid,a.slotid,
a.waferid,
LEFT(b.ParameterName, CHARINDEX('-', b.ParameterName) -1) as Param_Name,
LEFT(c.stage, CHARINDEX('_', c.stage) -1) as Stg,
c.stage,
SUBSTRING(b.ParameterName, CHARINDEX('-', b.parametername) +3,+1) as Step,
SUBSTRING(b.ParameterName, CHARINDEX('-', b.parametername) +5, LEN( b.parametername)) as Recipie_Name,
b.parametername,
a.[data] as data1,a.finishtime,
rank() OVER (PARTITION BY a.slotid, b.eqpid,b.lotid,c.stage,List.layer_id,b.parametername ORDER BY a.finishtime) AS RNK
FROM [SSMC_RTM].[dbo].[rtm_tbl_massdata] a INNER JOIN [SSMC_RTM].[dbo].[rtm_tbl_datahist] b
ON a.[datahist_fno]=b.[fno] INNER JOIN SSMC_RTM.dbo.rtm_tbl_batchdatahist c ON b.fno = c.datahist_fno
INNER JOIN List ON List.lot_id = b.lotid AND List.waferid = a.slotid AND LEFT(c.stage, CHARINDEX('_', c.stage) -1) = List.Stg
AND SUBSTRING(b.parametername, CHARINDEX('-', b.parametername) +5, LEN( b.parametername)) = List.automatedrecipeid
WHERE b.parametername LIKE '%-4-%'
--AND b.parametername NOT LIKE '%-$'
AND b.parametername LIKE '%-%-%-%'
AND ( b.parametername LIKE 'ETCM_PHA4_A%' OR b.parametername LIKE 'ETCM_PHA4_B%'
OR b.parametername LIKE 'ETCM_PHB4_A%' OR b.parametername LIKE 'ETCM_PHB4_B%'
OR b.parametername LIKE 'ETCM_PHC4_A%' OR b.parametername LIKE 'ETCM_PHC4_B%'
OR b.parametername LIKE 'HELK_MEAN_A%' OR b.parametername LIKE 'HELK_MEAN_B%'
OR b.parametername LIKE 'LOWERCHM_PRESS_A%' OR b.parametername LIKE 'LOWERCHM_PRESS_B%'
OR b.parametername LIKE 'PBK4_A%' OR b.parametername LIKE 'PBK4_B%'
OR b.parametername LIKE 'RR23_MEAN_A%' OR b.parametername LIKE 'RR23_MEAN_B%'
OR b.parametername LIKE 'RR23_MAX._A%' OR b.parametername LIKE 'RR23_MAX._B%'
OR b.parametername LIKE 'RR13_MEAN_A%' OR b.parametername LIKE 'RR13_MEAN_B%'
OR b.parametername LIKE 'RR13_MAX._A%' OR b.parametername LIKE 'RR13_MAX._B%'
OR b.parametername LIKE 'THR3_MAX._A%' OR b.parametername LIKE 'THR3_MAX._B%'
OR b.parametername LIKE 'THR3_MAX._DIFF_A%' OR b.parametername LIKE 'THR3_MAX._DIFF_B%'
OR b.parametername LIKE 'THR3_MEAN_A%' OR b.parametername LIKE 'THR3_MEAN_B%'
OR b.parametername LIKE 'THR3_MEAN_DIFF_A%' OR b.parametername LIKE 'THR3_MEAN_DIFF_B%'
OR b.parametername LIKE 'THR3_MEAN_SLOPE_A%' OR b.parametername LIKE 'THR3_MEAN_SLOPE_B%'
-- b.parametername LIKE 'BIAS_COEF_A%' OR b.parametername LIKE 'BIAS_COEF_B%'
OR b.parametername LIKE 'CURRENT_MAX._A%' OR b.parametername LIKE 'CURRENT_MAX._B%'
OR b.parametername LIKE 'CURRENT_MAX._DIFF_A%' OR b.parametername LIKE 'CURRENT_MAX._DIFF_B%'
OR b.parametername LIKE 'CURRENT_MEAN_A%' OR b.parametername LIKE 'CURRENT_MEAN_B%'
OR b.parametername LIKE 'CURRENT_MEAN_DIFF_A%' OR b.parametername LIKE 'CURRENT_MEAN_DIFF_B%'
OR b.parametername LIKE 'DCB3_MEAN_A%' OR b.parametername LIKE 'DCB3_MEAN_B%'
--OR b.parametername LIKE 'DCB3_MEAN_DIFF_A%' OR b.parametername LIKE 'DCB3_MEAN_DIFF_B%'
OR b.parametername LIKE 'DCB3_MEAN_SLOPE_A%' OR b.parametername LIKE 'DCB3_MEAN_SLOPE_B%'
OR b.parametername LIKE 'ETCM_PHA4_DIFF_A%' OR b.parametername LIKE 'ETCM_PHA4_DIFF_B%'
OR b.parametername LIKE 'ETCM_PHB4_DIFF_A%' OR b.parametername LIKE 'ETCM_PHB4_DIFF_B%'
OR b.parametername LIKE 'ETCM_PHC4_DIFF_A%' OR b.parametername LIKE 'ETCM_PHC4_DIFF_B%'
OR b.parametername LIKE 'HECL_MAX._A%' OR b.parametername LIKE 'HECL_MAX._B%'
OR b.parametername LIKE 'HECL_MEAN_A%' OR b.parametername LIKE 'HECL_MEAN_B%'
OR b.parametername LIKE 'HECL_MEAN_DIFF_A%' OR b.parametername LIKE 'HECL_MEAN_DIFF_B%'
OR b.parametername LIKE 'HECL_MIN._A%' OR b.parametername LIKE 'HECL_MIN._B%'
OR b.parametername LIKE 'HELK_MEAN_DIFF_A%' OR b.parametername LIKE 'HELK_MEAN_DIFF_B%'
OR b.parametername LIKE 'HELK_MEAN_SLOPE_A%' OR b.parametername LIKE 'HELK_MEAN_SLOPE_B%'
OR b.parametername LIKE 'LOWERCHM_PRESS_DIFF_A%' OR b.parametername LIKE 'LOWERCHM_PRESS_DIFF_B%'
OR b.parametername LIKE 'PBK4_DIFF_A%' OR b.parametername LIKE 'PBK4_DIFF_B%'
OR b.parametername LIKE 'RF11_MAX._A%' OR b.parametername LIKE 'RF11_MAX._B%'
OR b.parametername LIKE 'RF11_MAX._DIFF_A%' OR b.parametername LIKE 'RF11_MAX._DIFF_B%'
OR b.parametername LIKE 'RF11_MEAN_A%' OR b.parametername LIKE 'RF11_MEAN_B%'
OR b.parametername LIKE 'RF21_MAX._A%' OR b.parametername LIKE 'RF21_MAX._B%'
OR b.parametername LIKE 'RF21_MEAN_A%' OR b.parametername LIKE 'RF21_MEAN_B%'
OR b.parametername LIKE 'RF21_MEAN_DIFF_A%' OR b.parametername LIKE 'RF21_MEAN_DIFF_B%'
--OR b.parametername LIKE 'RF21_MIN._A%' OR b.parametername LIKE 'RF21_MIN._B%'
OR b.parametername LIKE 'RF21_SD_A%' OR b.parametername LIKE 'RF21_SD_B%'
OR b.parametername LIKE 'RLD3_MEAN_A%' OR b.parametername LIKE 'RLD3_MEAN_B%'
OR b.parametername LIKE 'RLD3_MEAN_DIFF_A%' OR b.parametername LIKE 'RLD3_MEAN_DIFF_B%'
OR b.parametername LIKE 'RR13_MAX._A%' OR b.parametername LIKE 'RR13_MAX._B%'
OR b.parametername LIKE 'RR13_MAX._DIFF_A%' OR b.parametername LIKE 'RR13_MAX._DIFF_B%'
OR b.parametername LIKE 'RR23_MEAN_DIFF_A%' OR b.parametername LIKE 'RR23_MEAN_DIFF_B%'
OR b.parametername LIKE 'RR23_MEAN_SLOPE_A%' OR b.parametername LIKE 'RR23_MEAN_SLOPE_B%'
OR b.parametername LIKE 'RTN3_MEAN_A%' OR b.parametername LIKE 'RTN3_MEAN_B%'
OR b.parametername LIKE 'RTN3_MEAN_DIFF_A%' OR b.parametername LIKE 'RTN3_MEAN_DIFF_B%'
--OR b.parametername LIKE 'SOURCE_COEF_A%' OR b.parametername LIKE 'SOURCE_COEF_B%'
OR b.parametername LIKE 'SRTM_A%' OR b.parametername LIKE 'SRTM_B%'
OR b.parametername LIKE 'SRTM_SLOPE_A%' OR b.parametername LIKE 'SRTM_SLOPE_B%'
OR b.parametername LIKE 'TMP1_MAX._A%' OR b.parametername LIKE 'TMP1_MAX._B%'
OR b.parametername LIKE 'TMP1_MAX._DIFF_A%' OR b.parametername LIKE 'TMP1_MAX._DIFF_B%'
OR b.parametername LIKE 'TMP1_MEAN_A%' OR b.parametername LIKE 'TMP1_MEAN_B%'
--OR b.parametername LIKE 'TMP1_MEAN_DIFF_A%' OR b.parametername LIKE 'TMP1_MEAN_DIFF_B%'
OR b.parametername LIKE 'VOLT_MAX._A%' OR b.parametername LIKE 'VOLT_MAX._B%'
--OR b.parametername LIKE 'VOLT_MAX._DIFF_A%' OR b.parametername LIKE 'VOLT_MAX._DIFF_B%'
OR b.parametername LIKE 'VOLT_MEAN_A%' OR b.parametername LIKE 'VOLT_MEAN_B%'
OR b.parametername LIKE 'VOLT_MEAN_DIFF_A%' OR b.parametername LIKE 'VOLT_MEAN_DIFF_B%'
)