Calculate the percentage change between two rows into a view in Postgres
<p>I have a table with yearly values for 200+ countries. For a graphical representation, I'd like to get the percentage change between two specific years, 1990 and 2013.</p>

<p>The table looks a bit like this:</p>

<pre><code>  id_country       year       value
    886            2002      161.348
    886            2003      161.348
    886            2004      176.016
    886            2005      176.016
    886            2006      179.683
    886            2007      183.35
    886            2008      201.685
    886            2009      227.354
    886            2010      234.688
    886            2011      245.689
    886            2012      293.36
    886            2013      440.04
    620            1990      40.337
    620            1991      1056.096
    620            1992      1151.438
    620            1993      1389.793
    620            1994      1584.144
    620            1995      1631.815
    620            1996      1749.159
    620            1997      1796.83
    620            1998      1906.84
    620            1999      1664.818
    620            2000      1642.816
    620            2001      2016.85
    620            2002      1760.16
    620            2003      1873.837
    620            2004      1961.845
    620            2005      2310.21
    620            2006      2328.545
    620            2007      2361.548
    620            2008      3329.636
    620            2009      3069.279
    620            2010      3098.615
    620            2011      2823.59
    620            2012      3373.64
    620            2013      2948.268
</code></pre>

<p>I thought the best way would be to produce a VIEW with the <code>id_country</code>, which calculates that difference. But I have no clue how that query would look like. It must <code>SELECT</code> all countries, and then divide <code>year = 2013</code> by <code>year = 1990</code> for each of these countries.</p>

<p>It could get more complicated as there are multiple variables in that table (represented by additional columns), which would be needed to be filtered by those additional column values, like <code>id_source = 1</code> or <code>id_source = 2</code>, or <code>id_sector = 1</code> or <code>id_sector = 2</code>.</p>

<p>Any help is very much appreciated!</p>