Optimize this Sql server 14 code. Give output in text, not in code snippet editor. Don't add explanations, Also make sure you remember this code as code-1, So when i ask you you should remember it. Break the optimized code into 8 parts, And remember those parts. I will ask you the optimized code one by one. For this answer give me part1 and part2. Because due to character limit, i don't get the whole code at once. Here is the CODE :-
;with X as 
(
Select            Barcode, Start_datetime, Mater_code as Recipe,Serial_id as Batch_ID, Done_rtime as Mixing_Time,
                Done_allrtime as Cycle_Time,Bwb_time as BWB_Time, Lot_energy as Batch_Total_Energy,
                AlarmStr as Alarms ,oiltime as OilInjectionTime,oilfeedtime as OilFeeding_Time_Taken,oilenergy,oiltemp,
                AddSilaneTime as AddSilane_Time_Taken,AddSilaneBTime as AddSilaneB_Time_Taken, 
                Case When oiltime is NULL then 'No' else 'Yes' end as OilBased_Recipe_Status,
                Case When AddSilaneTime =0 and AddSilaneBTime = 0 then 'No' else 'Yes' end as SilaneBased_Recipe_Status
from Ppt_lot
where Start_datetime between '2023-02-27 00:00:00.000' and '2023-02-28 00:00:00.000'
--order by Barcode
),
Curve_Lot_Merge as
(
SELECT cd.Barcode, cd.curve_data, plt.Start_datetime
FROM Ppt_curvedata cd
LEFT JOIN Ppt_lot plt ON plt.Barcode = cd.Barcode
where Start_datetime between '2023-02-27 00:00:00.000' and '2023-02-28 00:00:00.000'
),
Y as
(
SELECT 
CONCAT(Cast(Barcode as varchar(50)) ,RIGHT('000' + CAST(FLOOR(CAST(SUBSTRING(flat, 1, CHARINDEX(':', flat)-1)  AS FLOAT)) AS VARCHAR(3)), 3))  as Unique_Key_Y,
   barcode,
    SUBSTRING(flat, 1, CHARINDEX(':', flat)-1) as CT_Sec_Incremental,
    RIGHT(REPLICATE('0', 3) + CAST(CAST(SUBSTRING(flat, 1, CHARINDEX(':', flat)-1) AS DECIMAL(10,2)) AS INT), 3) as MixtimeX,     SUBSTRING(flat, CHARINDEX(':', flat)+1,
                    CHARINDEX(':', flat, CHARINDEX(':', flat)+1)
                                        -CHARINDEX(':', flat)-1) as MixTemp_Sec_Incremental,
    SUBSTRING(flat, CHARINDEX(':', flat, CHARINDEX(':', flat)+1)+1,
                    CHARINDEX(':', flat, CHARINDEX(':', flat, CHARINDEX(':', flat)+1)+1)
                                        -CHARINDEX(':', flat, CHARINDEX(':', flat)+1)-1) as MixPower_Sec_Incremental,
    SUBSTRING(flat, CHARINDEX(':', flat, CHARINDEX(':', flat, CHARINDEX(':', flat)+1)+1)+1,
                    CHARINDEX(':', flat, CHARINDEX(':', flat, CHARINDEX(':', flat, CHARINDEX(':', flat)+1)+1)+1)
                                        -CHARINDEX(':', flat, CHARINDEX(':', flat, CHARINDEX(':', flat)+1)+1)-1) as MixEnergy_Sec_Incremental,
    SUBSTRING(flat, CHARINDEX(':', flat, CHARINDEX(':', flat, CHARINDEX(':', flat, CHARINDEX(':', flat)+1)+1)+1)+1,
                    CHARINDEX(':', flat, CHARINDEX(':', flat, CHARINDEX(':', flat, CHARINDEX(':', flat,CHARINDEX(':', flat)+1)+1)+1)+1)
                                        -CHARINDEX(':', flat, CHARINDEX(':', flat, CHARINDEX(':', flat,CHARINDEX(':', flat)+1)+1)+1)-1) as MixPressure_Sec_Incremental,
    SUBSTRING(flat, CHARINDEX(':', flat, CHARINDEX(':', flat, CHARINDEX(':', flat, CHARINDEX(':', flat, CHARINDEX(':', flat)+1)+1)+1)+1)+1,
                    CHARINDEX(':', flat, CHARINDEX(':', flat, CHARINDEX(':', flat, CHARINDEX(':', flat, CHARINDEX(':', flat, CHARINDEX(':', flat)+1)+1)+1)+1)+1)
                                        -CHARINDEX(':', flat, CHARINDEX(':', flat, CHARINDEX(':', flat, CHARINDEX(':', flat, CHARINDEX(':', flat)+1)+1)+1)+1)-1) as MixSpeed_Sec_Incremental
--    ,SUBSTRING(flat, CHARINDEX(':', flat, CHARINDEX(':', flat, CHARINDEX(':', flat, CHARINDEX(':', flat, CHARINDEX(':', flat, CHARINDEX(':', flat)+1)+1)+1)+1)+1)+1, LEN(flat)) AS Rest
FROM (
    SELECT
        barcode,
        LTRIM(RTRIM(Split.a.value('.', 'NVARCHAR(150)'))) AS flat
    FROM 
    (
        SELECT 
            barcode, 
            CAST('<M>'+REPLACE(CONVERT(NVARCHAR(MAX), curve_data), '/', '</M><M>')+'</M>' AS XML) AS flat_data 
        FROM [Curve_Lot_Merge]
    ) flat_tbl
    CROSS APPLY flat_data.nodes('/M') AS Split(a)
) t 
WHERE t.flat LIKE '%:%'
),
mix_term_act_merge as
(
SELECT m.*, a.act_name, tm.term_name,plt.Start_datetime ,
            CONCAT(Cast(m.Barcode as varchar(50)) ,RIGHT('000' + CAST(FLOOR(CAST(m.MixTime AS FLOAT)) AS VARCHAR(3)), 3))  as Unique_Key_mix_term_act_merge
FROM ppt_mixdata m
LEFT JOIN pmt_act a
ON m.Actcode = a.act_addr
LEFT JOIN pmt_term tm
ON m.Termcode = tm.term_addr
LEFT JOIN Ppt_lot plt ON plt.Barcode = m.Barcode
where Start_datetime between '2023-02-27 00:00:00.000' and '2023-02-28 00:00:00.000'
) Select 
       CASE WHEN Y.Unique_Key_Y IS NULL tHEN mix_term_act_merge.Unique_Key_mix_term_act_merge ELSE Y.Unique_Key_Y end as Unique_Key,
