title: T-SQL How to select min, max and first row from table from duplicate values
body: <blockquote>
  <pre><code>     DateTime        --  Unit  --  Client  --- Qty
  03/02/2013 08:00:01  --   3     --   1      ---   1
  03/02/2013 08:00:02  --   3     --   2      ---   1
  03/02/2013 08:00:03  --   3     --   3      ---   2
  03/02/2013 08:00:04  --   3     --   3      ---   2
  03/02/2013 08:00:05  --   3     --   3      ---   5
  03/02/2013 08:00:06  --   3     --   3      ---   4
  03/02/2013 08:00:07  --   3     --   4      ---   6
  03/02/2013 08:00:08  --   3     --   4      ---   67
  03/02/2013 08:00:09  --   3     --   4      ---   76
  03/02/2013 08:00:10  --   3     --   4      ---   76
  </code></pre>
  </blockquote>
  
  <p>And I want :</p>
  
  <blockquote>
  <pre><code>     DateTime        --  Unit  --  Client  --- Qty
  03/02/2013 08:00:01  --   3     --   1      ---   1
  03/02/2013 08:00:02  --   3     --   2      ---   1
  03/02/2013 08:00:03  --   3     --   3      ---   2
  03/02/2013 08:00:05  --   3     --   3      ---   5
  03/02/2013 08:00:07  --   3     --   4      ---   6
  03/02/2013 08:00:09  --   3     --   4      ---   76
  </code></pre>
  </blockquote>
  
  <p>The criteria to filter is get the min and max "Qty" from table and get only the first value when exists duplicate "Qty" values in the same "Unit" and "client" column.</p>
  
  <p>I do the follow T-SQL, but the retrieval is the last "Qty" value when the "Unit" and "client" column are the same, I need the first.</p>
  
  <pre><code>    --1
  CREATE TABLE Transact
      (DateTime   DateTime,
       Unit       INT NULL,
       Client     INT NULL,
       Qty        INT NULL
      )
  
  INSERT INTO Transact (Datetime,Unit,Client,Qty)
      Values ( '03/02/2013 08:00:01',3,1,1)
      Values ( '03/02/2013 08:00:02',3,2,1)
      Values ( '03/02/2013 08:00:03',3,3,2)
      Values ( '03/02/2013 08:00:04',3,3,2)
      Values ( '03/02/2013 08:00:05',3,3,5)
      Values ( '03/02/2013 08:00:06',3,3,4)
      Values ( '03/02/2013 08:00:07',3,4,6)
      Values ( '03/02/2013 08:00:08',3,4,67)
      Values ( '03/02/2013 08:00:09',3,4,76)
      Values ( '03/02/2013 08:00:10',3,4,76)
  
  
  DECLARE @Total TABLE
      (DateTime   DateTime,
       Unit       INT NULL,
       Client     INT NULL,
       Qty        INT NULL
      )
  DECLARE @Uniques TABLE
      (DateTime   DateTime,
       Unit       INT NULL,
       Client     INT NULL,
       Qty        INT NULL
      )
  DECLARE @Mini TABLE
      (DateTime   DateTime,
       Unit       INT NULL,
       Client     INT NULL,
       Qty        INT NULL
      )
  DECLARE @Maxi TABLE
      (DateTime   DateTime,
       Unit       INT NULL,
       Client     INT NULL,
       Qty        INT NULL
      )
  --2
  
  INSERT INTO @Total SELECT * FROM Transact
  
  INSERT INTO @Mini SELECT  MIN(Datetime) Datetime,Unit,Client,MIN(Qty) FROM @Total GROUP BY Unit,Client 
  INSERT INTO @Maxi SELECT  MAX(Datetime) Datetime,Unit,Client,MAX(Qty) FROM @Total GROUP BY Unit,Client
  
  --3
  
  INSERT INTO @Uniques SELECT * FROM @Mini UNION SELECT * FROM @Maxi
  
  SELECT * FROM @Uniques
  </code></pre>
  
  <p>Thanks in advance.</p>
  
  <p>Pablo Geronimo.</p>