| how to use the ORDER BY with DISTINCT in Sql Query.
Just look at the following query.
select distinct AcademicYear from IVLESToolUsage
order by cast(SUBSTRING(LTRIM(RTRIM(AcademicYear)),9,4) as int) DESC
AcademicYear-char(15)
When you are execute you will get following error message.
Error:[color=#FF0000]ORDER BY items must appear in the select list if SELECT DISTINCT is specified.[/color]
Solution:
[color=#0000FF]
select AliasTable.AcadmicYearAL
from ( select DISTINCT LTRIM(RTRIM(AcademicYear)) as AcadmicYearAL from IVLESToolUsage) as AliasTable
ORDER BY cast(SUBSTRING(LTRIM(RTRIM(AliasTable.AcadmicYearAL)),9,4) as int) DESC [/color]
Thank you | |