Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
dim
Yak Posting Veteran
57 Posts |
Posted - 2010-10-08 : 16:06:18
|
| Hi,I have a requiremnet wherein I need to list all the shop names and the managers which gets the highest no of calls. There can be more than one manager associated with the shop...but we need to find only those who have maximum no of calls. The query below gives me all the managers and their call with each shop...select t.shop_name,t.manager_code,t.number_of_total_callsfrom(select shop_Name,manager_code,sum(Number_of_Total_Calls) as Number_Of_Total_Callsfrom dbo.Facilities --where -- Number_of_Total_Calls = (SELECT MAX(Number_of_Total_Calls) FROM dbo.Facilities )group byShop_Name,Manager_Code) torder by Number_Of_Total_Calls descThe output that we want should be Shop ManagerCode Total No of Calls 1. 265 1002. 300 250Please advice ..Thank you,Dp |
|
|
slimt_slimt
Aged Yak Warrior
746 Posts |
Posted - 2010-10-09 : 13:47:46
|
just a simple correction:select t.shop_name,t.manager_code,max(t.number_of_total_calls) as max_nof_callsfrom(selectshop_Name,manager_code,sum(Number_of_Total_Calls) as Number_Of_Total_Callsfromdbo.Facilitiesgroup byShop_Name,Manager_Code) tgroup byt.shop_name,t.manager_codeorder by max_nof_calls desc |
 |
|
|
dim
Yak Posting Veteran
57 Posts |
Posted - 2010-10-11 : 09:36:09
|
| Hi slimt_slimt,Thank you for your prompt response. I tried implementing this query select t.shop_name,t.manager_code,max(t.number_of_total_calls) as max_nof_callsfrom(selectshop_Name,manager_code,sum(Number_of_Total_Calls) as Number_Of_Total_Callsfromdbo.Facilitiesgroup byShop_Name,Manager_Code) tgroup byt.shop_name,t.manager_codeorder by max_nof_calls descBut what I am getting is Shop Name || ManagerCode || Max No Of CallsABC 110 200BCD 120 400ABC 100 500BCD 125 250GDF 300 210 For the Shop ABC there should be only one Manager displayed in the result set. And that manager should have made the max no of calls for the shop ABC. Here what is happening is we are displaying more than one manager for that shop. So I should be like Shop Name || ManagerCode || Max No Of CallsBCD 120 400ABC 100 500GDF 300 210 Please advice. Thank You, |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-10-11 : 09:41:23
|
| [code]select shop_Name,manager_code,Number_Of_Total_Calls from(select shop_Name,manager_code,Number_Of_Total_Calls ,row_number() over (partition by shop_Name order by Number_Of_Total_Calls desc) as seqfrom(selectshop_Name,manager_code,sum(Number_of_Total_Calls) as Number_Of_Total_Callsfromdbo.Facilitiesgroup byShop_Name,Manager_Code)t)rwhere seq=1[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
dim
Yak Posting Veteran
57 Posts |
Posted - 2010-10-11 : 10:23:51
|
| Thank You visakh16. The code works !Dp |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-10-11 : 12:05:25
|
| welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|