postgres lag and window to create cohort table

时间:2016-04-15 14:47:03

标签: postgresql pivot-table

I am a beginner in Postgres, I am trying to create a cohort table from the following table:

column1     | column2     | count
------------------------------------
01-apr-2016 | 01-apr-2016 | 16
01-apr-2016 | 02-apr-2016 | 15
01-apr-2016 | 03-apr-2016 | 19
02-apr-2016 | 02-apr-2016 | 18
02-apr-2016 | 03-apr-2016 | 16
03-apr-2016 | 03-arp-2016 | 14

I want to use the lag and window functions to create a table that looks like:

            | 01-apr-2016 | 02-apr-2016 | 03-apr-2016
---------------------------------------------------------
01-apr-2016 |      16     |      15     |    19
02-apr-2016 |             |      18     |    16
03-apr-2016 |             |             |    14

I followed this tutorial http://www.craigkerstiens.com/2014/02/26/Tracking-MoM-growth-in-SQL/ but it seems to be outdated, and I keep getting syntax error at the GROUP BY clause. I fiddled around a bit, but I can only ever get 1 lag column. Could someone shed some light on me please?

0 个答案:

没有答案