select c.`id`, `notPendingOrders`.`quantity` from `Customer` c left join lateral ( select count(o.id) as `quantity` from `Order` o join `OrderDetail` od on od.`idOrder` = o.`id` join `OrderDetailStatus` ods on ods.`id` = od.`idOrderDetailStatus` where o.`idCustomer` = c.`id` and ods.`name` != 'PENDING' ) `notPendingOrders` on true where coalesce(`notPendingOrders`.`quantity`, 0) > 0 ;