SQL, when count(*) is 0 , the table did not return value

hi Expert,

Please see my query below. If the dealershipname A has test drive, the table will return all columns with values. If the dalershipname B does not have test drive (which means count(*)=0), the table  return the header only , even not value in the column, like [trending], which was supposed to return value.

Can you help me on it. Thanks!



declare @DealershipName varchar(250)
set @DealershipName ='A'

SELECT  dt.[dealerid]
      ,[name]
       ,[Alternatedealmtd]
      ,[ManualDealmtd]
       ,[wtd]
      ,[mtd]
      ,[lastmonth]
      ,lastmonthtotal              
        ,[trending]
        ,case when isnull (count(*),0)=0 then 0 else count(*) end as testdrive
  FROM [MMD_Feed].[dbo].[DART] dt
  inner join Mmd_feed..dealview dv on dt.dealerid=dv.dealerid
  inner join Mmd_feed..deal d on dv.dealid=d.dealid
  inner join [MakeMyDeal_com].[dbo].[TestDrive] t on t.dealid=d.dealid
where name in (@DealershipName)
group by dt.[dealerid]
      ,[name]
       ,[Alternatedealmtd]
      ,[ManualDealmtd]
       ,[wtd]
      ,[mtd]
      ,[lastmonth]
      ,lastmonthtotal
      ,[trending]
tanj1035Asked:
Who is Participating?
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.

Brian CroweDatabase AdministratorCommented:
You need to OUTER JOIN from a list of dealers if you want a COUNT when there are 0 rows.

You will also want to do the COUNT on a column in the joined table since COUNT won't count NULL values.  In other words COUNT(*) will return a value of 1 for dealers that have no records so you should use COUNT(dealid)
0
Brian CroweDatabase AdministratorCommented:
try this...

SELECT  dt.[dealerid]
      ,[name]
       ,[Alternatedealmtd]
      ,[ManualDealmtd]
       ,[wtd]
      ,[mtd]
      ,[lastmonth]
      ,lastmonthtotal              
        ,[trending]
        ,COUNT(t.dealid) 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)
group by dt.[dealerid]
      ,[name]
       ,[Alternatedealmtd]
      ,[ManualDealmtd]
       ,[wtd]
      ,[mtd]
      ,[lastmonth]
      ,lastmonthtotal
      ,[trending]

Open in new window

0

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
Brian CroweDatabase AdministratorCommented:
Also, I don't know under what context this query is run but it is susceptible to SQL injection as written.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

tanj1035Author Commented:
You are totally right, Brian!! Thanks for sharing!
0
tanj1035Author Commented:
Hi Brian, maybe this is a stupid question,

Can you give me more details of left out join from the query. What if I use left join?

Thx,
0
Brian CroweDatabase AdministratorCommented:
LEFT JOIN and LEFT OUTER JOIN are exactly the same thing.  It's just a syntax difference.  Just like you can use JOIN or INNER JOIN interchangeably.
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.