title: Add percentage on every columns on pivot table
body: <p>******Here is below query for your knowledge how to add percentge columns on every pivot columns let you give the answer 1</p>
  <p>I've created a SQL pivot and want to add a further calculated percentage column based on the aggregated figures within and I cannot figure out how to make this happen.****</p>
  <pre><code>DELETE TOP(100) PERCENT FROM PlusRS.rpt.RepairLevel
  
  -- STEP NO. 2
  INSERT INTO PlusRS.rpt.RepairLevel (ProgramID, PartNo, SerialNo, InvoiceFamily, RepairLevel)
  SELECT  WOH.ProgramID
        , WOH.PartNo
        , WOH.SerialNo
        , PNA.Value AS InvoiceFamily
        , CASE WOH.RepairTypeID WHEN 42 THEN '1' 
                                WHEN 71 THEN '3'
                                ELSE MAX(PLVL.Value)
          END AS RepairLevel      
  FROM pls.WOHeader WOH
  INNER JOIN pls.WOStationHistory WSH ON WOH.ID = WSH.WOHeaderID
  INNER JOIN pls.CodeRepairType CRT ON CRT.ID = WOH.RepairTypeID
  INNER JOIN pls.WOLine WOL ON WOL.WOHeaderID = WOH.ID AND WOL.StatusID = 14
  LEFT JOIN pls.PartNoAttribute PNA ON PNA.AttributeID = 354 AND PNA.ProgramID = WOH.ProgramID AND PNA.PartNo = WOH.PartNo 
  LEFT JOIN pls.PartNoAttribute PLVL ON PLVL.AttributeID = 149 AND PNA.ProgramID = WOH.ProgramID AND PLVL.PartNo = WOL.ComponentPartNo
  WHERE WSH.IsPass = 1 AND WSH.WorkStationID = 2  AND WOH.StatusID != 3
  AND woh.ProgramID = '10001' and CONVERT(Date, WSH.lastactivitydate) &gt;= '2022.04.11 ' AND CONVERT(Date, WSH.lastactivitydate) &lt;= '2022.06.15'
  GROUP BY  WOH.ProgramID
           , WOH.PartNo
           , WOH.SerialNo
           , PNA.Value
           , WOH.RepairTypeId
  
  UPDATE  PlusRS.rpt.RepairLevel SET BCRatio = CASE WHEN RepairLevel = '0' THEN 9 
                                                    WHEN RepairLevel = '1' THEN 0
                                                    WHEN RepairLevel = '2' THEN 53
                                                    WHEN RepairLevel = '2.5' THEN 0
                                                    WHEN RepairLevel = '3' THEN 38
                                                    WHEN RepairLevel = 'O' THEN 0 
                                                END
  
  Declare @sqlquery nvarchar(max),
  @column nvarchar(max);
  SELECT @column = COALESCE(@column + ',', '') + QUOTENAME(InvoiceFamily)
  FROM
  (
  SELECT Distinct InvoiceFamily
  FROM PlusRS.rpt.RepairLevel
  ) AS PIVOT_COLUMNS
  
  DECLARE @ColumnForSum AS NVARCHAR(MAX)
  SELECT @ColumnForSum =REPLACE(@column,',','+')
  SELECT @ColumnForSum =REPLACE(@ColumnForSum,'[','ISNULL([')
  SELECT @ColumnForSum =REPLACE(@ColumnForSum,']','],0)')
  
  SET @sqlquery =
  N'select * into #TempBag from
  (
  SELECT SerialNo as Qty
  , InvoiceFamily
  , RepairLevel
  , BCRatio
  FROM
  PlusRS.rpt.RepairLevel
  
  )as t
  pivot(
  count(qty)
  for InvoiceFamily In('+@column+')
  )As Pvt_table
  ORDER BY RepairLevel
  
  select t.*, SUM('+@ColumnForSum+') Total from #TempBag t
  GROUP BY RepairLevel,BCRatio,'+@column+'
  Drop Table #TempBag'
  EXEC sp_executesql @sqlquery**
  </code></pre>