表概述:
2列
i_trans_to 和 i_trans_amnt
i_trans_to 由 ID 组成, i_trans_amnt 由用户存入账户的金额组成
用户只能存入这些金额 300,175,75,40,20(因此 i_trans_amnt 列将仅包含这些值)
IDS 可能有多个条目,因为用户可以多次存款。
我想要一个将选择的查询
1.所有存款总额大于500的用户
2.在所有这些用户中,查询必须说明他做了多少种条目(例如:5 个 300 卢比的条目和 2 个 175 卢比的条目)
我成功地设计了查询,除了用户必须存入超过 rs.500 的约束,这是查询
SELECT DISTINCT i_trans_to,
SUM( i_trans_amnt ),
(SELECT COUNT(*)
FROM transac
WHERE i_trans_amnt=300 AND transac.i_trans_to=current.i_trans_to) AS level1,
(SELECT COUNT(*)
FROM transac
WHERE i_trans_amnt=175 AND transac.i_trans_to=current.i_trans_to) AS level2,
(SELECT COUNT(*)
FROM transac
WHERE i_trans_amnt=75 AND transac.i_trans_to=current.i_trans_to) AS level3,
(SELECT COUNT(*)
FROM transac
WHERE i_trans_amnt=40 AND transac.i_trans_to=current.i_trans_to) AS level4,
(SELECT COUNT(*)
FROM transac
WHERE i_trans_amnt=40 AND transac.i_trans_to=current.i_trans_to) AS level5,
(SELECT COUNT(*)
FROM transac
WHERE i_trans_amnt=20 AND transac.i_trans_to=current.i_trans_to) AS level6
FROM transac as current
WHERE SUM( current.i_trans_amnt )>500
GROUP BY i_trans_to
我尝试在查询末尾添加一个 where 子句,其中我写了 SUM(i_trans_amnt)> 500 但这给了我一个错误。
有什么建议么?