У меня есть запрос на обновление таблицы с более чем 5 миллионами записей и 270 столбцами.В настоящее время запрос выполняется более 20 часов.Как оптимизировать запрос
DECLARE @DateRange NVARCHAR(50)
SET @DateRange = ?
DECLARE @Charges TABLE
(
EntId VARCHAR(100) NOT NULL,
DateRange VARCHAR(100) NOT NULL,
Identifier UNIQUEIDENTIFIER NOT NULL,
Reason VARCHAR(100) NOT NULL,
PriceClass VARCHAR(100) NOT NULL,
AClass VARCHAR(100) NOT NULL,
Bieceps VARCHAR(100) NOT NULL,
abc DECIMAL(38,15) NOT NULL,
Rate DECIMAL(38,15) NOT NULL,
Total DECIMAL(38,15) NOT NULL
PRIMARY KEY (EntId, DateRange, Identifier, Reason, PriceClass, AClass, Bieceps)
);
INSERT INTO @Charges(EntId, DateRange, Identifier, Reason, PriceClass, AClass, Bieceps, abc, Rate, Total)
SELECT
EntId, DateRange, Identifier, Reason, PriceClass,
AClass, Bieceps, abc, Rate, Total
FROM
zera.Charges
WHERE
DateRange = @DateRange
;WITH CostCTE AS
(
SELECT *
FROM
(SELECT
Bieceps, EntId, Total, r.CostColumn
FROM
(SELECT
DateRange, Bieceps, EntId, PriceClass, Total
FROM
[zera].[DNUC] dnc
WHERE
DateRange = @DateRange
UNION ALL
SELECT
DateRange, duc.Bieceps, duc.EntId, PriceClass, Total
FROM
@Charges duc
JOIN
zera.Ezra pb ON pb.Identifier = duc.Identifier
WHERE
pb.ezratype <> 'qwerty'
UNION ALL
SELECT
DateRange, pbd.Bieceps, duc.EntId, PriceClass,(Total / Equipments.cnt)
FROM
@Charges duc
JOIN
zera.Kozra pbd ON pbd.Identifier = duc.Identifier
JOIN
zera.Ezra pb ON pb.Identifier = duc.Identifier
JOIN
(SELECT
COUNT(pbd.Bieceps) AS cnt, pb.Identifier AS Identifier, pb.EntId
FROM
[zera].[Kozra] pbd
JOIN
[zera].[Ezra] pb ON pbd.Identifier = pb.Identifier
WHERE
pbd.StartDate <= '2019-09-31' AND pbd.EndDate >= '2019-09-01'
AND pb.ezratype = 'qwerty'
GROUP BY
jaba, pb.EntId, pb.Identifier) AS Equipments ON Equipments.Identifier = duc.Identifier AND Equipments.orgUrn = duc.orgUrn
WHERE
pb.ezratype ='qwerty' AND duc.Reason = 'Overage'
GROUP BY
DateRange, pbd.Bieceps, duc.EntId, PriceClass, Total, cnt) c
JOIN
zivaTable r ON c.PriceClass = 'subru-class-'+ CAST(r.cid AS NVARCHAR(100)))tmp
PIVOT
(SUM(Total) FOR CostColumn
IN (Fee, GstFee, TotalExerciseCost, TotalTrainingCost, TotalInstrumentsCost)
) costs
)
,
ActivityCTE as
(
select * from(
SELECT EntId,Bieceps,r.ActivityColumn,c.ActivityAmount FROM
(
SELECT DateRange,Bieceps,EntId,PriceClass,1 as ActivityAmount
FROM [zera].[DNUC] dnc
WHERE DateRange = @DateRange
UNION ALL
SELECT DateRange,duc.Bieceps,duc.EntId,PriceClass,
CASE WHEN pb.ActivityType = 'Exercise' THEN abc/1024.0 ELSE abc END AS ActivityAmount
FROM @Charges duc
JOIN zera.Ezra pb ON pb.Identifier = duc.Identifier
)c
JOIN zivaTable r ON c.PriceClass = 'subru-class-'+ CAST(r.cid AS NVARCHAR(100))
)tmp
PIVOT
(
SUM(ActivityAmount) FOR ActivityColumn IN
(
Activity1, Activity2, Activity3, Activity4, Activity5, Activity6, Activity7, Activity8, Activity9, Activity10,
Activity11, Activity12, Activity13, Activity14, Activity15, Activity16, Activity17, Activity18, Activity19, Activity20,
Activity21, Activity22, Activity23, Activity24, Activity25, Activity26, Activity27, Activity28, Activity29, Activity30,
Activity31, Activity32, Activity33, Activity34, Activity35, Activity36, Activity37, Activity38, Activity39, Activity40,
Activity41, Activity42, Activity43, Activity44, Activity45, Activity46, Activity47, Activity48, Activity49, Activity50,
Activity51, Activity52, Activity53, Activity54, Activity55, Activity56, Activity57, Activity58, Activity59, Activity60,
Activity61, Activity62, Activity63, Activity64, Activity65, Activity66, Activity67, Activity68, Activity69, Activity70,
Activity71, Activity72, Activity73, Activity74, Activity75, Activity76, Activity77, Activity78, Activity79, Activity80,
Activity81, Activity82, Activity83, Activity84, Activity85, Activity86, Activity87, Activity88, Activity89, Activity90,
Activity91, Activity92, Activity93, Activity94, Activity95, Activity96, Activity97, Activity98, Activity99, Activity100,
Activity101, Activity102, Activity103, Activity104, Activity105, Activity106, Activity107, Activity108, Activity109, Activity110,
Activity111, Activity112, Activity113, Activity114, Activity115, Activity116, Activity117, Activity118, Activity119, Activity120,
Activity121, Activity122, Activity123, Activity124, Activity125, Activity126, Activity127, Activity128, Activity129, Activity130,
Activity131, Activity132, Activity133, Activity134, Activity135, Activity136, Activity137, Activity138, Activity139, Activity140,
Activity141, Activity142, Activity143, Activity144, Activity145, Activity146, Activity147, Activity148, Activity149, Activity150,
Activity151, Activity152, Activity153, Activity154, Activity155, Activity156, Activity157, Activity158, Activity159, Activity160,
Activity161, Activity162, Activity163, Activity164, Activity165, Activity166, Activity167, Activity168, Activity169, Activity170,
Activity171, Activity172, Activity173, Activity174, Activity175, Activity176, Activity177, Activity178, Activity179, Activity180,
Activity181, Activity182, Activity183, Activity184, Activity185, Activity186, Activity187, Activity188, Activity189, Activity190,
Activity191, Activity192, Activity193, Activity194, Activity195, Activity196, Activity197, Activity198, Activity199, Activity200,
Activity201, Activity202, Activity203, Activity204, Activity205, Activity206, Activity207, Activity208, Activity209, Activity210,
Activity211, Activity212, Activity213, Activity214, Activity215, Activity216, Activity217, Activity218, Activity219, Activity220,
Activity221, Activity222, Activity223, Activity224, Activity225, Activity226, Activity227, Activity228, Activity229, Activity230
)
) Activitys
)
UPDATE ssc SET
ssc.Fee = ISNULL(CostCTE.Fee,0),
ssc.GstFee= ISNULL(CostCTE.GstFee, 0),
ssc.TotalExerciseCost = ISNULL(CostCTE.TotalExerciseCost, 0),
ssc.TotalTrainingCost =ISNULL(CostCTE.TotalTrainingCost, 0),
ssc.TotalInstrumentsCost = ISNULL(CostCTE.TotalInstrumentsCost, 0),
ssc.ExerciseplanOverage = ISNULL(ssc.ExerciseplanOverage,0),
ssc.TrainingPlanOverage = ISNULL(ssc.TrainingPlanOverage,0),
ssc.Activity1 = ISNULL(ActivityCTE.Activity1, 0), ssc.Activity2 = ISNULL(ActivityCTE.Activity2, 0), ssc.Activity3 = ISNULL(ActivityCTE.Activity3, 0), ssc.Activity4 = ISNULL(ActivityCTE.Activity4, 0), ssc.Activity5 = ISNULL(ActivityCTE.Activity5, 0),
ssc.Activity6 = ISNULL(ActivityCTE.Activity6, 0), ssc.Activity7 = ISNULL(ActivityCTE.Activity7, 0), ssc.Activity8 = ISNULL(ActivityCTE.Activity8, 0), ssc.Activity9 = ISNULL(ActivityCTE.Activity9, 0), ssc.Activity10 = ISNULL(ActivityCTE.Activity10, 0),
ssc.Activity11 = ISNULL(ActivityCTE.Activity11, 0), ssc.Activity12 = ISNULL(ActivityCTE.Activity12, 0), ssc.Activity13 = ISNULL(ActivityCTE.Activity13, 0), ssc.Activity14 = ISNULL(ActivityCTE.Activity14, 0), ssc.Activity15 = ISNULL(ActivityCTE.Activity15, 0),
ssc.Activity16 = ISNULL(ActivityCTE.Activity16, 0), ssc.Activity17 = ISNULL(ActivityCTE.Activity17, 0), ssc.Activity18 = ISNULL(ActivityCTE.Activity18, 0), ssc.Activity19 = ISNULL(ActivityCTE.Activity19, 0), ssc.Activity20 = ISNULL(ActivityCTE.Activity20, 0),
ssc.Activity21 = ISNULL(ActivityCTE.Activity21, 0), ssc.Activity22 = ISNULL(ActivityCTE.Activity22, 0), ssc.Activity23 = ISNULL(ActivityCTE.Activity23, 0), ssc.Activity24 = ISNULL(ActivityCTE.Activity24, 0), ssc.Activity25 = ISNULL(ActivityCTE.Activity25, 0),
ssc.Activity26 = ISNULL(ActivityCTE.Activity26, 0), ssc.Activity27 = ISNULL(ActivityCTE.Activity27, 0), ssc.Activity28 = ISNULL(ActivityCTE.Activity28, 0), ssc.Activity29 = ISNULL(ActivityCTE.Activity29, 0), ssc.Activity30 = ISNULL(ActivityCTE.Activity30, 0),
ssc.Activity31 = ISNULL(ActivityCTE.Activity31, 0), ssc.Activity32 = ISNULL(ActivityCTE.Activity32, 0), ssc.Activity33 = ISNULL(ActivityCTE.Activity33, 0), ssc.Activity34 = ISNULL(ActivityCTE.Activity34, 0), ssc.Activity35 = ISNULL(ActivityCTE.Activity35, 0),
ssc.Activity36 = ISNULL(ActivityCTE.Activity36, 0), ssc.Activity37 = ISNULL(ActivityCTE.Activity37, 0), ssc.Activity38 = ISNULL(ActivityCTE.Activity38, 0), ssc.Activity39 = ISNULL(ActivityCTE.Activity39, 0), ssc.Activity40 = ISNULL(ActivityCTE.Activity40, 0),
ssc.Activity41 = ISNULL(ActivityCTE.Activity41, 0), ssc.Activity42 = ISNULL(ActivityCTE.Activity42, 0), ssc.Activity43 = ISNULL(ActivityCTE.Activity43, 0), ssc.Activity44 = ISNULL(ActivityCTE.Activity44, 0), ssc.Activity45 = ISNULL(ActivityCTE.Activity45, 0),
ssc.Activity46 = ISNULL(ActivityCTE.Activity46, 0), ssc.Activity47 = ISNULL(ActivityCTE.Activity47, 0), ssc.Activity48 = ISNULL(ActivityCTE.Activity48, 0), ssc.Activity49 = ISNULL(ActivityCTE.Activity49, 0), ssc.Activity50 = ISNULL(ActivityCTE.Activity50, 0),
ssc.Activity51 = ISNULL(ActivityCTE.Activity51, 0), ssc.Activity52 = ISNULL(ActivityCTE.Activity52, 0), ssc.Activity53 = ISNULL(ActivityCTE.Activity53, 0), ssc.Activity54 = ISNULL(ActivityCTE.Activity54, 0), ssc.Activity55 = ISNULL(ActivityCTE.Activity55, 0),
ssc.Activity56 = ISNULL(ActivityCTE.Activity56, 0), ssc.Activity57 = ISNULL(ActivityCTE.Activity57, 0), ssc.Activity58 = ISNULL(ActivityCTE.Activity58, 0), ssc.Activity59 = ISNULL(ActivityCTE.Activity59, 0), ssc.Activity60 = ISNULL(ActivityCTE.Activity60, 0),
ssc.Activity61 = ISNULL(ActivityCTE.Activity61, 0), ssc.Activity62 = ISNULL(ActivityCTE.Activity62, 0), ssc.Activity63 = ISNULL(ActivityCTE.Activity63, 0), ssc.Activity64 = ISNULL(ActivityCTE.Activity64, 0), ssc.Activity65 = ISNULL(ActivityCTE.Activity65, 0),
ssc.Activity66 = ISNULL(ActivityCTE.Activity66, 0), ssc.Activity67 = ISNULL(ActivityCTE.Activity67, 0), ssc.Activity68 = ISNULL(ActivityCTE.Activity68, 0), ssc.Activity69 = ISNULL(ActivityCTE.Activity69, 0), ssc.Activity70 = ISNULL(ActivityCTE.Activity70, 0),
ssc.Activity71 = ISNULL(ActivityCTE.Activity71, 0), ssc.Activity72 = ISNULL(ActivityCTE.Activity72, 0), ssc.Activity73 = ISNULL(ActivityCTE.Activity73, 0), ssc.Activity74 = ISNULL(ActivityCTE.Activity74, 0), ssc.Activity75 = ISNULL(ActivityCTE.Activity75, 0),
ssc.Activity76 = ISNULL(ActivityCTE.Activity76, 0), ssc.Activity77 = ISNULL(ActivityCTE.Activity77, 0), ssc.Activity78 = ISNULL(ActivityCTE.Activity78, 0), ssc.Activity79 = ISNULL(ActivityCTE.Activity79, 0), ssc.Activity80 = ISNULL(ActivityCTE.Activity80, 0),
ssc.Activity81 = ISNULL(ActivityCTE.Activity81, 0), ssc.Activity82 = ISNULL(ActivityCTE.Activity82, 0), ssc.Activity83 = ISNULL(ActivityCTE.Activity83, 0), ssc.Activity84 = ISNULL(ActivityCTE.Activity84, 0), ssc.Activity85 = ISNULL(ActivityCTE.Activity85, 0),
ssc.Activity86 = ISNULL(ActivityCTE.Activity86, 0), ssc.Activity87 = ISNULL(ActivityCTE.Activity87, 0), ssc.Activity88 = ISNULL(ActivityCTE.Activity88, 0), ssc.Activity89 = ISNULL(ActivityCTE.Activity89, 0), ssc.Activity90 = ISNULL(ActivityCTE.Activity90, 0),
ssc.Activity91 = ISNULL(ActivityCTE.Activity91, 0), ssc.Activity92 = ISNULL(ActivityCTE.Activity92, 0), ssc.Activity93 = ISNULL(ActivityCTE.Activity93, 0), ssc.Activity94 = ISNULL(ActivityCTE.Activity94, 0), ssc.Activity95 = ISNULL(ActivityCTE.Activity95, 0),
ssc.Activity96 = ISNULL(ActivityCTE.Activity96, 0), ssc.Activity97 = ISNULL(ActivityCTE.Activity97, 0), ssc.Activity98 = ISNULL(ActivityCTE.Activity98, 0), ssc.Activity99 = ISNULL(ActivityCTE.Activity99, 0), ssc.Activity100 = ISNULL(ActivityCTE.Activity100, 0),
ssc.Activity101 = ISNULL(ActivityCTE.Activity101, 0), ssc.Activity102 = ISNULL(ActivityCTE.Activity102, 0), ssc.Activity103 = ISNULL(ActivityCTE.Activity103, 0), ssc.Activity104 = ISNULL(ActivityCTE.Activity104, 0), ssc.Activity105 = ISNULL(ActivityCTE.Activity105, 0),
ssc.Activity106 = ISNULL(ActivityCTE.Activity106, 0), ssc.Activity107 = ISNULL(ActivityCTE.Activity107, 0), ssc.Activity108 = ISNULL(ActivityCTE.Activity108, 0), ssc.Activity109 = ISNULL(ActivityCTE.Activity109, 0), ssc.Activity110 = ISNULL(ActivityCTE.Activity110, 0),
ssc.Activity111 = ISNULL(ActivityCTE.Activity111, 0), ssc.Activity112 = ISNULL(ActivityCTE.Activity112, 0), ssc.Activity113 = ISNULL(ActivityCTE.Activity113, 0), ssc.Activity114 = ISNULL(ActivityCTE.Activity114, 0), ssc.Activity115 = ISNULL(ActivityCTE.Activity115, 0),
ssc.Activity116 = ISNULL(ActivityCTE.Activity116, 0), ssc.Activity117 = ISNULL(ActivityCTE.Activity117, 0), ssc.Activity118 = ISNULL(ActivityCTE.Activity118, 0), ssc.Activity119 = ISNULL(ActivityCTE.Activity119, 0), ssc.Activity120 = ISNULL(ActivityCTE.Activity120, 0),
ssc.Activity121 = ISNULL(ActivityCTE.Activity121, 0), ssc.Activity122 = ISNULL(ActivityCTE.Activity122, 0), ssc.Activity123 = ISNULL(ActivityCTE.Activity123, 0), ssc.Activity124 = ISNULL(ActivityCTE.Activity124, 0), ssc.Activity125 = ISNULL(ActivityCTE.Activity125, 0),
ssc.Activity126 = ISNULL(ActivityCTE.Activity126, 0), ssc.Activity127 = ISNULL(ActivityCTE.Activity127, 0), ssc.Activity128 = ISNULL(ActivityCTE.Activity128, 0), ssc.Activity129 = ISNULL(ActivityCTE.Activity129, 0), ssc.Activity130 = ISNULL(ActivityCTE.Activity130, 0),
ssc.Activity131 = ISNULL(ActivityCTE.Activity131, 0), ssc.Activity132 = ISNULL(ActivityCTE.Activity132, 0), ssc.Activity133 = ISNULL(ActivityCTE.Activity133, 0), ssc.Activity134 = ISNULL(ActivityCTE.Activity134, 0), ssc.Activity135 = ISNULL(ActivityCTE.Activity135, 0),
ssc.Activity136 = ISNULL(ActivityCTE.Activity136, 0), ssc.Activity137 = ISNULL(ActivityCTE.Activity137, 0), ssc.Activity138 = ISNULL(ActivityCTE.Activity138, 0), ssc.Activity139 = ISNULL(ActivityCTE.Activity139, 0), ssc.Activity140 = ISNULL(ActivityCTE.Activity140, 0),
ssc.Activity141 = ISNULL(ActivityCTE.Activity141, 0), ssc.Activity142 = ISNULL(ActivityCTE.Activity142, 0), ssc.Activity143 = ISNULL(ActivityCTE.Activity143, 0), ssc.Activity144 = ISNULL(ActivityCTE.Activity144, 0), ssc.Activity145 = ISNULL(ActivityCTE.Activity145, 0),
ssc.Activity146 = ISNULL(ActivityCTE.Activity146, 0), ssc.Activity147 = ISNULL(ActivityCTE.Activity147, 0), ssc.Activity148 = ISNULL(ActivityCTE.Activity148, 0), ssc.Activity149 = ISNULL(ActivityCTE.Activity149, 0), ssc.Activity150 = ISNULL(ActivityCTE.Activity150, 0),
ssc.Activity151 = ISNULL(ActivityCTE.Activity151, 0), ssc.Activity152 = ISNULL(ActivityCTE.Activity152, 0), ssc.Activity153 = ISNULL(ActivityCTE.Activity153, 0), ssc.Activity154 = ISNULL(ActivityCTE.Activity154, 0), ssc.Activity155 = ISNULL(ActivityCTE.Activity155, 0),
ssc.Activity156 = ISNULL(ActivityCTE.Activity156, 0), ssc.Activity157 = ISNULL(ActivityCTE.Activity157, 0), ssc.Activity158 = ISNULL(ActivityCTE.Activity158, 0), ssc.Activity159 = ISNULL(ActivityCTE.Activity159, 0), ssc.Activity160 = ISNULL(ActivityCTE.Activity160, 0),
ssc.Activity161 = ISNULL(ActivityCTE.Activity161, 0), ssc.Activity162 = ISNULL(ActivityCTE.Activity162, 0), ssc.Activity163 = ISNULL(ActivityCTE.Activity163, 0), ssc.Activity164 = ISNULL(ActivityCTE.Activity164, 0), ssc.Activity165 = ISNULL(ActivityCTE.Activity165, 0),
ssc.Activity166 = ISNULL(ActivityCTE.Activity166, 0), ssc.Activity167 = ISNULL(ActivityCTE.Activity167, 0), ssc.Activity168 = ISNULL(ActivityCTE.Activity168, 0), ssc.Activity169 = ISNULL(ActivityCTE.Activity169, 0), ssc.Activity170 = ISNULL(ActivityCTE.Activity170, 0),
ssc.Activity171 = ISNULL(ActivityCTE.Activity171, 0), ssc.Activity172 = ISNULL(ActivityCTE.Activity172, 0), ssc.Activity173 = ISNULL(ActivityCTE.Activity173, 0), ssc.Activity174 = ISNULL(ActivityCTE.Activity174, 0), ssc.Activity175 = ISNULL(ActivityCTE.Activity175, 0),
ssc.Activity176 = ISNULL(ActivityCTE.Activity176, 0), ssc.Activity177 = ISNULL(ActivityCTE.Activity177, 0), ssc.Activity178 = ISNULL(ActivityCTE.Activity178, 0), ssc.Activity179 = ISNULL(ActivityCTE.Activity179, 0), ssc.Activity180 = ISNULL(ActivityCTE.Activity180, 0),
ssc.Activity181 = ISNULL(ActivityCTE.Activity181, 0), ssc.Activity182 = ISNULL(ActivityCTE.Activity182, 0), ssc.Activity183 = ISNULL(ActivityCTE.Activity183, 0), ssc.Activity184 = ISNULL(ActivityCTE.Activity184, 0), ssc.Activity185 = ISNULL(ActivityCTE.Activity185, 0),
ssc.Activity186 = ISNULL(ActivityCTE.Activity186, 0), ssc.Activity187 = ISNULL(ActivityCTE.Activity187, 0), ssc.Activity188 = ISNULL(ActivityCTE.Activity188, 0), ssc.Activity189 = ISNULL(ActivityCTE.Activity189, 0), ssc.Activity190 = ISNULL(ActivityCTE.Activity190, 0),
ssc.Activity191 = ISNULL(ActivityCTE.Activity191, 0), ssc.Activity192 = ISNULL(ActivityCTE.Activity192, 0), ssc.Activity193 = ISNULL(ActivityCTE.Activity193, 0), ssc.Activity194 = ISNULL(ActivityCTE.Activity194, 0), ssc.Activity195 = ISNULL(ActivityCTE.Activity195, 0),
ssc.Activity196 = ISNULL(ActivityCTE.Activity196, 0), ssc.Activity197 = ISNULL(ActivityCTE.Activity197, 0), ssc.Activity198 = ISNULL(ActivityCTE.Activity198, 0), ssc.Activity199 = ISNULL(ActivityCTE.Activity199, 0), ssc.Activity200 = ISNULL(ActivityCTE.Activity200, 0),
ssc.Activity201 = ISNULL(ActivityCTE.Activity201, 0), ssc.Activity202 = ISNULL(ActivityCTE.Activity202, 0), ssc.Activity203 = ISNULL(ActivityCTE.Activity203, 0), ssc.Activity204 = ISNULL(ActivityCTE.Activity204, 0), ssc.Activity205 = ISNULL(ActivityCTE.Activity205, 0),
ssc.Activity206 = ISNULL(ActivityCTE.Activity206, 0), ssc.Activity207 = ISNULL(ActivityCTE.Activity207, 0), ssc.Activity208 = ISNULL(ActivityCTE.Activity208, 0), ssc.Activity209 = ISNULL(ActivityCTE.Activity209, 0), ssc.Activity210 = ISNULL(ActivityCTE.Activity210, 0),
ssc.Activity211 = ISNULL(ActivityCTE.Activity211, 0), ssc.Activity212 = ISNULL(ActivityCTE.Activity212, 0), ssc.Activity213 = ISNULL(ActivityCTE.Activity213, 0), ssc.Activity214 = ISNULL(ActivityCTE.Activity214, 0), ssc.Activity215 = ISNULL(ActivityCTE.Activity215, 0),
ssc.Activity216 = ISNULL(ActivityCTE.Activity216, 0), ssc.Activity217 = ISNULL(ActivityCTE.Activity217, 0), ssc.Activity218 = ISNULL(ActivityCTE.Activity218, 0), ssc.Activity219 = ISNULL(ActivityCTE.Activity219, 0), ssc.Activity220 = ISNULL(ActivityCTE.Activity220, 0),
ssc.Activity221 = ISNULL(ActivityCTE.Activity221, 0), ssc.Activity222 = ISNULL(ActivityCTE.Activity222, 0), ssc.Activity223 = ISNULL(ActivityCTE.Activity223, 0), ssc.Activity224 = ISNULL(ActivityCTE.Activity224, 0), ssc.Activity225 = ISNULL(ActivityCTE.Activity225, 0),
ssc.Activity226 = ISNULL(ActivityCTE.Activity226, 0), ssc.Activity227 = ISNULL(ActivityCTE.Activity227, 0), ssc.Activity228 = ISNULL(ActivityCTE.Activity228, 0), ssc.Activity229 = ISNULL(ActivityCTE.Activity229, 0), ssc.Activity230 = ISNULL(ActivityCTE.Activity230, 0)
FROM zeraT.MainTable ssc
LEFT OUTER JOIN CostCTE on CostCTE.EntId = ssc.EntId and CostCTE.Bieceps = ssc.Bieceps
LEFT OUTER JOIN ActivityCTE ON ssc.EntId = ActivityCTE.EntId and ssc.Bieceps = ActivityCTE.Bieceps