Question: I am getting the sum of all the rows in `registration` table. Table structure looks like this:

```
row_id  row_registration_fee  row_amount  row_class  row_user_id
1          200                 1000         18           1
2          200                 2510         18           1
3            0                 1600         19           2
4            0                 1500         19           1
5          200                 1254         19           3
6          200                 3000         19           1
7          200                 2000         19           1
8            0                  100         20           1
9            0                  300         20           2

```

A user can have multiple registration fee. And I need to get the sum of all the `row_registration_fee` by `row_class`. The result should be like this:

```
row_registration_fee  row_class
       200                18
       400                19
         0                20

```

My select :

```
SELECT (COUNT(DISTINCT(row_user_id))* 200) as 'fee' 
FROM registration 
WHERE row_registration_fee  > 0  
GROUP BY row_class

```

Is there a better query here that can give a result like the above sample? 

The result will be displayed in table rows using foreach loop in PHP. As of now, it will only give me two results for the registration fee, the `row_class 18` and `row_class 19` it excludes the `row_class 20` because it only selects the user with a fee.

**Additional explanation**: The user with 2 or more registration fees should count only as one if the user has a total of 400 fees it should sum only 200.

Answer: 
Another approach might be to find the most recent row\_id. In this case I have altered the data so that the first entry appears to be an error (fee = 300) followed by the second entry for the correct amount .

```
drop table if exists t;
create table t(
row_id  int,row_registration_fee  int,row_amount int, row_class int, row_user_id int);
insert into t values
(1    ,      300  ,               1000 ,        18    ,       1),
(2    ,      200  ,               2510 ,        18    ,       1),
(3    ,        0  ,               1600 ,        19    ,       2),
(4    ,        0  ,               1500 ,        19    ,       1),
(5    ,      200  ,               1254 ,        19    ,       3),
(6    ,      200  ,               3000 ,        19    ,       1),
(7    ,      200  ,               2000 ,        19    ,       1),
(8    ,        0  ,                100 ,        20    ,       1),
(9    ,        0  ,                300 ,        20    ,       2)
;

select sum(row_registration_fee),row_class
from
(
select t.row_class,t.row_registration_fee
from t
where t.row_id = (select max(row_id) from t t1 where t1.row_user_id = t.row_user_id and t1.row_class = t.row_class)
) a
group by row_class;

+---------------------------+-----------+
| sum(row_registration_fee) | row_class |
+---------------------------+-----------+
|                       200 |        18 |
|                       400 |        19 |
|                         0 |        20 |
+---------------------------+-----------+
3 rows in set (0.00 sec)

```
```
   SELECT SUM(row_registration_fee), row_class, row_user_id
    FROM registration 
    WHERE row_registration_fee  > 0
    GROUP BY row_class, row_user_id;

```

Yeah, group by two columns you can get per class and user.