3

We have three tables that we would like to query to find out the count of clients per business and the number of records per business

The three tables are:

businessDetails
-------------------
businessDetails.ID
businessDetails.name

clientDetails
-------------------
clientDetails.ID
clientDetails.businessDetailsID

records
-------------------
records.ID
records.businessDetailsID

We don't have any problem with being able to select a count from two of the tables at one time (businessDetails plus either clientDetails OR records). For example

SELECT  businessDetails.name AS businessName
        , COUNT(clientDetails.businessDetailsID) AS totalClients
FROM `businessDetails`
INNER JOIN clientDetails 
        ON clientDetails.businessDetailsID = businessDetails.businessDetailsID
GROUP BY 
businessDetails.name
ORDER BY
totalClients DESC

This query gives us a nice result as expected:

--------------------------------
businessName    |   totalClients
--------------------------------
Initech         |   23
Cylon Inc       |   148
The Dude Ltd    |   71

The problem we are running into is how to do a count on both clientDetails AND records at once. We tried the following query, but have found that it multiplies the number of totalClients for some reason:

SELECT  businessDetails.name AS businessName
        , COUNT(clientDetails.businessDetailsID) AS totalClients
        , COUNT(records.businessDetailsID) AS totalRecords
FROM `businessDetails`
INNER JOIN clientDetails 
        ON clientDetails.businessDetailsID = businessDetails.businessDetailsID
INNER JOIN records ON records.businessDetailsID = businessDetails.ID
GROUP BY 
businessDetails.name
ORDER BY
totalClients DESC

This returns a result something like:

--------------------------------------------------------
businessName    |   totalClients    |   totalRecords
--------------------------------------------------------
Initech         |   93              |    93
Cylon Inc       |   398             |    398
The Dude Ltd    |   215             |    215

I expect we're just making some simple error. Any help would be greatly appreciated.

3 Answers 3

4

You need something like this:

SELECT  
    businessDetails.name AS businessName,
    (SELECT count('x') FROM clientDetails WHERE clientDetails.businessDetailsID = businessDetails.businessDetailsID) as totalclients,
    COUNT(records.businessDetailsID) AS totalRecords
FROM 
    `businessDetails`
    INNER JOIN records ON records.businessDetailsID = businessDetails.ID
GROUP BY 
    businessDetails.name
ORDER BY
    totalClients DESC
Sign up to request clarification or add additional context in comments.

2 Comments

@direct00. Note this could have some performance issues with large recordsets, due to the sub-query.
+1 interesting: MySql doesn't always cope with COUNT DISTINCT very well; that's probably why this variation works better than the one I outlined.
2

How about if you add a DISTINCT like this:

SELECT  businessDetails.name AS businessName
        , COUNT(distinct clientDetails.ID) AS totalClients
        , COUNT(distinct records.ID) AS totalRecords
FROM `businessDetails`
INNER JOIN clientDetails 
        ON clientDetails.businessDetailsID = businessDetails.businessDetailsID
INNER JOIN records ON records.businessDetailsID = businessDetails.ID
GROUP BY 
businessDetails.name
ORDER BY
totalClients DESC

that way, for every group of businessDetails.name, you have the count of distinct clientDetails and records within that "window".

4 Comments

I think that will give you '1' for both counts. You could use distinct clientdetails.clientid and distinct records.recordid if both tables have their own unique ids.
you're right! thanks for pointing out the oversight. have edited the answer accordingly.
Correct - just adding DISTINCT returned 1 for both.
Both tables have their own unique IDs, so I tried using DISTINCT with those instead, and the query timed out.
1

Count(Distinct [FieldName]) works in MYSQL. Give It a try.

SELECT  businessDetails.name AS businessName
        , COUNT(Distinct clientDetails.businessDetailsID) AS totalClients
        , COUNT(records.businessDetailsID) AS totalRecords
FROM `businessDetails`
INNER JOIN clientDetails 
        ON clientDetails.businessDetailsID = businessDetails.businessDetailsID
INNER JOIN records ON records.businessDetailsID = businessDetails.ID
GROUP BY 
businessDetails.name
ORDER BY
totalClients DESC

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.