Wednesday, May 2, 2012

Use TOP and DISTINCT together in t-sql

Surprisingly it is difficult to use TOP and DISTINCT together in SQL. This will not work: select top 2 distinct t.ID as tID, t.Name as tName from Table1 order by tID It can be replaced by something like this to work: select top 2 * from (select distinct t.ID as tID, t.Name as tName from Table1 t) y order by tID Also see some other web links, such as Here

1 comment:

Vasileios said...

select distinct top x fields from table ;

Blog Archive

Followers