Hope this is a quick one:
I need to generate a single row containing the sum of all values where the ACCOUNT and MONTH are the same, in order to compare values between 2 systems.
However, when I simply select values, I get the correct amount, but when I apply a SUM() to the values, I get a doubling up.
Table structure:
ACCOUNT MONTH PC VALUE
12110 201305 B1000 100
12110 201305 B1000 50
The correct values are returned by:
select
t1.[ACCOUNT] as [ACCOUNT],
t1.[MONTH] as [MONTH],
t1.[PC] as [PC],
t2.[VALUE] as [VALUE]
from Table1 t1
inner join Table2 t2
on t1.ACCOUNT = t2.ACCOUNT
and t1.MONTH = t2.MONTH
and t1.[PC] = t2.[PC]
group by t1.ACCOUNT, t1.MONTH, t1.PC, t2.VALUE
This returns:
ACCOUNT MONTH PC VALUE
12110 201305 B1000 150
However, when I apply the roll-up:
select
t1.[ACCOUNT] as [ACCOUNT],
t1.[MONTH] as [MONTH],
t1.[PC] as [PC],
sum(t2.[VALUE]) as [VALUE]
from Table1 t1
inner join Table2 t2
on t1.ACCOUNT = t2.ACCOUNT
and t1.MONTH = t2.MONTH
and t1.[PC] = t2.[PC]
group by t1.ACCOUNT, t1.MONTH, t1.PC
This returns:
ACCOUNT MONTH VALUE
12110 201305 300
Can anyone please help, as I've been staring at this for over an hour, and can't bend my head around what should be a minor issue?
Thanks.
Craig
* EDIT *
OK, full table structure:
CATEGORY DATASRC CURRENCY MONTH VALUE ACCOUNT PC FLOW
WF PP LC 201305 2588651.6800000000 12110 B1090 MV
WF PP LC 201305 -1288651.6800000000 12110 B1090 MV
WF PP LC 201306 1000000.0000000000 12110 B1090 MV
WF PP LC 201305 -500000.0000000000 12110 B1500 MV
In the above example, you see 2 values for the junction I need (the top 2). The other rows have either a different MONTH (row 3) or a different PC.
What I am doing is to SUM the VALUES wherever the ACCOUNT, PC and MONTH are the same (rows 1 and 2 above). The other columns (bar VALUE) are always the same.
The total of those rows should be 1,300,000.00.
When I SUM them (as shown in the first example), it returns 2,600,000.00
Is this enough to go on?
Thanks again
Craig