24 October 2018        Add to Favorites   Report

Convert Rows to Columns Using PIVOT in Snowflake

Often, there are requirements, where we need to summarize a table and show the results in columns. i.e, Converting Rows to columns, Snowflake supports this operation using a PIVOT function

Here's my Sales table:

Emp_idMonthAmount
1Jan2000
2Jan8000
1Feb6000
2Feb3000
1Mar5000
2Mar7000
1Apr3000
2Apr4000
1Jan3000

PIVOT Example:

select * from sales
pivot(sum(amount) for month in ('Jan', 'Feb', 'Mar', 'Apr')) as p;
+-------+-------+-------+-------+-------+
| EMP_ID| 'Jan' | 'Feb' | 'Mar' | 'Apr' |
|-------+-------+-------+-------+-------|
| 1 | 5000 | 6000 | 5000 | 3000 |
| 2 | 8000 | 3000 | 7000 | 4000 |
+-------+-------+-------+-------+-------+


Read great educational content like this and a lot more ! Create my free account now 🎁

Copied