我希望获取按商品ID对其进行分组的交易总额。我能够在Oracle DB中的查询下面获得所需的结果,但是在MYSQL中也不起作用。
以下错误是我得到的:
未知栏' tran.itemid'在' on条款'
SELECT tran.itemid,
(SELECT SUM(tranitm.quandity)
FROM transactions_item tranitm
JOIN transactions tr
ON(tr.id = tranitm.transid and tranitm.itemid = tran.itemid)
where TR.TRANSTYPE = 1) Purchases,
(SELECT SUM(tranitm.quandity)
FROM transactions_item tranitm
JOIN transactions tr
ON(tr.id = tranitm.transid and tranitm.itemid = tran.itemid)
where TR.TRANSTYPE = 2 ) Sales,
(SELECT SUM(tranitm.quandity)
FROM transactions_item tranitm
JOIN transactions tr
ON(tr.id = tranitm.transid and tranitm.itemid = tran.itemid)
where TR.TRANSTYPE = 3 ) PurchasesRtn,
(SELECT SUM(tranitm.quandity)
FROM transactions_item tranitm
JOIN transactions tr
ON(tr.id = tranitm.transid and tranitm.itemid = tran.itemid)
where TR.TRANSTYPE = 4) SalesRtn
FROM transactions_item tran
GROUP BY tran.itemid
架构:
CREATE TABLE transactions
( id INT, transtype INT,fromledger INT,
ledgerid INT,agentid INT, quandity FLOAT,rate FLOAT);
CREATE TABLE transactions_item
(transid INT, itemid INT,quandity FLOAT,rate FLOAT);
INsert into transactions (id, transtype, fromledger, ledgerid, agentid,
quandity, rate) values (1, 1, 1, 1, 1, 100, 1000);
INsert into transactions (id, transtype, fromledger, ledgerid, agentid,
quandity, rate) values (2, 2, 1, 1, 1, 10, 1200);
INsert into transactions (id, transtype, fromledger, ledgerid, agentid,
quandity, rate) values (3, 3, 1, 1, 1, 50, 1500);
INsert into transactions (id, transtype, fromledger, ledgerid, agentid,
quandity, rate) values (4, 4, 1, 1, 1, 120, 1300);
INsert into transactions (id, transtype, fromledger, ledgerid, agentid,
quandity, rate) values (5, 1, 1, 1, 1, 10, 1100);
INsert into transactions (id, transtype, fromledger, ledgerid, agentid,
quandity, rate) values (6, 2, 1, 1, 1, 12, 1400);
INsert into transactions (id, transtype, fromledger, ledgerid, agentid,
quandity, rate) values (7, 3, 1, 1, 1, 130, 1550);
INsert into transactions (id, transtype, fromledger, ledgerid, agentid,
quandity, rate) values (8, 4, 1, 1, 1, 110, 1700);
INsert into transactions_item (transid, itemid,quandity,rate) values (1, 1, 100, 1000);
INsert into transactions_item (transid, itemid,quandity,rate) values (2, 1, 10, 1200);
INsert into transactions_item (transid, itemid,quandity,rate) values (3, 1, 50, 1500);
INsert into transactions_item (transid, itemid,quandity,rate) values (4, 1, 120, 1300);
INsert into transactions_item (transid, itemid,quandity,rate) values (5, 1, 10, 1100);
INsert into transactions_item (transid, itemid,quandity,rate) values (6, 1, 12, 1400);
INsert into transactions_item (transid, itemid,quandity,rate) values (7, 1, 130, 1550);
INsert into transactions_item (transid, itemid,quandity,rate) values (8, 1, 110, 1700);
INsert into transactions_item (transid, itemid,quandity,rate) values (1, 2, 100, 1000);
INsert into transactions_item (transid, itemid,quandity,rate) values (2, 2, 10, 1200);
INsert into transactions_item (transid, itemid,quandity,rate) values (3, 2, 50, 1500);
INsert into transactions_item (transid, itemid,quandity,rate) values (4, 2, 120, 1300);
INsert into transactions_item (transid, itemid,quandity,rate) values (5, 2, 10, 1100);
INsert into transactions_item (transid, itemid,quandity,rate) values (6, 2, 12, 1400);
INsert into transactions_item (transid, itemid,quandity,rate) values (7, 2, 130, 1550);
INsert into transactions_item (transid, itemid,quandity,rate) values (8, 2, 110, 1700);
答案 0 :(得分:1)
子查询中的ON
子句应该用于关联要连接的表。不属于联接的条件应该在WHERE
。
但是,除了所有相关的子查询之外,还有更好的方法。
SELECT tran.itemid,
SUM(IF(tr.transtype = 1, tran.quandity, 0)) AS Purchases,
SUM(IF(tr.transtype = 2, tran.quandity, 0)) AS Sales,
SUM(IF(tr.transtype = 3, tran.quandity, 0)) AS PurchasesRtn,
SUM(IF(tr.transtype = 4, tran.quandity, 0)) AS SalesRtn
FROM transactions_item AS tran
JOIN transactions AS tr ON tran.transid = tr.id
GROUP BY tran.itemid
请参阅need to return two sets of data with two different where clauses