Quantcast
Channel: VBForums - Database Development
Viewing all articles
Browse latest Browse all 2918

Is there a way to sum with left join?

$
0
0
Hi.
I can do this with union but I wanted to ask if it is possible woth a left join
I have 2 tables tblDWTicket and tblDWBookingFee . tblDWTicket will always have a transaction with values tblDWBookingFee somethimes

So doing this:
Code:

  select T.TransNumber, isnull(T.GrossValue,0)
  from tblDWTicket  T
    where  T.SystemCode = 'V'
  and t.TransStatus =  'C'
    and t.TransNumber = 110520

will give me this:
Code:

TransNumber        (No column name)
110520        -8,50
110520        -8,50
110520        -8,50

Doing this:
Code:

        select BF.TransNumber,  IsNull(BF.GrossValue, 0)
        from tblDWBookingFee BF
        where  isnull( BF.TransStatus,'0') in ('C')
  and BF.TransNumber = 110520

Will give me this:

Code:

TransNumber        GrossValue
110520        -1,00
110520        -1,00
110520        -1,00

But trying to add them:
Code:

  select T.TransNumber,T.GrossValue, IsNull(BF.GrossValue, 0)
    from tblDWTicket T
Left join tblDWBookingFee BF on BF.TransNumber = T.TransNumber
 where  T.SystemCode = 'V'
  and t.TransStatus =  'C'
  and  isnull( BF.TransStatus,'0') in ('C')
  and t.TransNumber = 110520

Will give me this:
Code:

TransNumber        GrossValue        GrossValue
110520        -8,50        -1,00
110520        -8,50        -1,00
110520        -8,50        -1,00
110520        -8,50        -1,00
110520        -8,50        -1,00
110520        -8,50        -1,00
110520        -8,50        -1,00
110520        -8,50        -1,00
110520        -8,50        -1,00

Ultimately I would like to sum the values by doing the below but I'm stuck at the above as I would be expecting zeros on the no value lines but I don't get them.

The gross that would be tried:
Code:

  select T.TransNumber,sum((T.GrossValue + IsNull(BF.GrossValue, 0))) as Total
  from tblDWTicket T
  Left join tblDWBookingFee BF on BF.TransNumber = T.TransNumber
  where  T.SystemCode = 'V'
  and t.TransStatus =  'C'
  and  isnull( BF.TransStatus,'0') in ('C')
  and t.TransNumber = 110520
  group by T.TransNumber

That will give me a completely wrong result:

Code:

TransNumber        Total
110520        -85,50

I can union or other mumbo jumbo but can't it be done with a left join? Why do i get values instead of nulls?

Viewing all articles
Browse latest Browse all 2918

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>