Domanda

If I run 2 queries separately I get results like this..

 select A.ACTNUMST, sum(B.EXTDCOST) as [IV Total]
 from GL00105 A
 INNER JOIN SEE30303 B on A.ACTINDX = B.IVIVINDX
 group by A.ACTNUMST

 Results -
 Account No      IV Total
 2101-00-137     2033.60
 4101-00-137     83765.86
 6101-00-137     301984.23

Second Query

 select A.ACTNUMST as [Account No], SUM(C.PERDBLNC) as [GL Total]
 from GL00105 A
 LEFT JOIN GL10110 C on A.ACTINDX = C.ACTINDX
 group by A.ACTNUMST

 Results -
 Account No      GL Total
 2101-00-137     2033.60
 4101-00-137     83765.86
 6101-00-137     302656.23

I want to be able to join both results together to compare but I believe it is repeating the sum for each line in the GL total and then summing it again, it comes out with large numbers like -

 select A.ACTNUMST as [Account No], sum(B.EXTDCOST) as [IV Total], SUM(C.PERDBLNC) as [GL Total]
 from GL00105 A
 INNER JOIN SEE30303 B on A.ACTINDX = B.IVIVINDX
 LEFT JOIN GL10110 C on A.ACTINDX = C.ACTINDX
 group by A.ACTNUMST

 Results -
 Account No      IV Total        GL Total
 2101-00-137     2033.60         14235.20
 4101-00-137     83765.86        116350696.20
 6101-00-137     301984.23       1612897825.84

When it should be

 Results -
 Account No      IV Total        GL Total
 2101-00-137     2033.60         2033.60
 4101-00-137     83765.86        83765.86
 6101-00-137     301984.23       302656.23

Please advise how to use the sum function to get the correct results.

È stato utile?

Soluzione

Do you want something like this? Please check and comment if this is not what is expected.

Also please rectify any syntax errors.

SELECT T1.[Account No], T1.[IV Total], T2.[GL Total] FROM
(
    select A.ACTNUMST as [Account No], sum(B.EXTDCOST) as [IV Total]
    from GL00105 A
    INNER JOIN SEE30303 B on A.ACTINDX = B.IVIVINDX
    group by A.ACTNUMST
) T1
INNER JOIN
(
    select A.ACTNUMST as [Account No], SUM(C.PERDBLNC) as [GL Total]
    from GL00105 A
    LEFT JOIN GL10110 C on A.ACTINDX = C.ACTINDX
    group by A.ACTNUMST
) T2
ON T1.[Account No] = T2.[Account No]

Hope this helps

Altri suggerimenti

this can be done with the multiple cte also

 with IVTotal as
 (
     select A.ACTNUMST, sum(B.EXTDCOST) as [IV Total]
     from   GL00105 A
     INNER  JOIN SEE30303 B 
     on     A.ACTINDX = B.IVIVINDX
     group  by A.ACTNUMST
 ),  
 GLTotal as 
 (
     select A.ACTNUMST as [Account No], SUM(C.PERDBLNC) as [GL Total]
     from   GL00105 A
     LEFT   JOIN GL10110 C 
     on     A.ACTINDX = C.ACTINDX
     group  by A.ACTNUMST
 )
 select a.*,b.[GL Total] 
 from   IVTotal a ,GLTotal b
 where  a.ACTNUMST =  b.aCTNUMST
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top