嘿伙计们我陷入了凌乱的结构,在编写查询时需要帮助,这是我当前的查询
SQL:
SELECT `name` , trnid, `material` , SUM( quantity )qty , SUM( price ) price, SUM( preturn ) return
FROM `transactions` a
JOIN item_master b ON a.material = b.id
GROUP BY material, trnid
LIMIT 0 , 30
输出:
如果你看到上面有相同材料的2条记录(trnd 1是购买数量& trnd 2是问题数量)我希望这些是具有以下列的单条记录。
名称|材料| purqty | issqty
答案 0 :(得分:4)
我认为你想要条件聚合:
SELECT name, material,
SUM(case when trnid = 1 then quantity else 0 end) as purchaseqty,
SUM(case when trnid = 2 then quantity else 0 end) as issueqty
FROM transactions t JOIN
item_master im
ON t.material = im.id
GROUP BY name, material
LIMIT 0 , 30;