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_id | Month | Amount |
1 | Jan | 2000 |
2 | Jan | 8000 |
1 | Feb | 6000 |
2 | Feb | 3000 |
1 | Mar | 5000 |
2 | Mar | 7000 |
1 | Apr | 3000 |
2 | Apr | 4000 |
1 | Jan | 3000 |
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 18Enjoy 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
Post Comment