如何从上表中获得9,300?我只需要添加6500 + 1800 + 1000
这是我当前的查询
SELECT
SUM(e.amount) / (SELECT count(e2.receipt_no)
FROM entries e2
WHERE e2.receipt_no = e.receipt_no) as total,
e.user_id
FROM
entries e
GROUP BY e.receipt_no
结果是
现在我需要获得每user_id
预期输出应为
答案 0 :(得分:1)
根据我的理解,这应该让你想要你想要
SELECT sum(DISTINCT amount) as total, reciept_no FROM entries GROUP BY receipt_no
答案 1 :(得分:1)
尝试这样的事情
SELECT SUM(DISTINCT(amount)) as total, user_id FROM `entries` GROUP BY user_id
答案 2 :(得分:1)
试试这个
SELECT SUM(amount) as total,user_id FROM entries GROUP BY user_id
答案 3 :(得分:1)
首先按userid,receipt_no计算DISTINCT金额组,然后按user_id计算条目组的总和:
SELECT sum(total),userid from (SELECT sum(DISTINCT amount) as total,
userid,receipt_no FROM entries GROUP BY userid,receipt_no) as rgrouped
GROUP BY userid
答案 4 :(得分:0)
你也可以尝试这个
SELECT user_id, SUM(DISTINCT `amount`) FROM `test` group by `user_id`
第1步:为每个用户ID选择不同的金额
101 - 6500,1800,1000
189 - 1019.00
步骤2
101 = 6500+1800+1000 = 9300.00
189 = 1019.00
这将为每个用户ID选择distinct
金额,然后添加所选金额并为您提供相同的结果。