0

I'm having difficulties to SUM in mysql with grouping from certain field

+------------+----------+----------------------+
| order_plan | subgroup | requested_amount_usd |
+------------+----------+----------------------+
| 2020-07    | SOFTWARE |                10.00 |
| 2020-07    | SOFTWARE |                15.00 |
| 2020-07    | FLASHDISK|                25.00 |
| 2020-08    | SOFTWARE |                10.00 |
+------------+----------+----------------------+

what i've tried to query (using CI) :

$this->db
->distinct('subgroup','order_plan')
->select_sum('requested_amount_usd','requested_amount')
->from($table);
return $this->db->get()->result_array();

the result was looks like this :

+------------------+
| requested_amount |
+------------------+
|           60.00  |
+------------------+

while I was expecting the result would be like this :

+------------+----------+----------------------+
| order_plan | subgroup | requested_amount_usd |
+------------+----------+----------------------+
| 2020-07    | SOFTWARE |                25.00 |
| 2020-07    | FLASHDISK|                25.00 |
| 2020-08    | SOFTWARE |                10.00 |
+------------+----------+----------------------+

How can I achieve that ?

1 Answer 1

1

SELECT order_plan
     , subgroup
     , SUM(requested_amount_usd) AS requested_amount_usd
  FROM {table} 
 group 
    by subgroup
     , order_plan

Always remember you need to GROUP all the other columns while using SUM(),AVG(),COUNT()

Sign up to request clarification or add additional context in comments.

3 Comments

i tried this one then i got this error --> ERROR 1241 (21000): Operand should contain 1 column(s), what should i do ?
Try Removing brackets from group by, If it works close the question by marking it answered.
@owf there's no multi-column operand here. Perhaps you have some kind of trigger or something

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.