答案 0 :(得分:0)
这应该可以解决问题:
SELECT
pid
,A.BeReID
,B.Amount
FROM your_table A
INNER JOIN (
SELECT
BeReID
,SUM(Amount) AS Amount
FROM your_table
GROUP BY BeReID
HAVING COUNT(DISTINCT ReID) >= 4
) B
ON A.BeReID = B.BeReID
答案 1 :(得分:0)
这是:
SELECT t.PID, t.BeReID, t2.SUM
FROM t
JOIN
(SELECT PID, BeReID, SUM(Amount) as SUM
FROM t
GROUP BY BeReID
HAVING COUNT(DISTINCT ReID) >= 4
) as t2
ON t.BeReID = t2.BeReID;
http://sqlfiddle.com/#!9/811481/11
使用第二个选择,您可以通过BeReID获得总和,并且通过连接可以将此总和设置为所有行。 有一部分是为了“具有与BeReID列ID不同的ReID计数> = 4”条件