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

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -