title: Obtaining a field associated with the previous month in Sybase
body: <p>In the query below, I am trying to get the <code>BALANCE</code> value from table <code>loans</code> for the same <code>Uniq_ID</code> and same year, but previous month, and then compare it to the current rows <code>BALANCE</code> value. The <code>fdate</code> column looks like <code>3/1/2019</code>. I try to do it as below but get the error <code>&quot;Sybase Database Error: Feature, reference containing a scalar value subquery (defined at line 13) inside a conditional expression (CASE, COALESCE, ARGN, NULLIF, or IF), is not supported&quot;</code>. How can I try to do this another way?</p>
  <pre><code>SELECT h.UNIQUEID, f.num
  (select BALANCE from loans b
      where b.UNIQ_ID = h.UNIQ_ID
      and year(b.FDATE) = year(h.FDATE) 
      and MONTH(b.fdate) = MONTH(h.fdate) - 1) AS prev_bal,
  (case when prev_bal &gt; 0 and prev_bal &gt;= BALANCE then 1 else 0 end) as  flag
  FROM loans h, perform f
  where f.uniq_id = h.uniq_id 
  </code></pre>
  <p><strong>Edit</strong>:</p>
  <p>The data looks like this:</p>
  <pre><code>fdate           UNIQUEID              NUM    BALANCE   
  3/1/2019       LNSAR17224-00453434    1      16254.1  
  4/1/2019       LNSAR17224-00453434    1      15643.2    
  </code></pre>
  <p>I would like to add the column <code>prev_bal</code> which looks as follows:</p>
  <pre><code>fdate           UNIQUEID              NUM    BALANCE   prev_bal    
  3/1/2019       LNSAR17224-00453434    1      16254.1    {null}     
  4/1/2019       LNSAR17224-00453434    1      15643.2    16254.1 
  </code></pre>
  <p>Expected Output:</p>
  <pre><code>UNIQUEID              NUM   prev_bal    flag
  LNSAR17224-00453434    1    {null}       0
  LNSAR17224-00453434    1    16254.1      1
  </code></pre>