2

what would be possible query of in sql ? if we want separate sum of each different item id's with multiple where clauses like if we have a table persons :

-----------------
id | Amount
1 | 1.0
1 | 2.0
2 | 1.0
2 | 8.0
3 | 1.0
4 | 1.0
4 | 2.0
5 | 1.0
5 | 1.0
6 | 3.0
6 | 1.0
-----------------

i have wrote this but it doesn't works.

select sku , sum(qty_ordered) as total from sales_order_item

where sku = '86-0007-000072,86-0007-000073,44-0002-000137,83-0003-000045,44-0002-000124,83-0003-000046,44-0002-000182,44-0002-000134,44-0002-000132,304113,44-0002-000131,44-0002-000133,320146,44-0002-000217,0001-0001-000002'

1 Answer 1

1

The code: sum(qty_ordered) as total seems just fine.

This one:

where sku = '86-0007-000072,86-0007-000073,44-0002-000137,83-0003-000045,44-0002-000124,83-0003-000046,44-0002-000182,44-0002-000134,44-0002-000132,304113,44-0002-000131,44-0002-000133,320146,44-0002-000217,0001-0001-000002'`

not so much.

You should do

where sku = '86-0007-000072' or sku = '86-0007-000073' or sku = '44-0002-000137'

and so on, for all of the skus that you need.

Shorter alternative for this is

where sku IN ('86-0007-000072','86-0007-000073','44-0002-000137')

Please note that to the IN you are sending an array of values. If you need to set the condition to be sku then you make sure that the values in this array are strings. If you need to set the condition to be, for example, entity_id, then make sure you send integers like

where entity_id IN (1436, 3632, 3453)
1
  • 1
    You are welcome.
    – Marjan
    Sep 22, 2018 at 11:58

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.