¿Por que la suma es erronea? tengo el siguiente Script de SQL SERVER
DECLARE
@UserUID INT,
@CharID INT = 1
CREATE table #temp (CharID INT, ItemID INT, Count INT);
CREATE table #temp2 (UserUID INT,CharID INT, ItemID INT, Count INT);
INSERT INTO #temp(CharID, ItemID ,Count)VALUES
(1,1001,15),
(1,1001,5),
(1,1001,5),
(1,1001,5),
(31,1001,5),
(31,1001,5),
(4,1001,5),
(2,1001,5);
INSERT INTO #temp2(UserUID, CharID, ItemID ,Count)VALUES
(6,1,1001,20),
(6,1,1001,5),
(6,1,1001,5),
(6,1,1001,5),
(6,1,1001,5),
(66,31,1001,5),
(66,31,1001,5),
(8,2,1001,5);
SET @UserUID =(SELECT TOP 1 UserUID FROM #temp2 WHERE CharID = @CharID)
SELECT
SUM(a.Count)AS COl_A,
SUM(b.Count)AS Col_B
,sum(a.Count) + SUM(b.Count)as Col_C
FROM #Temp a
INNER JOIN #Temp2 b
ON a.CharID = b.CharID
WHERE a.CharID = @CharID
AND b.UserUID =@UserUID
DROP TABLE #Temp
DROP TABLE #Temp2
me da como resultado
+-------+-------+-------+
| Col_A | Col_B | Col_C |
+-------+-------+-------+
| 150 | 160 | 310 |
+-------+-------+-------+
Pero deberia ser:
+-------+-------+-------+
| Col_A | Col_B | Col_C |
+-------+-------+-------+
| 30 | 40 | 70 |
+-------+-------+-------+