title: SQL Query Count and Label if first
body: <p>I'm using SQL Server management Studio 2010.</p>
  
  <p>I need my query to populate a column if the record contains the first occurrence of a value.</p>
  
  <p>The table my query returns is huge so I'll just use pretend columns to get my point across. My query currently returns a table that looks like this</p>
  
  <pre><code>| ROW | ItemNumber | DateOpen | Status |
  |  1  |      10045 | 5/5/2005 |   Open |
  |  2  |      10045 | 5/5/2005 |   Open |
  |  3  |      10046 | 5/5/2005 |   Open |
  |  4  |      10046 | 5/5/2005 |   Open |
  |  5  |      10046 | 5/5/2005 |   Open |
  </code></pre>
  
  <p>I've already added the row indicator in to the query thinking it would help identify the first occurrence of an ItemNumber. I need to have a new column that marks an X if the record is the first occurrence.</p>
  
  <p>I have this so far</p>
  
  <pre><code>    DECLARE @ItemData Table(itemRow BIGINT, itemNumber BIGINT, DateOpen VARCHAR(15), status VARCHAR(15))
  INSERT INTO @ItemData (itemRow, ItemNumber, DateOpen, Status)
  SELECT Row_Number() OVER(ORDER BY Cm_ItemNumber) AS 'ROW'
         ,Cm_ItemNumber                            AS ItemNumber
         ,Dates_DateOpen                           AS DateOpen
         ,St_Status                                AS Status
  FROM db_Items
  JOIN db_Dates ON Dates_Item = Cm_ItemID
  JOIN db_Status ON St_ID = Cm_StatusID
  Select * from @ItemData
  </code></pre>
  
  <p>The reason its thrown into a table then selected seperately is because there's actually a union and a lot more stuff in the query and when I realized I needed to do a unique checker and add it to a column, I figured the easiest way would be an "after the fact" type thing and it would go into the     <code>Select * from @ItemData</code> portion.</p>