You're getting an error because the syntax of your select statement is invalid.
The select should be in format
SELECT...
FROM ...
WHERE ...
GROUP BY ...
You cannot change the places of different clauses.
What comes to the question, since you're using different columns in the
WHERE
clause depending on the parameter, I'd recommend using
IF...ELSE
structure and completely separate statements.