max of count disaster MS SQL Server

oMG aggregate nightmare ( but there are worse nightmares  {:P )

Goal:
Find the name and license of the customer who has Test_Driven the maximum number of cars.
      
Bad attempt:
      Select c.cname, c.license from Customer c where c.license in
         (Select t.license, count(distinct t.license) as count_drives from Test_Drives t
                  where count_drives = max(count_drives) group by t.license );

    (see attached jpegs for better attempt)
Customer.jpg
Test-Drives.jpg
innerMAXofCount.jpg
outerMAXofCount.jpg
kayveyAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kevin CrossChief Technology OfficerCommented:
Hello kayvey,

Try this (see code snippet).

Regards,

mwvisa1
Select TOP 1 c.cname, c.license, td.count_drives
from Customer c 
inner join (select license, count(distinct t.license) as count_drives from Test_Drives t group by t.license) td
on td.license = c.license
group by c.name, c.license
order by td.count_drives DESC

Open in new window

0
Kevin CrossChief Technology OfficerCommented:
And your original count based on distinct license will probably not work in my code as this will end up being 1 for all drivers, so try this:
Select TOP 1 c.cname, c.license, td.count_drives
from Customer c 
inner join (select license, count(*) as count_drives from Test_Drives group by license) td
on td.license = c.license
group by c.name, c.license
order by td.count_drives DESC

Open in new window

0
kayveyAuthor Commented:
I was dubious on that syntax.. this looks like progress.. I tried, there's no such
thing as "bottom"..

umm.. I just Schtuck that top thing in my query.  My query is good, but it fails to select
the "max" ..

take a look at
topSyntz.jpg
0
IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

Kevin CrossChief Technology OfficerCommented:
Did you look at my last suggestion?  It should get you the max customer ...
0
kayveyAuthor Commented:
You've changed all the variables.  It gets me the LEAST prolific test driver.
0
kayveyAuthor Commented:
Don't be afraid to hit return. inner join is not needed.
qry12.jpg
0
kayveyAuthor Commented:
Okay these points are in the bag, but take a stab at this question:

http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SQL_Server_2008/Q_23873127.html#a22873286

to claim your points
0
Kevin CrossChief Technology OfficerCommented:
Glad you got that working.

(YES, count_drives should be in the group by...woops!!)

If you have other open questions, I will take a look but looks like this one was answered.
Select TOP 1 c.cname, c.license, td.count_drives
from Customer c 
inner join (select license, count(*) as count_drives from Test_Drives group by license) td
on td.license = c.license
group by c.name, c.license, td.count_drives
order by td.count_drives DESC

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.