我简化了我的查询,所以现在看起来如下:
SELECT allaccounts.*, groups.TOTALGROUPAMOUNT
FROM
(**SELECT FROM DUMMYTABLE WHERE...**) allaccounts,
(SELECT groupID, SUM(memberAmount) totalGroupAmount FROM(**SELECT FROM DUMMYTABLE WHERE...**) group by groupID) groups
WHERE allaccounts.groupID = groups.groupID
SELECT FROM DUMMYTABLE WHERE ...的位置如下:
GROUP|ACCOUNT|BALANCE
A |101 |1000
A |102 |2000
A |103 |3000
B |104 |4000
B |105 |5000
C |106 |6000
根据上面的查询,我希望得到以下结果:
GROUP|ACCOUNT|BALANCE|TOTALGROUPAMOUNT
A |101 |1000 |6000
A |102 |2000 |6000
A |103 |3000 |6000
B |104 |4000 |9000
B |105 |5000 |9000
C |106 |6000 |6000
所以我的问题是可以让分组不使用" SELECT FROM DUMMYTABLE WHERE ... "两次?
感谢您的回答和提前建议!
答案 0 :(得分:2)
使用sum() over()
。
select t.*, sum(balance) over(partition by grp) totalgroupamount
from tablename t