2015-07-01 97 views
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 

我一直在努力,只選擇不同的表值的總和,我想如何選擇不同欄的總和不添加整個列

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 ; 

我想要顯示的總和有產品類型的麪粉和油然後我只能得到表中所有金額的總和。我將不勝感激。

+0

http://stackoverflow.com/questions/11782292/sumdistinct-based-on-other-columns – Shivam

回答

0

您可以按類型使用組,然後在組上使用總和。

0

小組將按事件分組,然後對給定組執行操作。因此,爲此,它將評估將其分爲兩組,即油和麪粉,然後分別執行總和。

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; 
+0

我想,它仍然不能正常工作,它給整個帳戶支付列的總和 – Peter

+0

正在嘗試更加明確地表示您的信用和客戶加入。看看是否有幫助。 –

+0

謝謝你的幫助。 – Peter