Q:

SQL IF or CASE in IN statement

Users are entering data into my "track" table from an .aspx page.  They are entering a series of dates for different activities and checking a box if they want to be reminded of that date.  The specific issue I have at the moment is limiting the columns in my IN statement:
SELECT [id], [name], MIN([DATE]) AS [MinDate] 
FROM track UNPIVOT ([DATE] FOR d IN ([date1],[date2],[date2])) unpvt
GROUP BY [id], [name]

I only want to include date1 if column date1chk's value is 1.  Likewise for date2 if date2chk is "1", etc.  Otherwise that date# column is not included in the IN.
After this my challenge is to be able to update the table such that, for example, date2chk is set from 1 to 0 for a specific record if, for example, date2 was the minimum.
Your thoughts are greatly appreciated -- this is proving to be one of the harder issues I've dealt with.
EDIT (thank you for the feedback):
Data
ID       Date1       Date1Chk         Date2      Date2Chk       Date3        Date3Chk
0     2013-01-15        0          2013-02-01       1        2013-02-10         1
1     2013-02-05        1          2013-02-05       1        2013-02-10         0

Desired Result
ID    MinDate
0    2013-02-01
1    2013-02-05

Assuming I can get this result, I am not certain how I would update the table.  For example, in the "desired result" ID 1 is returning a minimum value that exists for both [Date1] and [Date2]; I would want the user to halt reminders on the first instance, so [Date1Chk] would be set to 0 (not [Date2Chk]).

A:

Move the unpivot into a subquery, so you can grab the name of the column.  Then a where clause solves your problem:
select id, name, min([date]) as mindate
from (SELECT [id], [name], d, [DATE]
      FROM track
      UNPIVOT ([DATE] FOR d IN ([date1], [date2], [date2])) unpvt
    ) t
where (d = 'date1' and datecheck1 = 1) or
      (d = 'date2' and datecheck2 = 1) or
      (d = 'date3' and datecheck3 = 1)
GROUP BY [id], [name]

To get the specific date, use row_number() instead of group by:
select id, name, [date] as mindate
from (select id, name, [date],
             row_number() over (partition by id, name order by [date]) as seqnum
      from (SELECT [id], [name], d, [DATE]
            FROM track
            UNPIVOT ([DATE] FOR d IN ([date1], [date2], [date2])) unpvt
          ) t
      where (d = 'date1' and datecheck1 = 1) or
            (d = 'date2' and datecheck2 = 1) or
            (d = 'date3' and datecheck3 = 1)
     ) t
where seqnum = 1