--       Y.Unique_Key_Y,
--       mix_term_act_merge.Unique_Key_mix_term_act_merge,
       X.Barcode, 
       X.start_datetime,
       X.Recipe,
       X.Batch_ID,
       X.Mixing_Time, 
       X.Cycle_Time,
       X.BWB_Time,
       y.CT_Sec_Incremental,
       Y.MixEnergy_Sec_Incremental,
       Y.MixPower_Sec_Incremental,
       Y.MixPressure_Sec_Incremental,
       Y.MixSpeed_Sec_Incremental,
       Y.MixTemp_Sec_Incremental,
       X.Batch_Total_Energy,
       mix_term_act_merge.Serial as Mix_ID,
       mix_term_act_merge.Actcode as Step_Actcode,
       mix_term_act_merge.Termcode as Step_TermCode,
       mix_term_act_merge.MixTime as Step_MixTime,
       mix_term_act_merge.MixTemp as Step_MixTemp,
       mix_term_act_merge.MixPower as Step_MixPower,
       mix_term_act_merge.MixPress as Step_MixPressure,
       mix_term_act_merge.MixSpeed as Step_MixSpeed,
       mix_term_act_merge.MixEner as Step_MixEnergy,
       mix_term_act_merge.act_name as Step_ActName,
       mix_term_act_merge.term_name as Step_TermName
