SQL, the table does not return value

Hi Expert,

If @Dealershipname = A which has 4 testdrive , the query will return the table with values.
If @Dealershipname = B which has 0 testdrive, the query will not return the values, only the row header.

Do you have any suggestions, thanks.


SELECT  dt.[dealerid]
      ,dt.[name]
       ,dt.[Alternatedealmtd]
      ,dt.[ManualDealmtd]
       ,dt.[wtd]
      ,dt.[mtd]
      ,dt.[lastmonth]
      ,dt.lastmonthtotal              
        ,dt.[trending]
        ,isnull(COUNT(t.dealid),0) as testdrive
FROM [MMD_Feed].[dbo].[DART] AS dt
INNER JOIN Mmd_feed..dealview AS dv
      on dt.dealerid=dv.dealerid
INNER JOIN Mmd_feed..deal d
      on dv.dealid=d.dealid
LEFT OUTER JOIN [MakeMyDeal_com].[dbo].[TestDrive] t
      on t.dealid=d.dealid
where name in (@DealershipName)
and t.dealid in (select t.dealid from  [MakeMyDeal_com].[dbo].[TestDrive]
where cast(d.submittedtimestamp as date)>=DateAdd("d",-(Day(getdate())-1), getdate()) and cast(d.submittedtimestamp as date)<getdate())
group by dt.[dealerid]
      ,[name]
       ,[Alternatedealmtd]
      ,[ManualDealmtd]
       ,[wtd]
      ,[mtd]
      ,[lastmonth]
      ,lastmonthtotal
      ,[trending]
tanj1035Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Vitor MontalvãoMSSQL Senior EngineerCommented:
Since you are using LEFT JOIN (even I don't know where name is from which table) you can add:
(...) where (name in (@DealershipName) or name is null)
and (...)
Brian CroweDatabase AdministratorCommented:
Unclear on what your goal is.

If @Dealershipname = A which has 4 testdrive , the query will return the table with values.
If @Dealershipname = B which has 0 testdrive, the query will not return the values, only the row header.

Is this your goal or your current problem?  If the former then what are you getting now, if latter then what are you trying to achieve?
Deepak ChauhanSQL Server DBACommented:
this is because you are using AND condition here. Change AND to OR and see the dirrerence.

where name in (@DealershipName)
 and t.dealid in (select t.dealid from  [MakeMyDeal_com].[dbo].[TestDrive]
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

tanj1035Author Commented:
hi Brain, sorry, this is the problem.
I want to get the table return value even dealershipename B has 0 test drive.
tanj1035Author Commented:
Vitor and Deepak, thanks for your replies. Sorry, the queries do not work. It still cannot return value for the dealershipname B
Vitor MontalvãoMSSQL Senior EngineerCommented:
Please provide some sample data so we can test before providing you an assertive answer.
tanj1035Author Commented:
Sure, how you want me to provide the sample data? Thanks.
Vitor MontalvãoMSSQL Senior EngineerCommented:
Few sample rows for each table. If the tables contains sensitive data don't forget to mask them.
Tables:
[MMD_Feed].[dbo].[DART]
Mmd_feed..dealview
Mmd_feed..deal
[MakeMyDeal_com].[dbo].[TestDrive]
tanj1035Author Commented:
Vitor, please find attached excel, thx.
Sample.xlsx
Vitor MontalvãoMSSQL Senior EngineerCommented:
I saw another thing in your query. What's the logic for this part?
and t.dealid in (select t.dealid
            from  [MakeMyDeal_com].[dbo].[TestDrive]
            where cast(d.submittedtimestamp as date)>=DateAdd("d",-(Day(getdate())-1), getdate()) and cast(d.submittedtimestamp as date)<getdate())


You are selecting from a table but filtering by the columns of another table?

Check if with this change the results are different from before:
SELECT  dt.[dealerid]
       ,dt.[name]
       ,dt.[Alternatedealmtd]
       ,dt.[ManualDealmtd]
       ,dt.[wtd]
       ,dt.[mtd]
       ,dt.[lastmonth]
       ,dt.lastmonthtotal              
       ,dt.[trending]
       ,isnull(COUNT(t.dealid),0) as testdrive
FROM [MMD_Feed].[dbo].[DART] AS dt
	INNER JOIN Mmd_feed..dealview AS dv on dt.dealerid=dv.dealerid
	INNER JOIN Mmd_feed..deal d on dv.dealid=d.dealid 
	LEFT OUTER JOIN [MakeMyDeal_com].[dbo].[TestDrive] t on t.dealid=d.dealid 
where name in (@DealershipName)
and cast(d.submittedtimestamp as date)>=DateAdd("d",-(Day(getdate())-1), getdate()) and cast(d.submittedtimestamp as date)<getdate()
group by dt.[dealerid]
       ,[name]
        ,[Alternatedealmtd]
       ,[ManualDealmtd]
        ,[wtd]
       ,[mtd]
       ,[lastmonth]
       ,lastmonthtotal
       ,[trending] 

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tanj1035Author Commented:
Thanks, Vitor. Yours worked out!!

I was originally trying to get the "isnull(COUNT(t.dealid),0) as testdrive" from "t.dealid in (select t.dealid from  [MakeMyDeal_com].[dbo].[TestDrive] where cast(d.submittedtimestamp as date)>=DateAdd("d",-(Day(getdate())-1), getdate()) and cast(d.submittedtimestamp as date)<getdate())

It means the count has to be in [testdrive] from the date range from another table [deal].

Yours worked successfully without "t.dealid in (select t.dealid from  [MakeMyDeal_com].[dbo].[TestDrive]" . So I do not need to especially to indicate dealid in [testdrive] in the query?
Vitor MontalvãoMSSQL Senior EngineerCommented:
No, you don't because you already did in the JOIN clause:
LEFT OUTER JOIN [MakeMyDeal_com].[dbo].[TestDrive] t on t.dealid=d.dealid
tanj1035Author Commented:
good point!!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.