MySQL - how to dynamically add columns with subquery in Select clause based on date range
You must construct different queries to get 1 column of output versus 3 columns.  That is best done in your application language.  If you are trying to do it entirely in SQL, it would require a messy Stored Procedure.