SQL, How to make "sum" work on the distinct value?

Hi Experts,

I want "sum" works on the distinct dealid. But the query I wrote is still calculate all dealid, not the distinct one. Can you help me on it. Thanks.

select 
sum (case when vehiclestatus='New' then 1 else 0 end ) as Newtestdrive,
sum (case when vehiclestatus='Used' then 1 else 0 end ) as Usedtestdrive
from [MakeMyDeal_com].[dbo].[TestDrive] t inner join Mmd_feed..deal d on t.dealid=d.dealid 
inner join Mmd_feed..dealer de on d.dealerid=de.dealerid
where cast(createdon as date)>=@StartDate and cast(createdon as date)<=@EndDate
and t.dealid in (select distinct dealid from [MakeMyDeal_com].[dbo].[TestDrive] )

Open in new window

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.

ste5anSenior DeveloperCommented:
Vitor MontalvãoMSSQL Senior EngineerCommented:
vehiclestatus belongs to which table?
tanj1035Author Commented:
Thanks for your reply. It is supposed to return 20 for newtestdrive and 81 for usedtestdrive. But the query I have modified returned 1 and 1 respectively. Did I modify it correctly?


select 
sum (distinct case when vehiclestatus='New' then 1 else 0 end ) as Newtestdrive,
sum (distinct case when vehiclestatus='Used' then 1 else 0 end )as Usedtestdrive

from [MakeMyDeal_com].[dbo].[TestDrive] t inner join Mmd_feed..deal d on t.dealid=d.dealid 
inner join Mmd_feed..dealer de on d.dealerid=de.dealerid
where cast(createdon as date)>=@StartDate and cast(createdon as date)<=@EndDate

Open in new window

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:
vechiclestatus belongs to [deal]
select 
sum (distinct case when vehiclestatus='New' then 1 else 0 end ) as Newtestdrive,
sum (distinct case when vehiclestatus='Used' then 1 else 0 end )as Usedtestdrive

from [MakeMyDeal_com].[dbo].[TestDrive] t inner join Mmd_feed..deal d on t.dealid=d.dealid 
inner join Mmd_feed..dealer de on d.dealerid=de.dealerid
where cast(createdon as date)>=@StartDate and cast(createdon as date)<=@EndDate

Open in new window

Vitor MontalvãoMSSQL Senior EngineerCommented:
Then try this:
select 
	sum (case 
		when d.vehiclestatus='New' then 1 
		else 0 
	end ) as Newtestdrive,
	sum (case 
		when d.vehiclestatus='Used' then 1 
		else 0 
	end ) as Usedtestdrive
from (select distinct dealid from [MakeMyDeal_com].[dbo].[TestDrive]) as t 
	inner join Mmd_feed..deal d on t.dealid=d.dealid 
	inner join Mmd_feed..dealer de on d.dealerid=de.dealerid
where cast(createdon as date)>=@StartDate and cast(createdon as date)<=@EndDate

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:
It works :) Thanks a lot
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.