I'm struggling to try to have the count of order id on an item_id row, any help is greatly appreciated!
Data
item_id | order_id
1 | Order_1
2 | Order_1
3 | Order_2
4 | Order_3
Desired Result
item_id | order_id | items_in_order
1 | Order_1 | 2
2 | Order_1 | 2
3 | Order_2 | 1
4 | Order_3 | 1
SELECT S.item_id, S.`order_id`, S.order_total, C.cnt as items_in_order,
`order_discount` / C.cnt as item_discount,
`order_total` / C.cnt as item_price
FROM `orders` S
LEFT JOIN (SELECT `item_id`, `order_id`, count(`order_id`) as cnt FROM `supplier_orders` GROUP BY `order_id`)
C ON S.`order_id` = C.`order_id` AND S.id = C.item_id
This would produce this with null values
item_id | order_id | items_in_order | item_discount | item_price
3009117 | 3029511 | 2 | 0 | 25
3009118 | 3029511 | null | null | null
UPDATE, this now seems to work as intended
SELECT S.`item_id`, S.`order_id`, S.order_total, C.cnt as items_in_order,
`order_discount` / C.cnt as item_discount,
`order_total` / C.cnt as item_price
FROM `orders` S
INNER JOIN (SELECT `item_id`, `order_id`, count(`order_id`) as cnt FROM `orders` GROUP BY `order_id`)
C ON S.`order_id` = C.`order_id`
GROUP BY S.`item_id`