﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / Article Discussions / Article Discussions by Author / Discuss content posted by Robert Griffin  / T-SQL - SELECT TOP / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Wed, 22 May 2013 14:45:11 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: T-SQL - SELECT TOP</title><link>http://www.sqlservercentral.com/Forums/Topic678050-1510-1.aspx</link><description>Yes, there are issues with the query itself, but what did the question ask?  How do you ensure you get the top 25 in sales.  The only answer that works there is that you also need to add an ORDER BY SUM(salesdollars) DESC.</description><pubDate>Thu, 19 Mar 2009 11:13:32 GMT</pubDate><dc:creator>Lynn Pettis</dc:creator></item><item><title>RE: T-SQL - SELECT TOP</title><link>http://www.sqlservercentral.com/Forums/Topic678050-1510-1.aspx</link><description>[quote][b]ghollis (3/19/2009)[/b][hr]Wouldn't using Top 25 WITH TIES  accomplish the same thing, rather than using the CTE (assuming they are using the group by and order by clauses without the SalesCategory being included in the query)?[/quote]Yes, it does.  Thank you for pointing out this option.  This works well.  Here's a test script ( I didn't bother including salesCategory in the table definition).....[code]create table #SalesTbl(			Salesperson char(30)			,SalesDollars money			,SalesDate datetime			)insert #SalesTbl Select 'Judy', 14554.22, '20090103' union All Select 'Judy', 15854.22, '20090103' union All Select 'Judy', 16554.22, '20090103' union All Select 'Joyce', 26554.22, '20090103' union All Select 'Ed', 25554.22, '20090103' union AllSelect 'Andy', 24554.22, '20090103' union All Select 'Len', 22222.22, '20090103' union AllSelect 'Tom', 22222.22, '20090103' union AllSelect 'Maria', 4554.22, '20090103' union AllSelect 'Chris', 6654.22, '20090103' union AllSelect 'Tony', 4554.22, '20090103' union AllSelect 'Sally', 4654.22, '20090103' union AllSelect 'Bob', 4554.22, '20090103' union AllSelect 'Bob', 4554.22, '20090103' Declare @StartDt DatetimeDeclare @EndDt  DatetimeSet @StartDt = '20090101'Set @EndDt   = '20090131'Select top 5 with ties 		 Salesperson		,sum(salesDollars) TotSales  from #SalesTbl	Where SalesDate between @StartDt and @EndDt        	group by SalesPerson    order by sum(salesDollars) Desc[/code]and results:[code]Salesperson                    TotSales------------------------------ ---------------------Judy                           46962.66Joyce                          26554.22Ed                             25554.22Andy                           24554.22Len                            22222.22Tom                            22222.22(6 row(s) affected)[/code]</description><pubDate>Thu, 19 Mar 2009 10:49:03 GMT</pubDate><dc:creator>john.arnott</dc:creator></item><item><title>RE: T-SQL - SELECT TOP</title><link>http://www.sqlservercentral.com/Forums/Topic678050-1510-1.aspx</link><description>Wouldn't using Top 25 WITH TIES  accomplish the same thing, rather than using the CTE (assuming they are using the group by and order by clauses without the SalesCategory being included in the query)?</description><pubDate>Thu, 19 Mar 2009 09:31:11 GMT</pubDate><dc:creator>ghollis</dc:creator></item><item><title>RE: T-SQL - SELECT TOP</title><link>http://www.sqlservercentral.com/Forums/Topic678050-1510-1.aspx</link><description>I don't understand. Can you give me Full Script ? (use the AventureWorks Database)I think must add the group by</description><pubDate>Wed, 18 Mar 2009 19:42:01 GMT</pubDate><dc:creator>linna</dc:creator></item><item><title>RE: T-SQL - SELECT TOP</title><link>http://www.sqlservercentral.com/Forums/Topic678050-1510-1.aspx</link><description>One more "real-world" caveat:  there could be a tie for the number 25 spot, so simply using "Top 25" in the select may leave a worthy salesperson off the list.  One way to include all who belong would be to use the RANK() function.  I like to wrap such in a CTE, but a correlated sub-query or other structure may be just as sound (or better) for your use. [code]Declare @StartDt DatetimeDeclare @EndDt	 DatetimeSet @StartDt = '20090101'Set @EndDt   = '20090131';with Sales (SalesRank, SalesPerson, TotSalesDollars)as (Select	 SalesRank = Rank() over (order by sum(SalesDollars) Desc)		,SalesPerson		,sum(SalesDollars)  from #SalesTbl Where SalesDate between @StartDt and @EndDt   	  group by SalesPerson) --end of CTEselect	SalesRank		,SalesPerson		,TotSalesDollars  from Sales where SalesRank &amp;lt;= 25[/code]</description><pubDate>Wed, 18 Mar 2009 16:41:52 GMT</pubDate><dc:creator>john.arnott</dc:creator></item><item><title>RE: T-SQL - SELECT TOP</title><link>http://www.sqlservercentral.com/Forums/Topic678050-1510-1.aspx</link><description>[quote][b]ghollis (3/18/2009)[/b][hr]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.[/quote]I agree. Someone participating in multiple 'salescategories' could actually be the top 'salesperson'Greg E</description><pubDate>Wed, 18 Mar 2009 14:01:18 GMT</pubDate><dc:creator>Greg Edwards-268690</dc:creator></item><item><title>RE: T-SQL - SELECT TOP</title><link>http://www.sqlservercentral.com/Forums/Topic678050-1510-1.aspx</link><description>HiThe Query need the GROUP BY clause also, as we are using SUM an aggregate function.</description><pubDate>Wed, 18 Mar 2009 09:16:32 GMT</pubDate><dc:creator>Balachandra</dc:creator></item><item><title>RE: T-SQL - SELECT TOP</title><link>http://www.sqlservercentral.com/Forums/Topic678050-1510-1.aspx</link><description>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.</description><pubDate>Wed, 18 Mar 2009 08:13:33 GMT</pubDate><dc:creator>ghollis</dc:creator></item><item><title>RE: T-SQL - SELECT TOP</title><link>http://www.sqlservercentral.com/Forums/Topic678050-1510-1.aspx</link><description>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 :).</description><pubDate>Wed, 18 Mar 2009 08:07:01 GMT</pubDate><dc:creator>Michael Poppers</dc:creator></item><item><title>RE: T-SQL - SELECT TOP</title><link>http://www.sqlservercentral.com/Forums/Topic678050-1510-1.aspx</link><description>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...</description><pubDate>Wed, 18 Mar 2009 07:23:58 GMT</pubDate><dc:creator>Ronald H</dc:creator></item><item><title>RE: T-SQL - SELECT TOP</title><link>http://www.sqlservercentral.com/Forums/Topic678050-1510-1.aspx</link><description>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.</description><pubDate>Wed, 18 Mar 2009 07:16:44 GMT</pubDate><dc:creator>J-440512</dc:creator></item><item><title>RE: T-SQL - SELECT TOP</title><link>http://www.sqlservercentral.com/Forums/Topic678050-1510-1.aspx</link><description>[quote][b]dmw (3/18/2009)[/b][hr]...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.[/quote]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?</description><pubDate>Wed, 18 Mar 2009 06:59:41 GMT</pubDate><dc:creator>Chris Harshman</dc:creator></item><item><title>RE: T-SQL - SELECT TOP</title><link>http://www.sqlservercentral.com/Forums/Topic678050-1510-1.aspx</link><description>I too selected the most accurate, albeit incomplete, answer. [quote][b]dmw (3/18/2009)[/b][hr]Poor (non-existent?) QA on this question. Unexpected from this site.[/quote]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:</description><pubDate>Wed, 18 Mar 2009 06:08:17 GMT</pubDate><dc:creator>  tosscrosby</dc:creator></item><item><title>RE: T-SQL - SELECT TOP</title><link>http://www.sqlservercentral.com/Forums/Topic678050-1510-1.aspx</link><description>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.</description><pubDate>Wed, 18 Mar 2009 04:45:51 GMT</pubDate><dc:creator>Martin Wills</dc:creator></item><item><title>RE: T-SQL - SELECT TOP</title><link>http://www.sqlservercentral.com/Forums/Topic678050-1510-1.aspx</link><description>I don't think that query will work without a group by?</description><pubDate>Wed, 18 Mar 2009 04:06:33 GMT</pubDate><dc:creator>SuperDBA-207096</dc:creator></item><item><title>RE: T-SQL - SELECT TOP</title><link>http://www.sqlservercentral.com/Forums/Topic678050-1510-1.aspx</link><description>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: [code]BETWEEN '20090101' AND '20090131'[/code]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[code]WHERE salesdate &amp;gt;= '20090101'AND salesdate &amp;lt; '20090201'[/code]because otherwise all sales made after Jan 31st, 0:00 will be excluded.</description><pubDate>Wed, 18 Mar 2009 02:48:00 GMT</pubDate><dc:creator>Hugo Kornelis</dc:creator></item><item><title>RE: T-SQL - SELECT TOP</title><link>http://www.sqlservercentral.com/Forums/Topic678050-1510-1.aspx</link><description>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 </description><pubDate>Wed, 18 Mar 2009 02:34:47 GMT</pubDate><dc:creator>Adi Cohn-120898</dc:creator></item><item><title>RE: T-SQL - SELECT TOP</title><link>http://www.sqlservercentral.com/Forums/Topic678050-1510-1.aspx</link><description>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</description><pubDate>Wed, 18 Mar 2009 00:58:11 GMT</pubDate><dc:creator>mithun.gite</dc:creator></item><item><title>RE: T-SQL - SELECT TOP</title><link>http://www.sqlservercentral.com/Forums/Topic678050-1510-1.aspx</link><description>Hi, It should be GROUP BY, otherwise it'll return error.</description><pubDate>Wed, 18 Mar 2009 00:49:50 GMT</pubDate><dc:creator>Danny Ocean</dc:creator></item><item><title>RE: T-SQL - SELECT TOP</title><link>http://www.sqlservercentral.com/Forums/Topic678050-1510-1.aspx</link><description>For that query to work it should be grouped by salescategory, salesperson, otherwise you will get this error when running the queryColumns salescategory, salesperson are invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.</description><pubDate>Tue, 17 Mar 2009 20:33:57 GMT</pubDate><dc:creator>jadsmith</dc:creator></item><item><title>T-SQL - SELECT TOP</title><link>http://www.sqlservercentral.com/Forums/Topic678050-1510-1.aspx</link><description>Comments posted to this topic are about the item [B]&lt;A HREF="/questions/Top/65914/"&gt;T-SQL - SELECT TOP&lt;/A&gt;[/B]</description><pubDate>Tue, 17 Mar 2009 20:28:47 GMT</pubDate><dc:creator>Bob Griffin</dc:creator></item></channel></rss>