Skip to content


The postgreSQL crosstab function is really powerful

I’m just beginning to wrap my brain around it. I’ve got some data that looks like this:


select rr as row_name, dt, c from mm limit 3;
+----------+----------+------+
| row_name |    dt    |  c   |
+----------+----------+------+
| xxx      | 2009 mar |  552 |
| xxx      | 2009 nov | 2179 |
| xxx      | 2009 jun | 1101 |
+----------+----------+------+
(3 rows)

And now I’m going to transpose it like this:

select * from crosstab('select rr as row_name, dt, c from mm limit 3')
as foo(row_name text, dt_1 bigint, dt_2 bigint);
+----------+------+------+
| row_name | dt_1 | dt_2 |
+----------+------+------+
| xxx      |  552 | 2179 |
+----------+------+------+
(1 row)

Neat, right?

Posted in PostgreSQL, Programming, SQL.

 
close Reblog this comment
blog comments powered by Disqus