Solved

MySQL SELECT GROUP COUNT Query

Posted on 2011-09-26
6
552 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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 250 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

920 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now