How to query COUNT as well as other columns?

Hi,

I have a data set. Now I want to perform a query that will show me some specific fields as well as the total number of results with count()

I tried this:
select count(s.id), s.id, s.unit_factor from test as s

But it is giving me error: Expression must be a group key or aggregate: (s.id)

Then I tried this: select count(s.id), s.id, s.unit_factor from test as s group by s.id

Now it’s giving error for s.unit_factor field

What is the solution? Should I need to group by all the fields that I query in the select statement?

If you need to project any columns all those must be in group by list. Without that you can’t do aggregation.

select count(s.id), s.id, s.unit_factor 
from  `test`  as s group by s.id, s.unit_factor ;

Thanks a lot for your help