sqlite - Using sql to sum with multiple table calls -
i'll down point. have 3 tables structured follows:
orders:
i_id | o_id | quantity -----+--------+---------- 1 | 1 | 5 2 | 2 | 2 1 | 3 | 3 1 | 4 | 3 2 | 5 | 4
orderinfos:
o_id | c_id ------+------------ 1 | 1 2 | 2 3 | 2 4 | 1 5 | 2
customers:
c_id | name_id ----------+---------- 1 | 100001 2 | 100002
then resulting chart be:
name_id | i_id | quantity -----------+----------+---------- 100001 | 1 | 8 100002 | 2 | 6 100002 | 1 | 3
so basically, have summary of (in case, orders) quantity, , each order has customer id , item name associated. resulting chart gives quantity per customer, per item, in descending order customer. first implementation this:
select quantcust.custidname, quantcust.itemid, quantcust.quant (select o.i_id itemid, c.name_id custidname, sum(o.quantity) quant orders o, orderinfos i, customers c o.o_id = i.o_id , i.c_id = c.c_id group o.i_id, i.c_id) quantcust order quantcust.custid, quantcust.quant desc;
which not print correct values.
i think you're close approach, recommend using explicit join syntax, , using aggregate sum (along group by) totals:
select c.name_id, i_id, sum(quant) quant customers c inner join orderinfo oi on c.c_id = oi.c_id inner join orders o on oi.o_id = o.o_id group c.name_id, i_id order c.name_id, quant desc
this works me sample data, giving desired output indicate.
Comments
Post a Comment