Search code examples
sqlsql-server-2000

Distinct of one column from a two column table in SQL Server 2000


I'm trying to get a unique list of a table with two columns ...

Table snapshot:

ITEM   DISP
-----------------
903996 , 1600
903996 , 3D13
123045 , 1D23
450300 , 3D14
450300 , 3DAD

The output I need is the following

ITEM     DISP
-----------------
903996 , 1600
123045 , 1D23
450300 , 3D14

I know that in SQL Server 2005 the ROW_NUMBER is very helpful for this type of problem by I'm working in SQL Server 2000 and this function does not exist.

This is my idea for SQL Server 2005:

    Select  [ITEM],[DISP]
FROM (
SELECT 
       [ITEM]
      ,[DISP]
      ,ROW_NUMBER() OVER (PARTITION BY [ITEM] ORDER BY [ITEM]) as RowNumber
  FROM [TABLE]
) WHERE RowNumber =1 

How can I do this in SQL Server 2000?

Thanks


Solution

  • You should be able to use MIN() with GROUP BY:

    select item, min(disp)
    from snapshot
    group by item
    

    See SQL fiddle with Demo

    Result:

    |   ITEM | DISP |
    -----------------
    | 123045 | 1D23 |
    | 450300 | 3D14 |
    | 903996 | 1600 |