'SQL Server 2019 - Pivot Result on multiple column

Please check my query and let me know what to do to get the proper result as expected - query:

With ChemReq (AmdCode, ItemCode, itemcode1, D1K,D2K,D3K,D1,D2,D3)
as
(SELECT * FROM (
  SELECT t.AmdCode, t.ItemCode, t.itemcode1, t.BomCode, t.Requirement, t.PDay, t.PDayQty FROM Temp_Chemical t  
) ChemReq
PIVOT (
  SUM(Requirement) FOR PDayQty
  IN (D1K, D2K, D3K)
) AS Req
PIVOT (
  max(bomcode) FOR PDay
  IN (D1, D2, D3)
) AS Req1
)
Select amdcode,itemcode,itemcode1,D1K,D2K,D3K,D1,D2,D3 from ChemReq  
Group By AmdCode, ItemCode, itemcode1, D1K,D2K,D3K,D1,D2,D3
GO

enter image description here



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source