我想在mysql中加上Amount和Count行数

时间:2016-03-09 09:58:52

标签: mysql

我有一张这样的表

id  productName amount
1      Abc       10
2      xyz       20
3      Abc       10
4      Abc       10
5      Abc       10
6      Abc       10
7      xyz       20
8      xyz       20
9      xyz       20
10     xyz       20

我想在mysql中对每个产品的Amount和Count行数求和。输出如下

productName noQty   totalAmount
 ABC         5        50
 xyz         5        100

3 个答案:

答案 0 :(得分:0)

select product_name,sum(amount)as totalamount,count(productName)as noQty
from table_name 
group by productname

答案 1 :(得分:0)

通过使用计数,您可以从表中获取COUNT并使用SUM获得所有相同产品名称的总和。

SELECT productName, 
count(productName) AS noQty, 
SUM(amount) AS totalAmount
FROM tableName 
GROUP BY productName

答案 2 :(得分:0)

db2 "select cast(lpad(123422,10,' ') as char(10)) as testme from sysibm.sysdummy1"

TESTME    
----------
    123422

  1 record(s) selected.