总结一个sql中的区别

时间:2015-10-19 08:59:26

标签: sql vb.net qsqlquery

我真的很困惑,我正在尝试和搜索几天没有结果,请帮助我,我有这样的表

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

1 个答案:

答案 0 :(得分:3)

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