title: Making SQL choose last non-zero amount
body: <p>I currently have a table that is in this format currently</p>
  
  <pre><code>id | year | jan | feb | mar | apr | may | jun | jul | aug | sep | oct | nov | dec
  A    2001  0.00   1.00  0.00  0.00  1.00  1.00  0.00  0.00  0.00  0.00  0.00  0.00
  A    2003  0.00   1.00  0.00  0.00  1.00  1.00  0.00  0.00  0.00  0.00  0.00  0.00
  A    2005  0.00   0.00  0.00  0.00  0.00  0.00  0.00  0.00  3.00  0.00  0.00  0.00
  </code></pre>
  
  <p>The result that I'm trying to get is such</p>
  
  <pre><code>id | firstyear | lastyear | lastpaidamount
  A      2001       2005           3.00
  </code></pre>
  
  <p>I tried using this code however all i got was NULL for lastpaidamount</p>
  
  <pre><code>    SELECT 
    a.id,
    MIN(YEAR) AS firstyear,
    MAX(YEAR) AS lastyear,
    b.lastpaidadmount 
  FROM
    table1 a 
    LEFT JOIN 
      (SELECT 
        id,
        CASE
          WHEN dec &gt; 0 
          THEN dec 
          WHEN nov &gt; 0 
          THEN nov 
          WHEN oct &gt; 0 
          THEN oct 
          WHEN sep &gt; 0 
          THEN sep 
          WHEN aug &gt; 0 
          THEN aug 
          WHEN jul &gt; 0 
          THEN jul 
          WHEN jun &gt; 0 
          THEN jun 
          WHEN may &gt; 0 
          THEN may 
          WHEN apr &gt; 0 
          THEN apr 
          WHEN mar &gt; 0 
          THEN mar 
          WHEN feb &gt; 0 
          THEN feb 
          WHEN jan &gt; 0 
          THEN jan 
        END AS lastpaidamount 
      FROM
        table1 
      WHERE YEAR = 'a.lastyear' 
      GROUP BY id) b 
      ON a.id = b.id 
  WHERE jan &gt; 0 
    OR feb &gt; 0 
    OR mar &gt; 0 
    OR apr &gt; 0 
    OR may &gt; 0 
    OR jun &gt; 0 
    OR jul &gt; 0 
    OR aug &gt; 0 
    OR aug &gt; 0 
    OR sep &gt; 0 
    OR oct &gt; 0 
    OR nov &gt; 0 
    OR dec &gt; 0 
  GROUP BY id ;
  </code></pre>
  
  <p>Appreciate any help here, thanks!</p>