title: Migrating SQL Server Outer Apply Query to Snowflake query
body: <p>Working on migrating to Snowflake from SQL Server. Stuck at below query. Couldn't find anything equivalent in Snowflake.
  For every row in outer query we go in inner query to get top datekey which is smaller than datekey in outer query.</p>
  
  <pre><code>
      select a.datekey , a.userid, a.transactionid
      ,cast( cast(b.datekey as varchar(8)) as date) priorone
      from tableA a
      outer apply (select top 1 b.datekey
                    from tableA b where a.userid = b.userid
                    and b.transactionid &lt; a.transactionid and b.datekey &lt; a.datekey
      order by b.transactionid desc) as b
  
  </code></pre>
  
  <p>Tried suggested answer as below:</p>
  
  <pre><code>create or replace table tableA
     (datekey date , userid int ,transactionid int)
     insert into tableA
     values('2020-06-01',1,101),('2020-06-02',1,102),('2020-06-02',1,103),('2020-06-01',2,104),('2020-06-02',2,105)
      select 
      a.datekey, 
      a.userid, 
      a.transactionid
      ,(
          select b.datekey
          from tableA b 
          where 
              a.userid = b.userid
              and b.transactionid &lt; a.transactionid 
              and b.datekey &lt; a.datekey
          order by b.transactionid desc
          limit 1
      ) priorone
  from tableA a
  </code></pre>