0

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?

Shadow
  • 33,525
  • 10
  • 51
  • 64
GameOfThrows
  • 4,510
  • 2
  • 27
  • 44
  • I guess you don't know all possible dates in advance. Anyway it could be simple PIVOT/Conditional aggregation if col_names are fixed. – Lukasz Szozda Apr 15 '16 at 14:48
  • I do not have the dates in advance, although I might be able to create it, if the lag/window method does not work I might try the aggregation. – GameOfThrows Apr 15 '16 at 14:54

0 Answers0