• Comments posted to this topic are about the item T-SQL - SELECT TOP

  • For that query to work it should be grouped by salescategory, salesperson, otherwise you will get this error when running the query

    Columns salescategory, salesperson are invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

  • Hi,

    It should be GROUP BY, otherwise it'll return error.

    Vinay Kumar
    Keep Learning - Keep Growing !!!

  • i think this the group by was left by mistake ...

    if we over look that group by then the option 3 is the right answer and thats pretty much fineee...


  • I was looking for an option that will say that the query will cause an error (because of the missing group by) but I didn’t find one. I ignored the error and chose what is considered the correct answer, but I think that all the suggested answers were wrong.


    To know how to ask questions and increase the chances of getting asnwers:

    For better answers on performance questions, click on the following...

  • Others have already pointed out the missing GROUP BY clause, so I'll skip that and limit myself to commenting the non-portable date format used. On many non-US systems, the date '1/31/09' will cause an error message to pop up. The correct format would be: BETWEEN '20090101' AND '20090131'

    And if the version is SQL2005 or less, or if it's SQL2008 but the data type is not "date" but one of the formats that include a time component as well, then it should even be

    WHERE salesdate >= '20090101'

    AND salesdate < '20090201'

    because otherwise all sales made after Jan 31st, 0:00 will be excluded.

    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/

  • I don't think that query will work without a group by?

  • Poor (non-existent?) QA on this question. Unexpected from this site.

    And what about the role of sales_category? The results of the query will also reflect the 'performance' of the sales_category, whatever that is.

  • I too selected the most accurate, albeit incomplete, answer.

    dmw (3/18/2009)

    Poor (non-existent?) QA on this question. Unexpected from this site.

    I couldn't agree more but I'm sure with the volume of questions that are sorted through to ultimately be posted, a clunker is going to get through every now and then. Maybe we've become so spoiled by the overall high quality of the site and the information provided here that even some of us "gurus-in-training" are actually learning something which allows us to catch things like this easier/faster???? :w00t:

    -- You can't be late until you show up.

  • dmw (3/18/2009)

    ...And what about the role of sales_category? The results of the query will also reflect the 'performance' of the sales_category, whatever that is.

    This is the part that really bothers me. OK, so they forgot the GROUP BY, but if we are grouping by sales_category but the question asks for total sales, we haven't really satisfied the requirements have we?

  • I agree that the first thing I would have done to correct the query would be to make it work by adding the obviously missing GROUP BY clause. AFTER that, if I could not correctly determine that last scenario was the proper answer it would be a simple matter to populate a few tables and run the query.

    Owing to the fact that query would not even run, I think that the set of possible answers did not match the situation. Imagine if a fourth alternative "Add GROUP BY" had been presented. By itself it would not have produced the stipulated results but at the very least it would have turned it into a runnable query. How many would have selected the fourth answer ?

    I have seen a few more questions of this ilk. For instance where the official solution was to change a table definition, which in real-life would mean that it was likely to break existing code.

    When such errors are pointed out, the points awarded for the question of the day should be clawed back.

  • I think the correct query would be to add the GROUP BY of course, and also ORDER BY 3 DESC. Or name the derived item, and order by that name. I wonder how much the author have tested his own query...

    Ronald HensbergenHelp us, help yourself... Post data so we can read and use it: http://www.sqlservercentral.com/articles/Best+Practices/61537/-------------------------------------------------------------------------2+2=5 for significant large values of 2

  • Glad 2 C there was discussion 'bout this Q's failings, but I trust most of us picked the A which was closest to what we thought the questioner was after :).

  • I agree with the previous comment that even with a group by, top 25, and order by, you are not going to get what the question asked for as the group by would have to group on the salescategory and salesperson; whereas, the question asked for the top 25 sales people, so salescategory would need to be removed.

  • Hi

    The Query need the GROUP BY clause also, as we are using SUM an aggregate function.

    Balachandra Srinevasalu

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

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