You select the columns you want out of your group, and use some kind of aggregate and alias for the rest - since it looks like all expense values for one date and name are set in only one row, MAX()
could work, but SUM()
is the safer choice for aggregation here. To group by multiple columns, just have a comma separated list of columns in your GROUP BY
statement:
SELECT username, exp_date,
SUM(lodging) AS lodging,
SUM(boarding) AS boarding,
SUM(laundry) AS laundry,
SUM(conveyance) AS conveyance
FROM table
GROUP BY exp_date, username;
That said the table should probably be normalized into three tables, since this looks like it's only in 2nd normal form - but the design works well enough for the purpose.
sqlFiddle
If you specifically want a row for one person on one day, not a row for each person each day there is data, your where clause goes between your from and group clauses:
sqlFiddle