Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 123»»»

T-SQL - SELECT TOP Expand / Collapse
Author
Message
Posted Tuesday, March 17, 2009 8:28 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Monday, July 7, 2014 10:01 AM
Points: 1,136, Visits: 697
Comments posted to this topic are about the item T-SQL - SELECT TOP
Post #678050
Posted Tuesday, March 17, 2009 8:33 PM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Sunday, March 30, 2014 8:18 PM
Points: 2,778, Visits: 477
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.



Post #678053
Posted Wednesday, March 18, 2009 12:49 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, February 6, 2014 4:15 AM
Points: 1,242, Visits: 1,546
Hi,

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




Thanks
Vinay Kumar
-----------------------------------------------------------------
Keep Learning - Keep Growing !!!
www.GrowWithSql.com

Post #678150
Posted Wednesday, March 18, 2009 12:58 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Tuesday, May 11, 2010 2:15 AM
Points: 327, Visits: 364
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...


Mithun
Post #678153
Posted Wednesday, March 18, 2009 2:34 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Wednesday, September 24, 2014 3:12 AM
Points: 2,112, Visits: 5,480
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.

Adi


--------------------------------------------------------------
To know how to ask questions and increase the chances of getting asnwers:
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #678192
Posted Wednesday, March 18, 2009 2:48 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 10:09 AM
Points: 6,020, Visits: 8,288
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 MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Post #678193
Posted Wednesday, March 18, 2009 4:06 AM


UDP Broadcaster

UDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP Broadcaster

Group: General Forum Members
Last Login: Wednesday, January 2, 2013 12:15 PM
Points: 1,443, Visits: 711
I don't think that query will work without a group by?
Post #678233
Posted Wednesday, March 18, 2009 4:45 AM
Say Hey Kid

Say Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey Kid

Group: General Forum Members
Last Login: Monday, September 8, 2014 4:40 PM
Points: 682, Visits: 893
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.

Post #678259
Posted Wednesday, March 18, 2009 6:08 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, April 19, 2011 7:31 AM
Points: 2,666, Visits: 5,316
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????


-- You can't be late until you show up.
Post #678321
Posted Wednesday, March 18, 2009 6:59 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Friday, September 26, 2014 11:24 AM
Points: 1,896, Visits: 2,045
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?
Post #678376
« Prev Topic | Next Topic »

Add to briefcase 123»»»

Permissions Expand / Collapse