?
Solved

MySQL SELECT GROUP COUNT Query

Posted on 2011-09-26
6
Medium Priority
?
579 Views
Last Modified: 2012-05-12
Hi,

I have a requirement to count 3 different amounts of content in a selected field.

My logic has written a query as follows, but this doesn't work as expected. Please can anyone shed some light?

Thanks,

Marty
SELECT
	A.FriendlyName,
	A.Delegates,
	COUNT(DISTINCT rU.ID_User) AS Registered,
	COUNT(DISTINCT aU.ID_User) AS Accepted,
	COUNT(DISTINCT pU.ID_User) AS Partial
	FROM
	ATL_V2_Affiliates AS A
	Left Join ATL_Users AS rU ON rU.ID_Affiliate AND rU.RegStatus='Registered'
	Left Join ATL_Users AS aU ON aU.ID_Affiliate AND aU.RegStatus='Accepted'
	Left Join ATL_Users AS pU ON pU.ID_Affiliate AND pU.RegStatus='Partial'
	GROUP BY
	A.ID_Affiliate
	ORDER BY
	A.FriendlyName;

Open in new window

0
Comment
Question by:marty_t
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 15

Expert Comment

by:Eyal
ID: 36598262
A.FriendlyName and A.Delegates should be also be included in the group by
and also do right joins
0
 
LVL 1

Author Comment

by:marty_t
ID: 36598283
I added the two fields in the Group By, but that makes no difference.

Using Right Joins instead of Left Joins gives zero rows.

I would like every row from "A" and counts from the others if they contain rows.

Thanks,

Marty
0
 
LVL 1

Author Comment

by:marty_t
ID: 36598306
This works as expected, but I would like to do it via joins instead.

Any ideas?

Thanks,

Marty
SELECT
	A.FriendlyName,
	A.Delegates,
	(SELECT COUNT(rU.ID_User) FROM ATL_Users AS rU WHERE rU.ID_Affiliate=A.ID_Affiliate AND rU.RegStatus='Registered') AS Registered,
	(SELECT COUNT(aU.ID_User) FROM ATL_Users AS aU WHERE aU.ID_Affiliate=A.ID_Affiliate AND aU.RegStatus='Accepted') AS Accepted,
	(SELECT COUNT(pU.ID_User) FROM ATL_Users AS pU WHERE pU.ID_Affiliate=A.ID_Affiliate AND pU.RegStatus='Partial') AS Partial
	FROM
	ATL_V2_Affiliates AS A
	ORDER BY
	A.FriendlyName;

Open in new window

0
Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

 
LVL 15

Expert Comment

by:Eyal
ID: 36598349
this query will have better performance. leave it like this
0
 
LVL 15

Accepted Solution

by:
Eyal earned 1000 total points
ID: 36598356
Left Join ATL_Users AS rU ON rU.ID_Affiliate=A.ID_Affiliate AND rU.RegStatus='Registered'
Left Join ATL_Users AS aU ON aU.ID_Affiliate=A.ID_Affiliate AND aU.RegStatus='Accepted'
Left Join ATL_Users AS pU ON pU.ID_Affiliate=A.ID_Affiliate AND pU.RegStatus='Partial'
0
 
LVL 1

Author Closing Comment

by:marty_t
ID: 36598463
I cannot believe I let out the =A.ID_Affiliate

Such a schoolboy error, I've been writing these queries for over a decade and couldn't see the wood for the trees.

Many thanks for pointing out my error.

Marty
0

Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently I was talking with Tim Sharp, one of my colleagues from our Technical Account Manager team about MongoDB’s scalability. While doing some quick training with some of the Percona team, Tim brought something to my attention...
Lotus Notes has been used since a very long time as an e-mail client and is very popular because of it's unmatched security. In this article we are going to learn about  RRV Bucket corruption and understand various methods to Fix "RRV Bucket Corrupt…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question