Skip to content
Advertisement

Could you tell me why my ORDER BY DESC function is not working properly?

SELECT 
    YEAR, 
    Strata, 
    Strata_Name, 
    FORMAT([%]/10000, 'P') [%] 
FROM 
    [dbo].[Depression_in_California] 
WHERE 
    Strata_Name = 'Male' 
ORDER BY [%] desc;

the column is float not null

enter image description here

Advertisement

Answer

You have a numeric column called [%] and you are naming your calculated column [%] too. The type of your calculated value if nvarchar.

As pointed out by @Tarik, it looks like SQL Server is ordering the result by the calculated column (alias). I think it would be much more clear to use a different alias name, for example:

SELECT 
    YEAR, 
    Strata, 
    Strata_Name, 
    FORMAT([%]/10000, 'P') PercentageOfDepression /* <-- calculated nvarchar */
FROM 
    [dbo].[Depression_in_California] 
WHERE 
    Strata_Name = 'Male' 
ORDER BY [%] desc; /* <-- numeric [%] */
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement