SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Count


Count

Author
Message
sharonsql2013
sharonsql2013
SSChasing Mays
SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)

Group: General Forum Members
Points: 600 Visits: 1265
I am trying to count the serversState based on server. Here is the query and Output

Select top 20 COUNT(State) StateCount ,Server_Name
FROM Production
Group By Server_Name
Order By COUNT(State ) Desc

StateCount Server_Name
11 abc
8 cde
5 JDE
.........

My Concern is I need to get top 20 But if the StateCount of 20 and 21st are same then get 21 row , If 21st and 22nd is same then get that too... Until all of the ones with same StateCount as of Count 20 are covered. I hope I am not confusing.....
Lynn Pettis
Lynn Pettis
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39948 Visits: 38564
Add WITH TIES to your select top 20. Hope the following helps illustrate what you need:



create table dbo.production(
ServerName varchar(20),
ServerState varchar(10)
);

insert into dbo.production
values ('ABC','A'),('ABC','B'),('ABC','C'),('ABC','D'),('ABC','E'),
('CDE','A'),('CDE','B'),('CDE','C'),('CDE','D'),('CDE','E'),
('FGH','A'),('FGH','B'),('FGH','C'),('FGH','D'),
('ZXC','A'),('ZXC','B'),('ZXC','C'),('ZXC','D'),('ZXC','E')

Select top 1 with ties COUNT(ServerState) StateCount ,ServerName
FROM production
Group By ServerName
Order By COUNT(ServerState ) Desc;

drop table dbo.production;




Please note that I was running this in a sandbox database and clean up after using the table.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
sharonsql2013
sharonsql2013
SSChasing Mays
SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)

Group: General Forum Members
Points: 600 Visits: 1265
Great! I did not know we could use With Ties with TOP Clause.
Thanks a Ton
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search