0

I have the following query:

SELECT location, COUNT( location ) AS Total, (

SELECT location, COUNT( location ) AS Responses
FROM  `trespondent` 
WHERE completion_status IN ('Started',  'Complete')
GROUP BY location
)

FROM  `trespondent` 
GROUP BY location

This brings back an error:

Operand should contain 1 column(s)

Which is entirely correct as there will be more than one row.

What I am trying to achieve, within a single query, is to bring back the Total number for each option in 'Location' and then also the the number that have 'Started or 'Completed' for each option in 'Location'.

Any suggestions if this is possible within a single query and if so, any pointers welcomed.

3 Answers 3

1

You can change your query like below using UNION clause to use both version of query and get the overall result

SELECT location, COUNT( location ) AS Total
FROM  `trespondent`
GROUP BY location

UNION ALL

SELECT location, COUNT( location ) AS Responses
FROM  `trespondent` 
WHERE completion_status IN ('Started',  'Complete')
GROUP BY location

Or probably using JOIN to join both query result like

SELECT t1.location as T1location, 
COUNT( t1.location ) AS Total,
tab.Responses,
tab.location as tablocation
FROM  `trespondent` t1 
JOIN
(
SELECT location, COUNT( location ) AS Responses
FROM  `trespondent` 
WHERE completion_status IN ('Started',  'Complete')
GROUP BY location
) tab
ON t1.location = tab.location
GROUP BY t1.location
Sign up to request clarification or add additional context in comments.

1 Comment

The option using 'JOIN' is exactly what I was looking for. Worth noting that the 'UNION' also provide the same information but laid out in a different way (vertically). Thanks @Rahul.
1

You can use conditional sum for the second calculation:

SELECT 
  location, 
  COUNT(*) AS Total, 
  SUM( IF(completion_status IN ('Started',  'Complete'), 1, 0)) as 'Responses'
FROM  trespondent 
GROUP BY location

Comments

0

How about this?

SELECT location, COUNT( location ) AS Total,
       (SELECT COUNT( location ) AS Responses
        FROM  `trespondent` 
        WHERE completion_status IN ('Started',  'Complete')
       )
FROM  `trespondent` 
GROUP BY location;

That is, the subquery for the total doesn't need a group by.

EDIT:

If you want a count, for each location, of the two statuses, then just use conditional aggregation:

SELECT location, COUNT(*) AS Total, SUM(completion_status IN ('Started',  'Complete'))
FROM  `trespondent` 
GROUP BY location;

This seems like the simplest solution.

2 Comments

@Gordon_Linoff - thanks for this, unfortunately it brings back the 'same' total for every option in Location which isn't what I was looking for. Thanks all the same.
@Homer_J . . . your question was unclear (to me) on what to do. The simplest solution would be the version I just added.

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.