2015-10-19 72 views
0

我真的很困惑,我是想和沒有結果搜索幾天,請您幫幫我,我有表像這樣

barcode pname  psize  quantity orderno 
1  test1  Small   10  15 
1  test1  XSmall  2  16 
1  test1  XSmall  6  17 
1  test1  Large   12  18 
1  test1  xlarge  1  19 
1  test1  Small   17  20 
1  test1  large   9  21 
2  test2  Small   9  22 
2  test2  Small   9  23 

======= 我想每個尺寸總和每個條形碼(例如,我想小尺寸的條碼1的總和),這樣的結果會是這樣總和在一個鮮明的SQL

barcode pname  psize  quantity 
1  test1  Small   27   
1  test1  XSmall  8  
1  test1  Large   21  
1  test1  xlarge  1    
2  test2  Small   18  

我試過如下因素SQL查詢:

SELECT SUM(quantity) AS quantity 
     FROM 
(SELECT DISTINCT barcode, quantity,psize FROM products) 

但結果給我(75)這是小計。 請任何幫助。我正在使用vb.net 2008與SQL 2008

回答

3
SELECT barcode, pname, psize, SUM(quantity) AS quantity 
FROM products 
GROUP BY barcode, pname, psize 
+0

謝謝你這麼多,它終於工作,謝謝,我會接受答案 –