2014-04-28 48 views
0

我在SQLite3中有這個表。如何創建一個提供多個sum()總數的SQL語句?

Name | Qty | Price | Category 
----------------------------- 
Z | 1 | $25 | A 
Y | 2 | $50 | B 
X | 3 | $100 | A 
W | 1 | $6 | C 
V | 2 | $15 | B 
U | 1 | $26 | A 

我怎樣才能得到每個類別和所有類別的總(數量*價格)與一個SQL語句的總(數量*價格)?這是我正在尋找的輸出:

A  - $351 
B  - $130 
C  - $6 
Total - $487 

這可能嗎?

回答

1
select category, sum(qty * price) 
from your_table 
group by category 

union all 

select 'Total' as category, sum(qty * price) 
from your_table 
+0

謝謝,那真棒! – Albion