Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

MySQL SELECT GROUP COUNT Query

Posted on 2011-09-26
6
Medium Priority
?
589 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
  • 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
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

972 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