into #temp1
from x left join y on X.Barcode = Y.Barcode
full outer join mix_term_act_merge on  Y.Unique_Key_Y = mix_term_act_merge.Unique_Key_mix_term_act_merge
                    and mix_term_act_merge.MixTime = Y.MixtimeX
ORDER BY Unique_Key asc, mix_term_act_merge.Serial asc 

;With fillgrp as
(
Select *,
            ROW_NUMBER() OVER(ORDER BY Unique_Key) AS index_copy
from #temp1
)

Select 
	   t1.Unique_Key,
       COALESCE(Barcode, 
       (SELECT TOP 1 Barcode 
        FROM fillgrp t2 
        WHERE t2.index_copy > t1.index_copy AND Barcode IS NOT NULL 
        ORDER BY t2.index_copy asc)) AS Barcode,
		COALESCE(start_datetime, 
       (SELECT TOP 1 start_datetime 
        FROM fillgrp t2 
        WHERE t2.index_copy > t1.index_copy AND start_datetime IS NOT NULL 
        ORDER BY t2.index_copy asc)) AS start_datetime, 
		COALESCE(Recipe, 
       (SELECT TOP 1 Recipe 
        FROM fillgrp t2 
        WHERE t2.index_copy > t1.index_copy AND Recipe IS NOT NULL 
        ORDER BY t2.index_copy asc)) AS Recipe, 		
		COALESCE(Batch_ID, 
       (SELECT TOP 1 Batch_ID 
        FROM fillgrp t2 
        WHERE t2.index_copy > t1.index_copy AND Batch_ID IS NOT NULL 
        ORDER BY t2.index_copy asc)) AS Batch_ID, 
		COALESCE(Mixing_Time, 
       (SELECT TOP 1 Mixing_Time 
        FROM fillgrp t2 
        WHERE t2.index_copy > t1.index_copy AND Mixing_Time IS NOT NULL 
        ORDER BY t2.index_copy asc)) AS Mixing_Time, 
		COALESCE(Cycle_Time, 
       (SELECT TOP 1 Cycle_Time 
        FROM fillgrp t2 
        WHERE t2.index_copy > t1.index_copy AND Cycle_Time IS NOT NULL 
        ORDER BY t2.index_copy asc)) AS Cycle_Time, 
		COALESCE(BWB_Time, 
       (SELECT TOP 1 BWB_Time 
        FROM fillgrp t2 
        WHERE t2.index_copy > t1.index_copy AND BWB_Time IS NOT NULL 
        ORDER BY t2.index_copy asc)) AS BWB_Time
       ,t1.CT_Sec_Incremental,
       t1.MixEnergy_Sec_Incremental,
       t1.MixPower_Sec_Incremental,
       t1.MixPressure_Sec_Incremental,
       t1.MixSpeed_Sec_Incremental,
       t1.MixTemp_Sec_Incremental,
       t1.Batch_Total_Energy,
	   t1.Step_MixTime ,
        t1.Step_MixTemp,
        t1.Step_MixPower,
        t1.Step_MixPressure ,
        t1.Step_MixSpeed ,
        t1.Step_MixEnergy,
	   COALESCE(Step_ActName, 
       (SELECT TOP 1 Step_ActName 
        FROM fillgrp t2 
        WHERE t2.index_copy < t1.index_copy AND Step_ActName IS NOT NULL 
        ORDER BY t2.index_copy desc))AS Step_Act_Name,
		t1.Step_TermName,
		COALESCE(Mix_ID , 
       (SELECT TOP 1 Mix_ID 
        FROM fillgrp t2 
        WHERE t2.index_copy < t1.index_copy AND Mix_ID IS NOT NULL 
        ORDER BY t2.index_copy desc) )AS Mix_ID

from [fillgrp] t1
drop table #temp1

