Link to home
Start Free TrialLog in
Avatar of tanj1035
tanj1035

asked on

SQL, the join did not return value 0

Hi Experts,

Query A returns 139 DealerIDs with their activeday.
Query B only return 38 DealerIDs, as some of 139 DealerID did not have 0 test drive # and their DealerID did not return.
How can I modify QUERY B, to show 139 DealerID and test drive # even the test drive # is 0. Thanks.

Currently, the Query B returns
DealerID   NAME   ACTIVEDAY   TESTDRIVE
BBB              YY             50                  5

I want Query B returns
DealerID   NAME   ACTIVEDAY   TESTDRIVE
AAA              XX             86                  0
BBB              YY             50                  5

QUERY A


select dealerid, dealershipName, count(dpd.date) as activeday
into #dealerwithactiveday
from [MMD_Feed].[dbo].[DealsPerDayHistoricalByType] dpd
where 
dpd.testdrive='Y'
and dealerid in (select dealerid from mmd_feed..dart)
group by dpd.dealerid,dpd.dealershipName

Open in new window

QUERY B

select dealerid, dealershipName, count(dpd.date) as activeday
into #dealerwithactiveday
from [MMD_Feed].[dbo].[DealsPerDayHistoricalByType] dpd
where 
dpd.testdrive='Y'
and dealerid in (select dealerid from mmd_feed..dart)
group by dpd.dealerid,dpd.dealershipName

select 
da.dealerid,
count(distinct d.dealid)as testdrive 
from mmd_feed..dart dt 
right join  #dealerwithactiveday da on da.dealerid=dt.dealerid
inner join Mmd_feed..dealview d on da.dealerid=d.dealerid 
inner join [MakeMyDeal_com].[dbo].[TestDrivelocal] t on t. dealid= d.dealid
where cast(d.submittedtimestamp as date)>=@StartDate and cast(d.submittedtimestamp as date)<=@EndDate
and d.dealstatus='sent'
and d.alternatedeal ='no'
and d.manualdeal='no'
group by da.dealerid

Open in new window

Avatar of Kyle Abrahams, PMP
Kyle Abrahams, PMP
Flag of United States of America image

you would left join instead of inner join.
ASKER CERTIFIED SOLUTION
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial