title: How to calculate total balance in sql using sum?
body: <p>updated question --</p>
  <p>I have a table that contains the following columns:</p>
  <pre><code>DROP TABLE TABLE_1;
  CREATE TABLE TABLE_1(
  TRANSACTION_ID number, USER_KEY number,AMOUNT number,CREATED_DATE DATE, UPDATE_DATE DATE
  );
  
  insert into TABLE_1
  values ('001','1001',75,'2022-12-02','2022-12-03'),
  ('001','1001',-74.98,'2022-12-02','2022-12-03'),
  ('001','1001',74.98,'2022-12-03','2022-12-04'),
  ('001','1001',-75,'2022-12-03','2022-12-04')
  </code></pre>
  <p>I need to calculate the balance based on the update date. In some cases there can be the same update_date for two different records. When I have this, I want to grab the lower value of the balance.</p>
  <p>This is the query I have so far:</p>
  <pre><code>select * from (
  select TRANSACTION_ID,USER_KEY,AMOUNT,CREATED_DATE,UPDATE_DATE,
  sum(AMOUNT) over(partition by USER_KEY order by UPDATE_DATE rows BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) as TOTAL_BALANCE_AMOUNT
  from TABLE_1
  ) qualify row_number() over (partition by USER_KEY order by UPDATE_DATE DESC, UPDATE_DATE DESC) = 1
  </code></pre>
  <p>In the query above, it's is grabbing the 75, rather than the 0 after I try to only grab the LAST balance.</p>
  <p>Is there a way to include in the qualify query to grab the last balance but if the dates are the same, to grab the lowest balance?</p>