我有一个表,我试图将Col A的总和加到所有行的2个小数位(有一个id列ID),满足以下条件:
1)还有另一行具有匹配的Col B
2)它在Col C和Col D
中有一对唯一的值答案 0 :(得分:0)
试试这个:
SELECT ROUND(SUM(ColA), 2) AS ColA_Sum
FROM datatable t
WHERE EXISTS (
SELECT 1
FROM datatable t2
WHERE t2.ID <> t.ID -- Another row must have different id
AND t2.ColB = t.ColB -- Matching Column B
)
AND EXISTS (
SELECT 1
FROM datatable t3
WHERE t3.ColC = t.ColC -- Same Column C
AND t3.ColD = t.ColD -- Same Column D
HAVING COUNT(*) = 1 -- Only 1 row with such values
)
答案 1 :(得分:0)
我认为它可能很简单:
SELECT ROUND(SUM(t.cola), 2) AS cola_sum
FROM t
WHERE cola = colb
AND NOT EXISTS
(SELECT 1
FROM t t2
WHERE t2.colc = t.colc
AND t2.cold = t.cold
GROUP BY colc, cold
HAVING COUNT(*) > 1)