1
a.id  c_id  p_type  paid     date
1      6    FLOUR   100.00  2015-06-22
2      7    OIL     50.00   2015-06-21
3      6    FLOUR   242.00  2015-06-24
4      6    FLOUR   392.00  2015-06-26
5      7    OIL     200.00  2015-07-29
6      7    OIL     300.00  2015-07-25

i have been trying to select only the sum of values from distinct tables, i tried

SELECT customer.first_name, customer.last_name, credit.quantity, 
       credit.date_supplied, credit.unit_price, credit.p_type,
       ROUND(SUM(account.amount_paid))
AS amount_paid, account.date_paid FROM credit, customer 
LEFT OUTER JOIN account 
ON account.c_id = customer.c_id ;

I want to display the sum total of columns with product type flour and then that of oil but i only get a sum of the whole amount in the table. I would appreciate the help.

Shivam
  • 457
  • 1
  • 6
  • 15
Peter
  • 21
  • 1
  • 7

2 Answers2

0

You can use group by type and then use sum on the group.

Ramzy
  • 6,948
  • 6
  • 18
  • 30
0

Group by will group things and then perform the operations on the given group. So for this, it will evaluate to split it into two groups, oil and flour, and then perform the sum on each.

SELECT 
  customer.first_name
, customer.last_name
, credit.quantity
, credit.date_supplied
, credit.unit_price
, credit.p_type
, ROUND(SUM(account.amount_paid)) AS amount_paid
, account.date_paid 
FROM credit
LEFT JOIN customer on credit.??? = customer.??? 
LEFT JOIN account ON account.c_id = customer.c_id 
GROUP BY credit.p_type;
Cohan
  • 4,384
  • 2
  • 22
  • 40