sql - Perform COUNT based on data from two tables -
i still new sql, , performing basic functions challenging, apologize if basic question.
i have several tables 2 being customer , orders. need count of how many orders each customer has placed. trying code , fails:
select customer.firstname || ' ' || customer.lastname customer, count(orders.orderid) orders customer inner join orders on customer.customerid = orders.customerid group customer;
can tell me correct statement?
the query want looks this:
select c.firstname || ' ' || c.lastname customer, count(o.orderid) numorders customer c left join orders o on c.customerid = o.customerid group c.firstname || ' ' || c.lastname;
notes:
- table aliases make query easier write , read.
- oracle doesn't support column aliases in
group by
. need repeat expression (or use subquery or cte). - if want customers, use
left join
include customers, no orders.
edit:
the answer question in comment:
select c.firstname || ' ' || c.lastname customer, count(o.orderid) numorders customer c left join orders o on c.customerid = o.customerid group c.firstname, c.lastname order c.lastname;
Comments
Post a Comment