SQL, how to apply date range for a column.

Hi Experts,

I want to apply date range (see bold part in the query to the column "COUNT(t.dealid) as testdrive" .

I wrote this query, but It did not return any value, only the header. Please help me on it. thanks.



use MMD_Feed


SELECT  dt.[dealerid]
      ,dt.[name]
       ,dt.[Alternatedealmtd]
      ,dt.[ManualDealmtd]
       ,dt.[wtd]
      ,dt.[mtd]
      ,dt.[lastmonth]
      ,dt.lastmonthtotal              
        ,dt.[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)
and t.dealid in (select t.dealid from  [MakeMyDeal_com].[dbo].[TestDrive]
where cast(t.createdon as date)>=DateAdd("d",-(Day(getdate())-1), getdate()) and cast(t.createdon 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.

Jim HornMicrosoft SQL Server Data DudeCommented:
Illustrate for us what you mean by 'date range'.  Month and year?  Week?  Custom range?
A data mockup would be real helpful here.
Kyle AbrahamsSenior .Net DeveloperCommented:
You don't need day:

DateAdd("d",-(Day(getdate())-1), getdate())
becomes:
DateAdd("d",-1,getdate())
tanj1035Author Commented:
I am trying to get the
count (t.dealid) as testdrive from cast(t.createdon as date)>=DateAdd("d",-1), getdate()) and cast(t.createdon as date)<getdate()

So, I do not know how can I put the date range above in a right place  in the query.

Thanks.
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.

Kyle AbrahamsSenior .Net DeveloperCommented:
you had an extra ")" in the dateadd function:

count (t.dealid) as testdrive from cast(t.createdon as date)>=DateAdd("d",-1, getdate()) and cast(t.createdon as date)<getdate()
tanj1035Author Commented:
Thanks, I have corrected it. but it still did not return value.
Kyle AbrahamsSenior .Net DeveloperCommented:
work on the inner query first:

select t.dealid from  [MakeMyDeal_com].[dbo].[TestDrive]
where cast(t.createdon as date)>=DateAdd("d",-1, getdate()) and cast(t.createdon as date)<getdate()

And are you sure there are any records for today?
tanj1035Author Commented:
If I run the query itself below
select t.dealid from  [MakeMyDeal_com].[dbo].[TestDrive]
 where cast(t.createdon as date)>=DateAdd("d",-1, getdate()) and cast(t.createdon as date)<getdate()

It can return the value.

But if I put it in the main query, it can not return value.
Kyle AbrahamsSenior .Net DeveloperCommented:
Are you sure you want to be joining by dealid?  Wouldn't you want it to be some kind of dealer ID?

the deal ids will only show up if they are created yesterday with the way you currently have the query structured.

What are you trying to do from a business sense?

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:
Yes, I have to join dealid
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.