简体   繁体   中英

Total products ordered by each customer

I have the following tables:

Customer ( custID )

Order ( orderID , custID)

PO ( prodID , orderID, amount)

I would like to select each custID and the total amount of products ordered. I have the following code:

SELECT c.custId (COUNT(po.amount)) totalOrders
FROM Customer c
INNER JOIN "Order" o
ON c.custId = o.custId
INNER JOIN PO po
ON o.orderId=po.orderId;

I am getting the following error:

Cannot find either column "c" or the user-defined function or aggregate "c.custId", or the name is ambiguous.

You need a group by . You don't need the customers table. You want SUM() , not COUNT()`. And your syntax is just off:

SELECT o.custId, SUM(po.amount) as totalOrders
FROM "Order" o INNER JOIN
      PO po
      ON o.orderId=po.orderId
GROUP BY o.custId;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM