title: How to select first row for each set of rows with common value
body: <p>As an example I have a a table like this:</p>
  
  <pre><code>week_dt     cust    y_w     w   y       y_fp
  2011-01-29  ABC     201122  6   2011    201106
  2011-02-05  ABC     201123  6   2011    201106
  2011-02-12  ABC     201124  6   2011    201106
  2011-02-19  ABC     201125  7   2011    201107
  2011-02-26  ABC     201126  7   2011    201107
  2011-03-05  ABC     201127  7   2011    201107
  2011-03-12  ABC     201128  7   2011    201107
  2011-03-19  ABC     201129  8   2011    201108
  2011-03-26  ABC     201130  8   2011    201108
  2011-04-02  ABC     201131  8   2011    201108
  2011-04-09  ABC     201132  8   2011    201108
  2011-04-16  ABC     201133  9   2011    201109
  2011-04-23  ABC     201134  9   2011    201109
  </code></pre>
  
  <p>The last column contains year/fiscal period info. I would like to select one row for each <code>y_fp</code> element with <code>week_dt</code> and <code>y_w</code> being the first date for that year/fiscal period. Thus selecting from the above would yield:</p>
  
  <pre><code>week_dt     cust    y_w     w   y       y_fp
  2011-01-29  ABC     201122  6   2011    201106
  2011-02-19  ABC     201125  7   2011    201107
  2011-03-19  ABC     201129  8   2011    201108
  2011-04-16  ABC     201133  9   2011    201109
  </code></pre>
  
  <p>I've tried following <a href="https://stackoverflow.com/questions/17991943/how-to-select-the-first-n-rows-of-each-group">this example</a> using <code>TOP(1)</code> instead of <code>LIMIT</code> but I got the entire table returned to me. </p>
  
  <p>This was my query:</p>
  
  <pre><code>SELECT * FROM ( 
      SELECT
      md.week_dt, 
      md.cust, 
      md.y_w, 
      md.y, 
      md.w, 
      md.y_fp, 
      FROM MASTER_DATES md
  ) t1
  Where 
      t1.week_dt IN (
      SELECT TOP(1) t2.week_dt
      FROM MASTER_DATES t2
      WHERE t2.week_dt = t1.week_dt AND t2.cust = t1.cust
      ORDER BY t2.week_dt
  )
  ORDER BY t1.week_dt
  </code></pre>
  
  <p>Note that MASTER_DATES contains a composite primary key from columns <code>week_dt</code> and <code>cust</code>.</p>