Note that there are some explanatory texts on larger screens.

plurals
  1. POgetting nearest past date records on sql server
    primarykey
    data
    text
    <p>I have following table:</p> <pre><code>id dr cr balance txn_date 1 0.00 0.00 31895532.96 12/20/2013 2 15000.00 0.00 31880532.96 12/20/2013 3 102120.00 0.00 31778412.96 12/20/2013 4 15250.00 0.00 31763162.96 12/20/2013 5 75000.00 0.00 25761370.96 12/22/2013 6 98600.00 0.00 25662770.96 12/22/2013 7 71460.00 0.00 25591310.96 12/22/2013 8 518400.00 0.00 25072910.96 12/22/2013 9 35000.00 0.00 25037910.96 12/22/2013 </code></pre> <p>I need to find the opening balance when the date is inputted . For example when <code>12/20/2013</code> is inputted then opening balance is <code>31895532.96</code>. </p> <p>It is not necessary to have transaction on daily basis. Here too we have no transaction on <code>21st December 2013</code>, in that case we need to take balance of last transaction of <code>12/20/2013</code> i.e <code>31763162.96</code>. Any ideas how to implement it? </p> <p>I did the following for <code>12/20/2013</code>:</p> <pre><code>select top 1 balance from banks where txn_date = '12/20/2013' order by id </code></pre> <p>now when I come to <code>12/21/2013</code> there is no transaction on that date. So the last transaction of previous date(not necessarily yesterday it could be any date depending on transaction date) should be balance i.e <code>31763162.96</code>:</p> <pre><code>select top 1 balance from banks where txn_date &lt;= '12/21/2013' order by id desc </code></pre> <p>It returned last transaction of previous date of <code>12.21.2013</code></p> <p>How to merge two above queries to satisfy both conditions?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload