我有以下mysql表:
item code | warehouse | qty
---------------------------
itm001 | abc |10
itm002 | xyz |20
itm003 | pqr |20
itm004 | pqr |15
itm001 | abc |60
itm002 | xyz |10
itm004 | tqr |20
itm003 | www |20
itm001 | ppp |15
当项目代码和仓库重复其总数时,我想要的总和: 例如: itm001 abc被重复两次,它们的总和显示为itm001 = 70 同样的方式为itm002和显示总和itm002 = 30.
我不想要静态where子句(其中item code ='itm001'和warehouse ='abc'或item code ='itm002'和warehouse ='xyz')。
答案 0 :(得分:7)
您应该GROUP BY
使用HAVING
:
SELECT itemcode,warehouse,SUM(qty) FROM yourtable
GROUP BY itemcode,warehouse
HAVING COUNT(*) > 1;
HAVING
过滤掉只在仓库中存在一次的项目。
答案 1 :(得分:2)
试试这个:
SELECT itemcode,warehouse,SUM(qty) FROM table1 GROUP BY warehouse,itemcode;
答案 2 :(得分:1)
使用此:
SELECT item_code, warehouse,sum(qty)
FROM your_table
GROUP BY item_code, warehouse
答案 3 :(得分:1)
您可以在分组上使用havin子句。
select item_code, warehous, sum(qty), count(*)
group by item_code, warehouse
having count(*) > 1
答案 4 :(得分:0)
如果我理解正确,你可以使用:
SELECT item_code, warehouse , SUM(qty) FROM yourTable WHERE (...) GROUP BY item_code, warehouse;