Select o.OrderId
from dbo.Order
inner join dbo.Cashmovement cm
on o.OrderId = cm.OrderId
inner join dbo.Orderdetails od
on od.OrderId = o.OrderId
inner join dbo.Product p
on p.Productid = od.ProductId
where o.CustomerId = @Customerid
Group by o.OrderId
Having Sum(cm.Cashmovement) < (od.SoldQuantity * p.SalePrice)