I tried referring to other questions; I've inferred that sub-queries cannot be used on aggregate functions, but I cannot solve this use case.
Tables:
1. CustomerInfo(c_id,name)
2. ProductInfo(p_id,price)
3. ModelInfo(p_id,m_id,name)
4. PurchaseRecords(c_id,m_id,quantity)
Required output:
List of customer names, with total amount purchased by each customer.
My flow of thought is that:
Link PurchaseRecords with ModelInfo to get p_id,
ModelInfo with ProductInfo to get the price,
Multiply price returned by quantity in PurchaseRecords for every specific customer,
which requires me to link CustomerInfo in the end to get the name.
I'm using Postgres. I could write a program for that in Java, but I find it hard to do it with SQL. So, what is the correct query here? Any pointers on how to think problems out are appreciated!