简体   繁体   中英

Display 2 columns from one table having max count in column 3 and display computed sum of values from another table

I've a customer table and purchases table, need to show cname, cid with max(customer_visits) from customer table and sum of total_purchases by customer in purchases table. I'm doing something like this

select p.cid, c.cname, sum(p.total_price)
from customers c where exists
(select max(visits_made) from customers having visits_made=max(visits_made)
and cid=p.cid)
inner join purchases p on p.cid=c.cid
group by p.cid,c.cname

and

select p.cid, c.cname, sum(p.total_price)
(select max(visits_made) from customers c where c.cid=p.cid) 
from purchases p
inner join customers c on c.cid=p.cid
group by p.cid,c.cname

What's going wrong with these queries?

Found the solution, had to include where clause after inner join :D

I think this is just an aggregation query:

select p.cid, c.cname, sum(p.total_price) as total_price,
       max(visits_made) as visits_made
from purchases p inner join
     customers c
     on c.cid = p.cid
group by p.cid, c.cname;

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