Previous Topic

Next Topic

Inhoudsopgave

Book Index

Distinct Combined With Order By

When you use a ‘Select Distinct’ in MS SQL Server, all columns in the ‘Order By’ clause also need to be present in the ‘Select’ clause. In ASE this is not obligatory.

Example

create table Example (Id int, First_Name varchar(30))

insert into Example values (1, 'John')

insert into Example values (2, 'Olivia')

insert into Example values (3, 'John')

insert into Example values (4, 'Jack')

insert into Example values (5, 'Olivia')

insert into Example values (1, 'John')

select distinct First_Name from Example

select distinct Id, First_Name from Example order by Id

go

-- This statement will not work on MS SQL Server

select distinct First_Name from Example order by Id

go

drop table Example

Note: Although ASE seems to offer you more flexibility, the actual result of the query ‘select distinct First_Name from Example order by Id’ is somewhat unexpected. Instead of giving you the distinct First_Name fields, it returns a result that can be described as the result of ‘select distinct Id, First_Name from Example order by Id’ with a hidden Id column in the result set.