Order By Column

  • Comments posted to this topic are about the item Order By Column

  • Option 3

    Hope this helps...

    Ford Fairlane
    Rock and Roll Detective

  • Nice question, but the explanation doesn't mention why the order of casting vs order by changes. (because of adding the T alias in the second statement)

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • This was removed by the editor as SPAM

  • Koen Verbeeck (3/4/2014)


    Nice question, but the explanation doesn't mention why the order of casting vs order by changes. (because of adding the T alias in the second statement)

    + 1

    Thanks & Best Regards,
    Hany Helmy
    SQL Server Database Consultant

  • Interesting QotD.

    Thanks & Best Regards,
    Hany Helmy
    SQL Server Database Consultant

  • Good one! thanks.

    ---------------------------------------------------
    "Thare are only 10 types of people in the world:
    Those who understand binary, and those who don't."

  • Nice qotd.

  • Interesting, I never realised that you could order by a column alias.

    I have to admit that I assumed that the same principle of not being able to use column aliases in WHERE, HAVING, GROUP BY, etc. clauses would apply.

    Nice to know that's not the case though.

  • nice question .... but Who Cares 😛

  • twin.devil (3/4/2014)


    nice question .... but Who Cares 😛

    Still remember the pale face of a report developer who wants his data sorder in descending date order, but it was showing in descending string order. 😉

    Nice to see that the third option is a valid answer:hehe:

  • +1. However, it was an interesting one. Thanks for the question.

  • Argh, you got me. Good question!

    Yet another reason to add to my list for always prefixing table names or aliases.

  • Koen Verbeeck (3/4/2014)


    Nice question, but the explanation doesn't mention why the order of casting vs order by changes. (because of adding the T alias in the second statement)

    That explanation is important for this question and would have made the question better.

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

  • Koen Verbeeck (3/4/2014)


    Nice question, but the explanation doesn't mention why the order of casting vs order by changes. (because of adding the T alias in the second statement)

    I remember being confused by this many years ago (won't say how many :-D) when I first encountered it. Hopefully this post will help alleviate some frustration.

    When the table alias is included in the ORDER BY clause, the table field value is used to order the records. When it is not, the alias (i.e. the calculated value) is used to order the records.

Viewing 15 posts - 1 through 15 (of 36 total)

You must be logged in to reply to this topic. Login to reply