Not exists/Where NOT in
If you use the ROW_NUMBER function partitioning over "Code" and ordering by "Date" descending like the following query

SELECT ROW_NUMBER() OVER (PARTITION BY CODE ORDER BY DATE DESC) AS rno,
     ID,
     CODE,
     CONVERT (VARCHAR, DATE, 101) AS fdate
FROM   A
WHERE  CODE &lt;&gt; 'FOL'


you will get.

    RNO ID           CODE       FDATE
    1   customer001  INQ        09/30/2013
    2   customer001  INQ        01/01/2013
    1   customer001  REF        10/15/2013


Then if you use a left outer join using that same select statement on RNO where the code of the right table is 'REF' like below

SELECT a1.ID,
   a1.CODE AS Code1,
   a2.CODE AS Code2,
   a1.fdate AS fdate1,
   a2.fdate AS fdate2
FROM
(
  SELECT ROW_NUMBER() OVER (PARTITION BY CODE ORDER BY DATE DESC) AS rno,
     ID,
     CODE,
     CONVERT (VARCHAR, DATE, 101) AS fdate
  FROM A
  WHERE CODE &lt;&gt; 'FOL'
) AS a1
LEFT OUTER JOIN
(
  SELECT ROW_NUMBER() OVER (PARTITION BY CODE ORDER BY DATE DESC) AS rno,
     ID,
     CODE,
     CONVERT (VARCHAR, DATE, 101) AS fdate
  FROM   A
  WHERE  CODE &lt;&gt; 'FOL'
  ) AS a2
  ON a1.rno = a2.rno
  AND a2.CODE = 'REF'
  WHERE  a1.CODE &lt;&gt; 'REF'
  ORDER BY FDATE1


and finally use a where clause filter on the left table where code is not equal to 'REF' you will get the following.

    ID          CODE1   CODE2   FDATE1      FDATE2
    customer001 INQ     (null)  01/01/2013  (null)
    customer001 INQ     REF     09/30/2013  10/15/2013


Hope this helps or is what you are looking for.