这是我的疑问:
SELECT TRANS_MSTR.branch_id
FROM TRANS_MSTR, BRANCH_MSTR
WHERE BRANCH_MSTR.branch_type_desc = 'ATM ONLY'
And ( SELECT SUM(TRANS_MSTR.trans_amount)
FROM TRANS_MSTR
WHERE TRANS_MSTR.trans_yyyymm = '201511'
GROUP BY TRANS_MSTR.branch_id) < 100;
&#34; ORA-01427:单行子查询返回多行&#34;
是我得到的错误。我真的不知道这甚至意味着什么?我的整体查询的目标是返回分支ID和总和,其中sum小于100.
当我尝试修复它时:
SELECT TRANS_MSTR.branch_id,
(SELECT SUM(TRANS_MSTR.trans_amount)
FROM TRANS_MSTR
WHERE TRANS_MSTR.trans_yyyymm = '201511'
GROUP BY TRANS_MSTR.branch_id) as TransAmt
FROM TRANS_MSTR, BRANCH_MSTR
WHERE BRANCH_MSTR.branch_type_desc = 'ATM ONLY'
And TransAmt < 100;
它告诉我TRANSAMT
是无效的标识符。但我以为你可以别名子查询?
答案 0 :(得分:2)
我认为你想要一个相关的子查询:
SELECT b.branch_id
FROM BRANCH_MSTR b
WHERE b.branch_type_desc = 'ATM ONLY' and
(SELECT SUM(t.trans_amount)
FROM TRANS_MSTR t
WHERE t.trans_yyyymm = '201511' AND
t.branch_id = b.branch_id
) < 100;
这不是我编写查询的方式。但是你已经开始使用子查询而不是JOIN
/ GROUP BY
,这将是更传统的方法。
答案 1 :(得分:2)
请试试这个:
SELECT BRANCH_MSTR.branch_id, SUM(TRANS_MSTR.trans_amount)
AS TransAmt FROM BRANCH_MSTR b
INNER JOIN BRANCH_MSTR
ON TRANS_MSTR.branch_id = BRANCH_MSTR.branch_id
WHERE BRANCH_MSTR.branch_type_desc = 'ATM ONLY'
AND TRANS_MSTR.trans_amount < 100
AND TRANS_MSTR.trans_yyyymm = '201511'
GROUP BY TRANS_MSTR.branch_id;