I have the following table,
id | add_card_id | rate | category | category_count | counts | total
7597 | 723 | 3 | 7 | 1 | 300 | 108
7596 | 723 | 3 | 9 | 1 | 500 | 180
7594 | 723 | 3 | 11 | 1 | 400 | 240
7593 | 723 | 5 | 10 | 2 | 400 | 240
7593 | 723 | 5 | 13 | 2 | 400 | 240
I want to get sum of total
(column name) where rate
is same but sum include only nth no. of rows having max total, nth is defined by category_count
(column name).
e.g the rate of first three column is 3 and category_ count is 1 so sum (total) first three rows is 240 because 240 > 180 > 108 and in the case of last two rows rate is 5 and category_count is 2 so 240 +240= 480 and i want 720= 240(from first three rows) + 480 (from last two rows).
I have tried the following query
SELECT SUM(total)
FROM (SELECT MAX(total)
FROM tableName
GROUP BY rate, category_count) as newTable
it's giving me 240+ 240 so how can I give the limit by category_count
(column)?