title: Multiple value IF statement, to show in select
body: <p>Any help appreciated,</p>
  
  <p>the code below is from a database which someone made, every time a receipt is made a unique receipt id is issued. same when a reversal is made a new receipt is issued. what links the two are the pay in number. If a reverse is issued, the reverse flag changes on the old receipt to Y and the new one says N. i have my query that select Minimum date and Max data, for receipts returned will have a much later date that when it was first created. The issue is when there is no reverse, it still pulls information as the min and max date are the same. I am fully aware that i need an if statement, but have no idea how to do it since i am new to Databases.    </p>
  
  <pre><code>Select distinct r.receipt_date, r.receipt_no, r.doc_no as Payin_No,r.trans_amt,l.location_desc, ct.charge_type_desc, 
          (select un.first_name + ' ' + un.last_name)as cashier,
          r.payee, r.comments, r.reverse_flag, ret1.returned_by, ret1.return_date
  from Cashier..receipts as r
  inner join Cashier..location as l on r.location_id=l.location_id
  inner join [Cashier].[dbo].[charge_type] as ct on ct.charge_type_no=r.charge_type_no
  inner join Cashier..user_name as un on un.user_name=(UPPER(r.created_by))
  inner join (
      select receipt_no as Return_Receipt , 
             (select un2.first_name + ' ' + un2.last_name) as returned_by, 
               created_date as return_date, doc_no as Payin_no, 
               r1.reverse_flag
              from Cashier..receipts as r1 
              inner join Cashier..user_name as un2 on un2.user_name=(UPPER(r1.created_by))
              where doc_no =  r1.doc_no
              and created_date = (select MAX(created_date) 
                                  from Cashier..receipts where doc_no = r1.doc_no)) as ret1 
              on (ret1.Payin_no=r.doc_no)
              where r.receipt_date = (select MIN(r1.receipt_date) from Cashier..receipts as r1 where r1.receipt_no = r.receipt_no )
  </code></pre>
  
  <p><a href="http://i.stack.imgur.com/Up6m2.jpg" rel="nofollow">Issue i am having, the return by is the same as created</a></p>
  
  <p><a href="http://i.stack.imgur.com/NLL1X.jpg" rel="nofollow">Desired result</a></p>