Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Comparing row counts and displaying results from greatest row count from a single table


Comparing row counts and displaying results from greatest row count from a single table

Author
Message
gregorykearney
gregorykearney
Forum Newbie
Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)

Group: General Forum Members
Points: 8 Visits: 41
insert into CallData ([800num], CompanyName) values ('8009874321', 'cars');
insert into CallData ([800num], CompanyName) values ('8009874321', 'Newsales');
insert into CallData ([800num], CompanyName) values ('8009874321', 'Newsales');
insert into CallData ([800num], CompanyName) values ('8009870000', 'BenaSales');
insert into CallData ([800num], CompanyName) values ('8009870000', 'BenaSales');
insert into CallData ([800num], CompanyName) values ('8009870000', 'BenaSales2');
insert into CallData ([800num], CompanyName) values ('8009870000', 'BenaSales2');

Requirement 1: When the 800num matches I would like to return a single row, the row with the greatest row count. Requirement 2: If the 800num row count matches then return both rows.

Results would be:

8009874321 Newsales
8009870000 BenaSales
8009870000 BenaSales2
Andrew G
Andrew G
SSCommitted
SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)

Group: General Forum Members
Points: 1999 Visits: 2226
How about this?


select max(x.row) rows, x.[800num], x.CompanyName
from (
select row_number() OVER (partition by a.CompanyName order by a.[800num]) row, a.[800num], a.CompanyName
from calldata a ) as x
group by x.[800num], x.CompanyName


gregorykearney
gregorykearney
Forum Newbie
Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)

Group: General Forum Members
Points: 8 Visits: 41
Thanks for the reply, I only want the results to show the 800 number with the greatest row count.
gregorykearney
gregorykearney
Forum Newbie
Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)

Group: General Forum Members
Points: 8 Visits: 41
Here's what I've come up with:
select distinct [800number], (
   select top 1 CompanyName
      from calldata
      where calldata.[800number]=cd.[800number]
      group by [800number], CompanyName
      order by [800number], count(*) desc) as companyname
from calldata cd

My issue is this solution doesn't list companyname that have matching row counts. Meaning is 8001231234 "newcars" as 3 rows and 8001231234 "cars" has 3 rows only one of those will be listed. I wanted both of them listed because they have the same 800 number - 8001231234. Any ideas how to rewrite the query to accomplish this requirement?
ChrisM@Work
ChrisM@Work
SSCrazy Eights
SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)

Group: General Forum Members
Points: 8923 Visits: 19005
;WITH SampleData AS (
   SELECT * FROM (values
   ('8009874321', 'cars'),
   ('8009874321', 'Newsales'),
   ('8009874321', 'Newsales'),
   ('8009870000', 'Newsales'),
   ('8009870000', 'BenaSales'),
   ('8009870000', 'BenaSales'),
   ('8009870000', 'BenaSales2'),
   ('8009870000', 'BenaSales2')
) sampleData ([800num], CompanyName))

SELECT [800num], CompanyName
FROM (
   SELECT [800num], CompanyName,
      dr = DENSE_RANK() OVER(PARTITION BY [800num] ORDER BY cnt DESC)
   FROM (
      SELECT [800num], CompanyName, cnt = COUNT(*)
      FROM SampleData
      GROUP BY [800num], CompanyName
   Wink d
) e
WHERE dr = 1



“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
gregorykearney
gregorykearney
Forum Newbie
Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)

Group: General Forum Members
Points: 8 Visits: 41
Amazing how fast this query works...Thank you!
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