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?
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 Developer, Architect, and AuthorCommented:
Illustrate for us what you mean by 'date range'.  Month and year?  Week?  Custom range?
A data mockup would be real helpful here.
0
Kyle AbrahamsSenior .Net DeveloperCommented:
You don't need day:

DateAdd("d",-(Day(getdate())-1), getdate())
becomes:
DateAdd("d",-1,getdate())
0
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.
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.

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()
0
tanj1035Author Commented:
Thanks, I have corrected it. but it still did not return value.
0
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?
0
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.
0
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?
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:
Yes, I have to join dealid
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.