|
|
|
SSC-Addicted
      
Group: General Forum Members
Last Login: Monday, September 14, 2009 5:22 AM
Points: 438,
Visits: 918
|
|
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.
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Friday, November 30, 2012 1:52 AM
Points: 1,116,
Visits: 602
|
|
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 Hensbergen
Help 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
|
|
|
|
|
Mr or Mrs. 500
      
Group: General Forum Members
Last Login: Thursday, January 17, 2013 2:34 PM
Points: 565,
Visits: 360
|
|
| 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 :).
|
|
|
|
|
Old Hand
      
Group: General Forum Members
Last Login: Thursday, January 12, 2012 12:05 PM
Points: 308,
Visits: 126
|
|
| 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.
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Wednesday, June 13, 2012 1:23 AM
Points: 2,162,
Visits: 170
|
|
Hi
The Query need the GROUP BY clause also, as we are using SUM an aggregate function.
|
|
|
|
|
Right there with Babe
      
Group: General Forum Members
Last Login: 2 days ago @ 12:34 PM
Points: 781,
Visits: 5,176
|
|
ghollis (3/18/2009) 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.
I agree. Someone participating in multiple 'salescategories' could actually be the top 'salesperson' Greg E
|
|
|
|
|
UDP Broadcaster
      
Group: General Forum Members
Last Login: Wednesday, April 17, 2013 10:57 PM
Points: 1,491,
Visits: 3,008
|
|
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.
Declare @StartDt Datetime Declare @EndDt Datetime Set @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 CTE select SalesRank ,SalesPerson ,TotSalesDollars from Sales where SalesRank <= 25
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Sunday, August 16, 2009 11:31 PM
Points: 15,
Visits: 40
|
|
I don't understand. Can you give me Full Script ? (use the AventureWorks Database) I think must add the group by
|
|
|
|
|
Old Hand
      
Group: General Forum Members
Last Login: Thursday, January 12, 2012 12:05 PM
Points: 308,
Visits: 126
|
|
| 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)?
|
|
|
|
|
UDP Broadcaster
      
Group: General Forum Members
Last Login: Wednesday, April 17, 2013 10:57 PM
Points: 1,491,
Visits: 3,008
|
|
ghollis (3/19/2009) 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)?
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).....
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 All Select 'Andy', 24554.22, '20090103' union All Select 'Len', 22222.22, '20090103' union All Select 'Tom', 22222.22, '20090103' union All Select 'Maria', 4554.22, '20090103' union All Select 'Chris', 6654.22, '20090103' union All Select 'Tony', 4554.22, '20090103' union All Select 'Sally', 4654.22, '20090103' union All Select 'Bob', 4554.22, '20090103' union All Select 'Bob', 4554.22, '20090103'
Declare @StartDt Datetime Declare @EndDt Datetime Set @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
and results:
Salesperson TotSales ------------------------------ --------------------- Judy 46962.66 Joyce 26554.22 Ed 25554.22 Andy 24554.22 Len 22222.22 Tom 22222.22
(6 row(s) affected)
|
|
|
|