title: SQL Partition By to calculate Net Value
body: <p>Imagine an order system, with multiple orders having sides as Buy and Sell.
  The data set look like this </p>
  
  <p><a href="https://i.stack.imgur.com/KdK4V.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/KdK4V.jpg" alt="Sample Structure"></a></p>
  
  <p>I want to calculate the Net Value which is (Sum of All Buy Sides - Sum of All Sell Sides)</p>
  
  <p>Expected output:</p>
  
  <p><a href="https://i.stack.imgur.com/KlSkG.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/KlSkG.png" alt="Expected"></a></p>
  
  <p>I have currently used Partition By to arrive at following output</p>
  
  <pre><code>SELECT 
  [NAME]
  ,[Side]
  ,[Value]
  ,SUM([Value]) OVER (PARTITION BY [NAME],[Side]) as Net
  FROM 
  [Order]
  </code></pre>
  
  <p>Output:</p>
  
  <p><a href="https://i.stack.imgur.com/xOrOV.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xOrOV.png" alt="enter image description here"></a></p>
  
  <p>But I am not getting how to subtract the values and arrive at the expected result.</p>