单项求和 sql

Sum Distinct in one sql

我真的很困惑,我试了几天都没有结果,请帮助我,我有table这样的

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 的小尺寸总和)所以结果就像

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

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