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 |
+-------+-------+-------+-------+-------+

Ryan-Dallas

posted on 24 Oct 18

Enjoy great content like this and a lot more !

Signup for a free account to write a post / comment / upvote posts. Its simple and takes less than 5 seconds