i have 2 tables
customer
customerid
customername
orders
customerid
orderid
isopen [true/false]
How can i create a view that will contain the following :
customer name , total orders , total orders where isopen column ==true
*Update**
create view CustomerOrders as
select c.id CustomerID,
max(c.customername) CustomerName,
count(o.order_id) CustomerOrders,
SUM(CASE WHEN o.isopen = 1 THEN 1 ELSE 0 END) AS OpenOrders
from customer c
left join orders o on c.id = o.customerid
group by c.id
Thank you all
mark please correct your answer so i can mark you
Try:
(Assumes that you want to include customers with no orders – change the left join to an inner join if you want to include only customers with orders.)