SQL, how to sum up the column when the value >=5

Hi Experts, I want the expected result to sum up values when "days before test drive"  >=5. How can I modify my query? Thanks.


select count (dealID) as RequestedTestDrive,daysbeforetestdrive
from 
(select distinct d.dealid, datediff(day, max(cast(t.createdon as date)), max(cast (t.appointmentlocaltime as date))) as daysbeforetestdrive
from [MakeMyDeal_com].[dbo].[TestDrivelocal] t
inner join Mmd_feed..dealview d on t.dealid=d.dealid 
inner join Mmd_feed..dart dt on dt.dealerid=d.dealerid
where cast(t.createdon as date)>=@StartDate and cast(t.createdon as date)<=@EndDate
and d.dealstatus='sent'
and d.alternatedeal ='no'
and d.manualdeal='no'
group by d.dealid)as t
group by daysbeforetestdrive

Open in new window

Capture.JPG
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.

Vitor MontalvãoMSSQL Senior EngineerCommented:
Try this:
select count(dealID) as RequestedTestDrive, 
    case 
	when daysbeforetestdrive > 4 then '5+'
	else CAST(daysbeforetestdrive as CHAR(1))
    end as daysbeforetestdrive
from 
	(select distinct d.dealid, datediff(day, max(cast(t.createdon as date)), max(cast (t.appointmentlocaltime as date))) as daysbeforetestdrive
	from [MakeMyDeal_com].[dbo].[TestDrivelocal] t
		inner join Mmd_feed..dealview d on t.dealid=d.dealid 
		inner join Mmd_feed..dart dt on dt.dealerid=d.dealerid
	where cast(t.createdon as date)>=@StartDate and cast(t.createdon as date)<=@EndDate
		and d.dealstatus='sent'
		and d.alternatedeal ='no'
		and d.manualdeal='no'
	group by d.dealid)as t
group by 
    case 
	when daysbeforetestdrive > 4 then '5+'
	else CAST(daysbeforetestdrive as CHAR(1))
    end as daysbeforetestdrive
order by 2

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
tanj1035Author Commented:
It works!!!Thanks a lot.
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.