Friday 15 January 2010

MySQL How to return a COUNT overall and also a COUNT using GROUP BY in one query -



MySQL How to return a COUNT overall and also a COUNT using GROUP BY in one query -

i have next query:

select location, count( location ) total, ( select location, count( location ) responses `trespondent` completion_status in ('started', 'complete') grouping location ) `trespondent` grouping location

this brings error:

operand should contain 1 column(s)

which exclusively right there more 1 row.

what trying achieve, within single query, bring total number each alternative in 'location' , the number have 'started or 'completed' each alternative in 'location'.

any suggestions if possible within single query , if so, pointers welcomed.

you can alter query below using union clause utilize both version of query , overall result

select location, count( location ) total `trespondent` grouping location union select location, count( location ) responses `trespondent` completion_status in ('started', 'complete') grouping location

or using join bring together both query result like

select t1.location t1location, count( t1.location ) total, tab.responses, tab.location tablocation `trespondent` t1 bring together ( select location, count( location ) responses `trespondent` completion_status in ('started', 'complete') grouping location ) tab on t1.location = tab.location grouping t1.location

mysql count group-by

No comments:

Post a Comment