Group by

  • amresh_jhingan (9/6/2010)


    I parsed the query before running it.. and it gave me "Command(s) completed successfully.". Not sure why.. ?

    Syntax check only. And the syntax is okay. But that doesnt mean that it will run.

    You can for instance parse say "select something from nothing" without having a table called "nothing".

  • Nice question, I got it right.

    I did want to point out that since the correct answer is that it will generate an error, no rows will be displayed because of that error. it could then be argued that the "No rows will be displayed" answer could be considered a correct choice as well.

  • Thanks for the question

    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

  • Good question, thanks. I have caused this error many times while composing my queries, so when I realized there was an alias in the GROUP BY clause, I figured that had to be the answer.

    - webrunner

    (Edit) P.S. Thanks, Hugo, for the detailed explanation of why this error happens.

    -------------------
    A SQL query walks into a bar and sees two tables. He walks up to them and asks, "Can I join you?"
    Ref.: http://tkyte.blogspot.com/2009/02/sql-joke.html

  • Nice question. And nice description of logical order from Hugo.

    Tom

  • Thanks to all for the positive comments and thanks Hugo for the logical explanation you gave. Helps a lot!

    Tom please tell me what this means. "Na tog mi gun tuit mi ach ma thuiteas tog! Thig crìoch air an t-saoghal ach mairidh gaol is ceòl "

    :-PManie Verster
    Developer
    Johannesburg
    South Africa

    I can do all things through Christ who strengthens me. - Holy Bible
    I am a man of fixed and unbending principles, the first of which is to be flexible at all times. - Everett Mckinley Dirkson (Well, I am trying. - Manie Verster)

  • Manie Verster (9/10/2010)


    Tom please tell me what this means. "Na tog mi gun tuit mi ach ma thuiteas tog! Thig crìoch air an t-saoghal ach mairidh gaol is ceòl "

    Google translate recognises it as Scottish Celtic, but is not yet able to translate that to either English or Dutch.

    Bing Translator thinks it's Hungarian and translates it to English as "Na train what gun tuit what ach thuiteas train today! Thig crìoch air an t-saoghal ach mairidh gaol also ceòl".

    Guess which of the two competing sites I believe 😉


    Hugo Kornelis, SQL Server/Data Platform MVP (2006-2016)
    Visit my SQL Server blog: https://sqlserverfast.com/blog/
    SQL Server Execution Plan Reference: https://sqlserverfast.com/epr/

  • Manie Verster (9/10/2010)


    Tom please tell me what this means. "Na tog mi gun tuit mi ach ma thuiteas tog! Thig crìoch air an t-saoghal ach mairidh gaol is ceòl "

    As Hugo said, it's Scotland's Celtic language, usually in English called Scottish Gaelic, or Gaelic for short (which of course creates confusion with Manx and Irish Gaelics) or in itself Gàidhlig na h-Alba. The first line means literally "Don't lift me unless I fall but if I fall do lift" but really "don't correct me unless I'm wrong but if I'm wrong do". The second line is a very old saying: "An end will come upon the world but love and music will survive".

    Tom

  • I was up late last night working. I am looking at the question and thinking, "Gee, I didn't think you could group that way" and in my fatigue I chose the wrong answer instead of realizing that you can't group that way.

  • There is a "workaround":

    SELECT DV.[Year],

    SOH.SalesPersonID,

    AverageOrderAmt = SUM(SOH.TotalDue)

    FROM Sales.SalesOrderHeader SOH

    CROSS

    APPLY (SELECT DATEPART(yyyy,OrderDate)) DV ([Year])

    GROUP BY

    DV.[Year],

    SOH.SalesPersonID

    ORDER BY

    DV.[Year] ;

    Over to you, Hugo.

    Paul

  • Great question and even better follow up discussions. Thanks!

  • Based on Hugo's LOGICAL progression where the group by preceeds the select. The alias is not yet defined so grouping is not possible against a column that does not yet exist. The Order By occurs post Select where the alias has been defined so it is acceptable. An alternative solution is Group by datepart(yyyy,orderdate).

Viewing 12 posts - 16 through 27 (of 27 total)

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