SELECT
	*
FROM
	(
		SELECT
			`po_detail_id`,
			`po_id`,
			`code`,
			`qty`
		FROM
			`po_detail`
			LEFT JOIN
			(
				SELECT
					`po_detail_id`,
					SUM(`qty`) AS `qty`
				FROM
					`po_queue`
				WHERE
					`commit_id` IS NOT NULL
				GROUP BY
					`po_detail_id`
			) po_added
			USING (`po_detail_id`)
	) po_detail_qa
    LEFT JOIN
	(
		SELECT
			`company_id`,
			`code`
		FROM
			`stock_history`
			JOIN
			(
				SELECT
					`company_id`,
					MAX(`date`) AS `date`
				FROM
					`stock_history`
				GROUP BY
					`company_id`
			) stock_latest
            USING (`company_id`,`date`)
	) stock
    USING (`code`)
WHERE
	po_id = 9936
ORDER BY
	po_detail_id