Question

Assume i have two table (attributes are in parentheses).

  1. products (pid, releasedate, productname, price, description)
  2. sells (sellid, sellproductid, selluserid, selldate, selltype)

From this the output will result to this problem: "Show all products with each total sell"
I wrote this SQL query with no luck:

select pid, productname, price, count(pid) from products p, sells s having p.pid = s.sellproductid group by pid;

Sound like the group by is not working, with sql minor knowledge i can't understand how to fix this...any idea?

Was it helpful?

Solution

You should include all the non-aggregated fields in the group by expression. Try This:

select pid, productname, price, count(1) as ProductsSold,count(1)*price as Income
from products p, sells s 
where p.pid = s.sellproductid 
group by pid, productname, price

OTHER TIPS

try

select pid, producname,price from products p inner join sells s on p.pid=s.sellproductid group by pid.

becouse function count() returned one row.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top