mysql - using joins together with aggregates, and retrieving rows when no aggregate exists -
the following query on mysql tables returns rows purchaseorder table have corresponding entries in deliveryorder table. how construct query rows purchaseorder table if no corresponding rows exist in deliveryorder table? if users want see sql table create statements, can post those, i'm not posting makes question big.
select `purchaseorder`.`id` `po_id`, `purchaseorder`.`order_quantity` `po_order_quantity`, `purchaseorder`.`applicable_approved_unit_rate` `po_unit_rate`, `purchaseorder`.`applicable_sales_tax_rate` `po_tax_rate`, `purchaseorder`.`order_date` `po_order_date`, `purchaseorder`.`remarks` `po_remarks`, `purchaseorder`.`is_open` `po_is_open`, `purchaseorder`.`is_active` `po_is_active`, `purchaseorder`.`approved_rate_id` `po_app_rate_id`, `supplier`.`name` `sup_name`, sum(`deliveryorder`.`quantity`) `total_ordered` `purchaseorder` left join `deliveryorder` on (`deliveryorder`.`purchase_order_id` = `purchaseorder`.`id`) inner join `approvedrate` on (`purchaseorder`.`approved_rate_id` = `approvedrate`.`id`) inner join `supplier` on (`approvedrate`.`supplier_id` = `supplier`.`id`) ( `purchaseorder`.`is_active` = 1 , `purchaseorder`.`is_open` = 1 , `deliveryorder`.`is_active` = 1 , `approvedrate`.`material_id` = 2 ) having `purchaseorder`.`order_quantity` >= `total_ordered` + 1
you have aggregating function no group clause, wierd, anyway - this? oops - edited...
select po.id po_id , po.order_quantity po_order_quantity , po.applicable_approved_unit_rate po_unit_rate , po.applicable_sales_tax_rate po_tax_rate , po.order_date po_order_date , po.remarks po_remarks , po.is_open po_is_open , po.is_active po_is_active , po.approved_rate_id po_app_rate_id , s.name sup_name , sum(do.quantity) total_ordered purchaseorder po left join deliveryorder on do.purchase_order_id = po. , do.is_active = 1 left join approvedrate ar on ar.id = po.approved_rate_id , ar.material_id = 2 left join supplier s on s.id = ar.supplier_id po.is_active = 1 , po.is_open = 1 having po.order_quantity >= total_ordered + 1
Comments
Post a Comment