38. SQL SERVER - PIVOT | UNPIVOT

Опубликовано: 21 Февраль 2026
на канале: Tech2BI
46
1

In this session we will discuss about PIVOT and UNPIVOT

-- PIVOT AND UNPIVOT
-- In SQL, Pivot and Unpivot are relational operators that are used to transform one table into another in order to achieve more simpler view of table.
-- Conventionally we can say that Pivot operator converts the rows data of the table into the column data.
-- The Unpivot operator does the opposite that is it transform the column based data into rows.

-- Syntax for PIVOT:

select col1,clo2from tab_name
PIVOT
( AggregateFunction(COL1)
FOR PIVOTCOLUMN IN (PIVOTCOLUMNVALUES)
)
AS AliasName

-- Syntax for unpivot

SELECT col1,col2
FROM tab_name
UNPIVOT
(
AggregateFunction(ColumnToBeAggregated)
FOR PivotColumn IN (PivotColumnValues)
) AS AliasName

#sql #sqlserver #database #dataanalytics #dbms #sql #tech2bi #PIVOT #UNPIVOT