sql, query improvement

Hi Experts,

My query works, but I do not know if there is a way to improve my query?  The query pull out data from the same tables for the 2 columns "Acceptedbydealer" and "Acceptedbyshopper" based on 2 different where clause



with cte as (select count(*) as Acceptedbydealer,dv.dealerid
from MMD_Feed..Dealresponse dr
inner join MMD_Feed..Dealview dv on dr.dealid=dv.dealid
where dr.DealerResponseStatus='Accepted'
and cast(timestamp as date) between @StartDate and @EndDate
and dealerid = @Dealerid
group by dealerid)


select cte.dealerid, count(*) as Acceptedbyshopper, Acceptedbydealer
from MMD_Feed..Dealresponse dr
inner join MMD_Feed..Dealview dv on dr.dealid=dv.dealid
inner join cte cte on cte.dealerid=dv.dealerid
where dr.ShopperResponseStatus='Accepted'
and cast(timestamp as date) between @StartDate and @EndDate
and cte.dealerid = @Dealerid
group by cte.dealerid, Acceptedbydealer
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:
Define 'improved', as that could mean a lot of things, and most experts are not going to write a book on something this general.

Also, I see two queries here, are they connected, are you asking to improve both?  Not sure.

So, unless I'm really missing something here, this question is not very actionable without some more details.

>and cast(timestamp as date) between @StartDate and @EndDate  
Show me the values here, and then eyeball this article and make sure you're handling this correctly, as it's a common error where @EndDate is just the date, and using BETWEEN this way will leave out any datetime values after midnight of that day.
0
tanj1035Author Commented:
Thanks for your date query recommendation .
 I was trying to see if I can combine everything into 1 dataset, which means not using CTE.
0
tanj1035Author Commented:
So, I do not know if I express the improvement I am seeking clearly? Any recommendations? 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.

Mark ElySenior Coldfusion DeveloperCommented:
Jim is correct you need to provide more detail.   I can see you are using CTE's to separate a query count for Dealer verses Shopper Responses; however based on unclear table relations in your query, you do not need to join the MMD_Feed.Dealresponse  to MMD_Feed.Dealview because your input is taking a SINGLE @DelaerID which means you can count accepted records in the MMD_Feed.Dealresponse .  Assuming that is true, then you can CROSS APPLY a select query to count the Shoppercount with the same stimpulations.  This is only an assumption based on my understanding of your table layout.
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
The following query returns different data from yours?
select dv.dealerid, count(dr.ShopperResponseStatus) as Acceptedbyshopper, count(dr.DealerResponseStatus) as Acceptedbydealer 
from MMD_Feed..Dealresponse dr
	inner join MMD_Feed..Dealview dv on dr.dealid=dv.dealid
where dr.ShopperResponseStatus='Accepted'
	and dr.DealerResponseStatus='Accepted'
	and cast(timestamp as date) between @StartDate and @EndDate
	and dv.dealerid = @Dealerid
group by dv.dealerid

Open in new window

0
Scott PletcherSenior DBACommented:
SELECT
    dv.dealerid,
    SUM(case when dr.ShopperResponseStatus='Accepted' then 1 else 0 end) as Acceptedbyshopper
    SUM(case when dr.DealerResponseStatus ='Accepted' then 1 else 0 end) as Acceptedbydealer
FROM MMD_Feed..Dealresponse dr
INNER JOIN MMD_Feed..Dealview dv ON dr.dealid=dv.dealid
WHERE
  (dr.DealerResponseStatus='Accepted' or dr.ShopperResponseStatus='Accepted')
  and timestamp >= DATEADD(day, DATEDIFF(day, 0, @StartDate), 0)
  and timestamp < DATEADD(day, DATEDIFF(day, 0, @EndDate) + 1, 0)
  and dv.dealerid = @Dealerid
GROUP BY
  dv.dealerid
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
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.