We help IT Professionals succeed at work.

How to structure query with count aggregate

ugeb
ugeb asked
on
252 Views
Last Modified: 2017-03-24
I'm working on a challenge on HackersRank, and brushing up on my SQL skills.  I'm given the following table 'Challenges':

Simple table with hackers and their challengesSo the challenge_id is unique in the table, and hacker_id is the id of the hacker who created the challenge.   I'm trying to meet the challenge step by step, so right now I'm working on what will be an inner query.  

What I'm trying to figure out is how to get a list of hacker_id's where there is no other hacker that created that exact number of challenges.  
Select c2.hacker_id, Count(c2.challenge_id) cnt2 
From Challenges as c2 
Group By c2.hacker_id 
Order By cnt2 Desc;

Open in new window

This query works in getting the number of challenges created by each hacker.  I don't really care about the order in this case, I just have the Order By for viewing intermediate results.

Next, I'm trying to get a list of EITHER all hackers who created a unique number of challenges or who DIDN'T create a unique number of challenges.  Either way works.  Playing around, I tried this:
Select *, count(ch.cnt2) cnt1 From 
(Select c2.hacker_id, Count(c2.challenge_id) cnt2 
From Challenges as c2 
Group By c2.hacker_id) ch, 
Challenges c1
Group By cnt1;

Open in new window

I received the error:
ERROR 1056 (42000) at line 1: Can't group on 'cnt1'
.  

Why can't I group on cnt1?


I want the query to either include or exclude hackers without a unique number of challenges.  
Without changing the approach I'm taking, how can I accomplish this query?
Comment
Watch Question

CERTIFIED EXPERT

Commented:
You can group on columns which are not used in aggregate functions only.

So to find hackers who created unique challenges could be e.g. following:
SELECT hacker_id, COUNT(*) 
  FROM Challenges 
 GROUP BY hacker_id 
HAVING COUNT(*) = 1;

Open in new window

Here it does not matter whether you use COUNT(*)  or  COUNT(challenge_id).
CERTIFIED EXPERT
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION

Author

Commented:
Thank you, but maybe I wasn't clear (or maybe I don't understand your query).  If I'm reading your query correctly, it's returning the set of hackers who created exactly 1 challenge.  But that's not what I meant by unique.  I want the set of hackers who created a unique number of challenges, not just one challenge.  

Let's say the challenges table has entries like:
Challenge_id, hacker_id
   7       99
   8       99
  11      99
  12      77
  14      77
  15      77
  17      44
  18      44
  19      51

In this table, hacker 99 created 3 challenges (7, 8 and 11), hacker 77 also created 3 challenges (12,14,15), hacker 44 created 2 challenges and hacker 51 created 1 challenge.  So, because hackers 99 and 77 both created 3 challenges, 3 is not unique.  However, hacker 44 created 2 challenges and nobody else created exactly two challenges. So, 2 is unique.  Likewise, hacker 51 was the only hacker who created exactly 1 challenge.

Is that clearer?

Author

Commented:
I think we were writing at the same.    Yes, this works.  I just had to get rid of the hacker_id in the outer Group By clause in your query.

Thanks!

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions