I am trying to extract a sum of modified history field limited by 100 and grouped by each account, but the query below extracts sum of all rows from CDR_Accounts with charged_quantity = 60 and does not limit to 100.
table1 = table2 and these are the temporary tables. I have simplified the query and filtered all data in temporary tables.
Nevertheless, the query still gives me the total sum of all 'history' fields, however I need the sum for only 100 of them.
table1/table2 format:
+-----------+----------+------------+-------------+----------------------+
| i_account | id | account_id | CLD | history |
+-----------+----------+------------+-------------+----------------------+
| 10272 | 46479968 | 0814781293 | 27815212963 | +1x60@1.32d100%=1.32 |
| 6316 | 46480100 | 0813741427 | 27780233136 | +1x60@1.32d100%=1.32 |
| 6316 | 46480107 | 0813741427 | 27780233136 | +1x60@1.32d100%=1.32 |
| 13830 | 46480435 | 0814346396 | 27781356515 | +1x60@1.32d100%=1.32 |
| 13830 | 46480436 | 0814346396 | 27781356515 | +1x60@1.32d100%=1.32 |
+-----------+----------+------------+-------------+----------------------+
Account
SELECT sum(SUBSTRING_INDEX(history,'=',-1)),
cdr.i_account,
cdr.account_id
FROM table1 cdr
WHERE cdr.id IN
(SELECT SUBSTRING_INDEX(group_concat(s.id SEPARATOR ','), ',', 100) id
FROM table2 s
GROUP BY id
)
GROUP BY i_account;