Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

difference between these 2 queries

$
0
0

I have the following query that I would like to understand what is happening

create table #tmpclients (id int not null)

insert into #tmpclients values ('1')
insert into #tmpclients values ('2')
insert into #tmpclients values ('3')
insert into #tmpclients values ('4')




create table #tmpdata (id int not null,
						trandate datetime not null,
						tranamount money not null)
						
insert #tmpdata values ('1','2013-07-01',24.54)
insert #tmpdata values ('1','2013-07-01',36.48)
insert #tmpdata values ('2','2013-07-01',45.00)
insert #tmpdata values ('2','2013-07-02',12.00)
insert #tmpdata values ('2','2013-07-02',125.00)
insert #tmpdata values ('3','2013-07-02',15.77)
insert #tmpdata values ('3','2013-07-02',0.77)
insert #tmpdata values ('3','2013-07-03',4.57)
insert #tmpdata values ('4','2013-07-03',16.99)
insert #tmpdata values ('4','2013-07-04',75.49)

select a.id,isnull(sum(b.tranamount),0) as amount
from #tmpclients as a
left outer join #tmpdata as b
on a.id=b.id 

where b.trandate ='2013-07-02'
group by a.id


drop table #tmpclients
drop table #tmpdata

what I would like to happen is that with the left join any id's that don't have a transaction that is on the where clause will show as 0.00 but I only get 2 records back so it feels like the left join is a inner join.

when I change the query to the following

select a.id,isnull(sum(b.tranamount),0) as amount
from #tmpclients as a
left outer join #tmpdata as b
on a.id=b.id and b.trandate ='2013-07-02'

--where b.trandate ='2013-07-02'
group by a.id

I get the correct result but the question is why?

Regards


Viewing all articles
Browse latest Browse all 23857

Trending Articles



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