title: Postgresql SUM function subtracting two columns is not working
body: <p>I have a database with a column of id and another column of json data which contains information of name, timestamp, action, and place. </p>
  
  <p>But the SUM function below doesn't work. </p>
  
  <p>It just outputs a column with rows of endtime-starttime.
  I was expecting the sum(endtime-starttime)
  Please help.
  Below is the code:</p>
  
  <pre><code>SELECT sum(extract (epoch from(endtime::timestamp - starttime::timestamp))/60)
  FROM 
  (select data-&gt;body-&gt;&gt;timestamp as endtime, 
  row_number() over (order by id) as row
  from table
  where data-&gt;body-&gt;&gt;action='off')as endtable
  inner join
  (select data-&gt;body-&gt;&gt;timestamp as starttime, 
  row_number() over (order by id) as row
  from table
  where data-&gt;body-&gt;&gt;action='on')as starttable
  on endtable.row = starttable.row
  group by endtable.row, starttable.row;
  </code></pre>