Hi @Farhan Jamil
There're several methods to achieve multiple Pivots:
First, If you want to keep using the PIVOT keyword twice ,then make some changes to your original query code,like this:
;with CompanySalesUnitSold as
(
......
)
select [Product Code],Product_Description,
max(cast(isnull([QtySoldLast6Days],0) as int)) as [QuantitySoldInLast6Days],
max(cast(isnull([QtySoldLast5Days],0)as int))as [QuantitySoldInLast5Days],
max(cast(isnull([QtySoldLast4Days],0)as int))as [QuantitySoldInLast4Days],
max(cast(isnull([QtySoldLast3Days],0)as int))As [QuantitySoldInLast3Days],
max(cast(isnull([QtySoldLast2Days],0)as int))As [QuantitySoldInLast2Days],
max(cast(isnull([QtySoldLast1Days],0)as int))As [QuantitySoldInLast1Days],
max(cast(isnull([SalesinLast6Days],0)as int)) as [SalesinLast6Days],
max(cast(isnull([SalesinLast5Days],0)as int))as [SalesinLast5Days],
max(cast(isnull([SalesinLast4Days],0)as int))as [SalesinLast4Days],
max(cast(isnull([SalesinLast3Days],0)as int))As [SalesinLast3Days],
max(cast(isnull([SalesinLast2Days],0)as int))As [SalesinLast2Days],
max(cast(isnull([SalesinLast1Days],0)as int))As [SalesinLast1Days]
into #UnitSold
from (
select [Product Code],Product_Description,sum(UnitsSold) as UnitsSold,sum(SALE_NET_VAL) as TotalSales,UnitSoldBD,SalesBD
from CompanySalesUnitSold with(nolock)
group by [Product Code],Product_Description,UnitSoldBD,SalesBD
)as pt
pivot(max(UnitsSold) for UnitSoldBD in([QtySoldLast6Days],[QtySoldLast5Days],[QtySoldLast4Days],[QtySoldLast3Days],[QtySoldLast2Days],[QtySoldLast1Days]))Pivottable
pivot(max(TotalSales) for SalesBD in([SalesinLast6Days],[SalesinLast5Days],[SalesinLast4Days],[SalesinLast3Days],[SalesinLast2Days],[SalesinLast1Days]))Pivottable2
group by [Product Code],Product_Description
Second, use CASE WHEN along with Aggregate Function,like this:
;WITH CompanySalesUnitSold AS
(......
)
SELECT [Product Code],Product_Description,
SUM(CASE WHEN UnitSoldBD='QtySoldLast6Days' THEN UnitsSold END) AS QtySoldLast6Days,
SUM(CASE WHEN UnitSoldBD='QtySoldLast5Days' THEN UnitsSold END) AS QtySoldLast5Days,
SUM(CASE WHEN UnitSoldBD='QtySoldLast4Days' THEN UnitsSold END) AS QtySoldLast4Days,
SUM(CASE WHEN UnitSoldBD='QtySoldLast3Days' THEN UnitsSold END) AS QtySoldLast3Days,
SUM(CASE WHEN UnitSoldBD='QtySoldLast2Days' THEN UnitsSold END) AS QtySoldLast2Days,
SUM(CASE WHEN UnitSoldBD='QtySoldLast1Days' THEN UnitsSold END) AS QtySoldLast1Days,
SUM(CASE WHEN SalesBD='SalesinLast6Days' THEN SALE_NET_VAL END) AS SalesinLast6Days,
SUM(CASE WHEN SalesBD='SalesinLast5Days' THEN SALE_NET_VAL END) AS SalesinLast5Days,
SUM(CASE WHEN SalesBD='SalesinLast4Days' THEN SALE_NET_VAL END) AS SalesinLast4Days,
SUM(CASE WHEN SalesBD='SalesinLast3Days' THEN SALE_NET_VAL END) AS SalesinLast3Days,
SUM(CASE WHEN SalesBD='SalesinLast2Days' THEN SALE_NET_VAL END) AS SalesinLast2Days,
SUM(CASE WHEN SalesBD='SalesinLast1Days' THEN SALE_NET_VAL END) AS SalesinLast1Days
FROM CompanySalesUnitSold
GROUP BY [Product Code],Product_Description
Third, you can unpivot first, then pivot the data into the final result that you want.Check this:
;with CompanySalesUnitSold as
(
......
),CTE1 AS
(
select [Product Code],Product_Description,sum(UnitsSold) as UnitsSold,sum(SALE_NET_VAL) as TotalSales,UnitSoldBD,SalesBD
from CompanySalesUnitSold with(nolock)
group by [Product Code],Product_Description,UnitSoldBD,SalesBD
),CTE2 AS
(
SELECT [Product Code],Product_Description,C.VALUE,C.COLUMN_NAME
FROM #test CROSS APPLY(VALUES(UnitsSold,UnitSoldBD),
(TotalSales,SalesBD))C(VALUE,COLUMN_NAME)
)
SELECT *
FROM CTE2 PIVOT(MAX(VALUE) FOR COLUMN_NAME IN([QtySoldLast6Days],[QtySoldLast5Days],[QtySoldLast4Days],[QtySoldLast3Days],[QtySoldLast2Days],[QtySoldLast1Days],
[SalesinLast6Days],[SalesinLast5Days],[SalesinLast4Days],[SalesinLast3Days],[SalesinLast2Days],[SalesinLast1Days]))P
BTW,since you didn't provide sample data, I can only write the query code, I can't test whether it can be executed normally and get the result. So while asking questions, please simplify the problem and provide sample data and the result you want to achieve. For example, for this issue, you need to create a temp table which contains the sample data before pivoting.
Best regards,
LiHong
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.