Skip to content

Commit 30fa75e

Browse files
authored
Saving UNPIVOT results in Fabric
1 parent 7dc629b commit 30fa75e

1 file changed

Lines changed: 10 additions & 0 deletions

File tree

docs/t-sql/queries/from-using-pivot-and-unpivot.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,16 @@ If the column is combined with other columns, then a collate clause (`COLLATE DA
102102

103103
In [!INCLUDE [fabric](../../includes/fabric.md)] and [!INCLUDE [ssazuresynapse_md](../../includes/ssazuresynapse-md.md)] pools, queries with `PIVOT` operator fail if there's a `GROUP BY` on the nonpivot column output by `PIVOT`. As a workaround, remove the nonpivot column from the `GROUP BY`. Query results are the same, as this `GROUP BY` clause is a duplicate.
104104

105+
Column names are of type `sysname` or `NVARCHAR(128)`. Because `UNPIVOT` projects column names as values, the data type for an `UNPIVOT` column will also be `NVARCHAR(128)`, which is not a supported data type in [!INCLUDE [fabric](../../includes/fabric.md)]. If you want to save the results of `UNPIVOT` to a table in [!INCLUDE [fabric](../../includes/fabric.md)], you may first need to cast to a supported datatype. See [Data types in Fabric Data Warehouse](/fabric/data-warehouse/data-types).
106+
```sql
107+
CREATE TABLE myTable AS
108+
SELECT value,
109+
CAST(columnNames as VARCHAR(128) ) columnNames
110+
FROM myTableToUnpivot
111+
UNPIVOT( value
112+
FOR columnNames IN( col1, col2 )) unpvt;
113+
```
114+
105115
## Basic PIVOT example
106116

107117
The following code example produces a two-column table that has four rows.

0 commit comments

Comments
 (0)