I am just trying to left join and attach all the table but remaining table seems to be same is there any other way to attach the table like function or temp table to optimize it.
SELECT Business Lines
, No Of Events
, Gross Loss
, Net Loss as [Internal Fraud]
, No Of Events
, Gross Loss
, Net Loss as [External Fraud]
, No Of Events
, Gross Loss
, Net Loss as [Employment Practices & Workplace Safety]
, No Of Events
, Gross Loss
, Net Loss as [Clients, Products Business Practices]
, No Of Events
, Gross Loss
, Net Loss as [Damage to Physical Assets]
, No Of Events
, Gross Loss
, Net Loss as [Business Disruption and System Failures]
, No Of Events
, Gross Loss
, Net Loss as [Execution Delivery and Process Management]
UNION ALL
SELECT bbl.Name
,e1.e1 AS e1
,e1.n1 AS n1
,e1.g1 AS g1
,e2.e2 AS e2
,e2.n2 AS n2
,e2.g2 AS g2
,e3.e3 AS e3
,e3.n3 AS n3
,e3.g3 AS g3
,e4.e4 AS e4
,e4.n4 AS n4
,e4.g4 AS g4
,e5.e5 AS e5
,e5.n5 AS n5
,e5.g5 AS g5
,e6.e6 AS e6
,e6.n6 AS n6
,e6.g6 AS g6
,e7.e7 AS e7
,e7.n7 AS n7
,e7.g7 AS g7
FROM LossData.BaselBusinessLine bbl
LEFT JOIN (
SELECT cast(isnull(count(*), 0) AS VARCHAR(200)) AS e1
,cast(isnull(sum(NetLoss), 0) AS VARCHAR(200)) AS n1
,cast(isnull(sum(LossAmountTillDate), 0) AS VARCHAR(200)) AS g1
,BaselBusinessLineId
,BaselEventTypeOneId
FROM LossData.BaselBusinessLine bbl
JOIN LossData.Register r ON r.BaselBusinessLineId = bbl.Id
WHERE r.BaselEventTypeOneId = 1
GROUP BY
BaselBusinessLineId
,BaselEventTypeOneId
) e1 ON bbl.Id = e1.BaselBusinessLineId
LEFT JOIN (
SELECT cast(isnull(count(*), 0) AS VARCHAR(200)) AS e2
,cast(isnull(sum(NetLoss), 0) AS VARCHAR(200)) AS n2
,cast(isnull(sum(LossAmountTillDate), 0) AS VARCHAR(200)) AS g2
,BaselBusinessLineId
,BaselEventTypeOneId
FROM LossData.BaselBusinessLine bbl
JOIN LossData.Register r ON r.BaselBusinessLineId = bbl.Id
WHERE r.BaselEventTypeOneId = 2
GROUP BY
BaselBusinessLineId
,BaselEventTypeOneId
) e2 ON bbl.Id = e2.BaselBusinessLineId
LEFT JOIN (
SELECT cast(isnull(count(*), 0) AS VARCHAR(200)) AS e3
,cast(isnull(sum(NetLoss), 0) AS VARCHAR(200)) AS n3
,cast(isnull(sum(LossAmountTillDate), 0) AS VARCHAR(200)) AS g3
,BaselBusinessLineId
,BaselEventTypeOneId
FROM LossData.BaselBusinessLine bbl
JOIN LossData.Register r ON r.BaselBusinessLineId = bbl.Id
WHERE r.BaselEventTypeOneId = 3
GROUP BY
BaselBusinessLineId
,BaselEventTypeOneId
) e3 ON bbl.Id = e3.BaselBusinessLineId
LEFT JOIN (
SELECT cast(isnull(count(*), 0) AS VARCHAR(200)) AS e4
,cast(isnull(sum(NetLoss), 0) AS VARCHAR(200)) AS n4
,cast(isnull(sum(LossAmountTillDate), 0) AS VARCHAR(200)) AS g4
,BaselBusinessLineId
,BaselEventTypeOneId
FROM LossData.BaselBusinessLine bbl
JOIN LossData.Register r ON r.BaselBusinessLineId = bbl.Id
WHERE r.BaselEventTypeOneId = 4
GROUP BY
BaselBusinessLineId
,BaselEventTypeOneId
) e4 ON bbl.Id = e4.BaselBusinessLineId
LEFT JOIN (
SELECT cast(isnull(count(*), 0) AS VARCHAR(200)) AS e5
,cast(isnull(sum(NetLoss), 0) AS VARCHAR(200)) AS n5
,cast(isnull(sum(LossAmountTillDate), 0) AS VARCHAR(200)) AS g5
,BaselBusinessLineId
,BaselEventTypeOneId
FROM LossData.BaselBusinessLine bbl
JOIN LossData.Register r ON r.BaselBusinessLineId = bbl.Id
WHERE r.BaselEventTypeOneId = 5
GROUP BY
BaselBusinessLineId
,BaselEventTypeOneId
) e5 ON bbl.Id = e5.BaselBusinessLineId
LEFT JOIN (
SELECT cast(isnull(count(*), 0) AS VARCHAR(200)) AS e6
,cast(isnull(sum(NetLoss), 0) AS VARCHAR(200)) AS n6
,cast(isnull(sum(LossAmountTillDate), 0) AS VARCHAR(200)) AS g6
,BaselBusinessLineId
,BaselEventTypeOneId
FROM LossData.BaselBusinessLine bbl
JOIN LossData.Register r ON r.BaselBusinessLineId = bbl.Id
WHERE r.BaselEventTypeOneId = 6
GROUP BY
BaselBusinessLineId
,BaselEventTypeOneId
) e6 ON bbl.Id = e6.BaselBusinessLineId
LEFT JOIN (
SELECT cast(isnull(count(*), 0) AS VARCHAR(200)) AS e7
,cast(isnull(sum(NetLoss), 0) AS VARCHAR(200)) AS n7
,cast(isnull(sum(LossAmountTillDate), 0) AS VARCHAR(200)) AS g7
,BaselBusinessLineId
,BaselEventTypeOneId
FROM LossData.BaselBusinessLine bbl
JOIN LossData.Register r ON r.BaselBusinessLineId = bbl.Id
WHERE r.BaselEventTypeOneId = 7
GROUP BY
BaselBusinessLineId
,BaselEventTypeOneId
) e7 ON bbl.Id = e7.BaselBusinessLineId