title: How to calculate the difference between column values of different records in grafana/postgres
body: <p>I have Grafana and postgres installed and connected. I use grafana to display charts of data I add to postgres. My postgres database has a table with records from multiple sources. The schema looks like this:</p>
  
  <pre><code>time   | source | bid | ask
  12:01  | bitmex | 10  | 11
  12:01  | deribit| 10  | 11
  12:02  | bitmex | 9   | 11
  </code></pre>
  
  <p>The exact times per source are different. I’m able to plot different lines for every source:</p>
  
  <p><a href="https://i.stack.imgur.com/rLc08.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rLc08.png" alt="enter image description here"></a></p>
  
  <p>I’m looking to plot the difference (grouped per minute) of two different sources. I think I need to select grouped minutes where “source = x” and the same where “source = y” and subtract those, while keeping the time macros from Grafana in there to keep the whole thing speedy. Can anyone point me in the right direction? I don't think this is at all possible with the "graphical query builder" in grafana.</p>