Q:

MySQL Group by conditioning

Is there a way to only group by certain rows when a specific value is set?
i.e. I have the following table:
USERNO  |   FIRSTNAME   |   NAME    |   CUSTOMER    |   YEAR
1       |   Jon         |   Snow    |   0           |   2014
2       |   Williams    |   Spear   |   1           |   2015
1       |   John        |   Snow    |   1           |   2015
3       |   Sam         |   Wells   |   0           |   2014
3       |   Sam         |   Wells   |   0           |   2013

When CUSTOMER is 1, it should group by USERNO. So I want the following result:
USERNO  |   FIRSTNAME   |   NAME    |   CUSTOMER    |   YEAR
1       |   Jon         |   Snow    |   1           |   2014
2       |   Williams    |   Spear   |   1           |   2015
3       |   Sam         |   Wells   |   0           |   2014
3       |   Sam         |   Wells   |   0           |   2013

Is this possible?
Thanks in advance!

A:

You can use UNION for getting this type of results like this:
SELECT *
FROM tablename
WHERE CUSTOMER != 1
UNION 
SELECT *
FROM tablename
WHERE CUSTOMER = 1
GROUP BY USERNO;

OR You can also use CASE but you have to specify the ELSE part to group records when condition is not satisfied:
SELECT *
FROM tablename
GROUP BY CASE WHEN CUSTOMER  = 1 THEN USERNO ELSE columnname END;

OR You can use GROUP_CONCAT like this if you want your results to be grouped as a COMMA delimited list:
SELECT 
  GROUP_CONCAT(DISTINCT CAST(USERNO) AS CHAR),
  GROUP_CONCAT(DISTINCT CAST(FIRSTNAME) AS CHAR),
  GROUP_CONCAT(DISTINCT CAST(NAME) AS CHAR),
  CUSTOMER,
  GROUP_CONCAT(DISTINCT CAST(YEAR) AS CHAR)
FROM tablename
GROUP BY CUSTOMER;

