Returning a recordcount from a subquery in a result set.
Posted
by KeRiCr
on Stack Overflow
See other posts from Stack Overflow
or by KeRiCr
Published on 2010-03-24T01:48:29Z
Indexed on
2010/03/24
1:53 UTC
Read the original article
Hit count: 193
I am attempting to return a rowcount from a subquery as part of a result set. Here is a sample that I've tried that didn't work:
SELECT recordID
, GroupIdentifier
, count() AS total
, (SELECT COUNT() FROM table WHERE intActingAsBoolean = 1) AS Approved
FROM table
WHERE date_format(Datevalue, '%Y%m%d') BETWEEN 'startDate' AND 'endDate'
GROUP BY groupIdentifier
What I'm attempting to return for 'Approved' is the number of records for the grouped value where intActingAsBoolean = 1. I have also tried modifying the where clause by giving the main query a table alias and applying an AND clause to match the groupidentifier in the subquery to the main query. None of these are returning the correct results. The query as written returns all records in the table where intActingAsBoolean = 1.
This query is being run against a MySQL database.
© Stack Overflow or respective owner