SQL, How to count "sent deal" by different conditions?

Hi Experts,

I want to calculate the column "sent deal" by the different conditions
1) when dc.respondsto = 'New', count "sent deals" when d.vehiclesstatus = 'New'
2)when dc.respondsto = 'Used', count "sent deals" when d.vehiclesstatus = 'Used'
3)when dc.respondsto = 'Both',  count "sent deals" when and d.vehiclesstatus = 'Used'+'New'

I do not know how can I write the query for those 3 conditions, I tried to use "sum case" and I knew it was not correct. Can you help me on it? Thanks.

select dc.respondsto as type,
dr.sentby as DealManager, 
d.dealershipname,
sum (case when dc.respondsto='New'and d.VehicleStatus ='New' then 1 else 0 end) as SentDeal,
sum (case when dc.respondsto='Used'and d.VehicleStatus ='Used' then 1 else 0 end) as SentDeal,
sum (case when dc.respondsto ='Both'and d.VehicleStatus in ('Used', ' New') then 1 else 0 end) as SentDeal
from mmd_feed..dealview d
inner join mmd_feed..dealercontact dc on d.dealerid=dc.dealerid
inner join mmd_feed..dealresponse dr on dr. dealid=d.dealid
where d.dealerid in (select d.dealerid from mmd_feed..dart)
and d.dealstatus='sent'
and d.manualdeal='No'
and d.alternatedeal='No'
and dc.respondsto in ('New', 'Used', 'Both')
and dr.sentby is not null
and d.submittedtimestamp >= '2015-07-01' and d.submittedtimestamp <= '2015-07-14'
group by  dr.sentby,d.dealershipname,dc.respondsto

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.

Brian CroweDatabase AdministratorCommented:
Using SUM/CASE is exactly what I would do.  How is it "not correct"?
Scott PletcherSenior DBACommented:
You can't name three result columns all "SentDeal" and then reference the result, because SQL wouldn't know which column "SELECT SentDeal" refers to.

Try this:

select dc.respondsto as type,
dr.sentby as DealManager,
d.dealershipname,
sum (case when dc.respondsto='New' and d.VehicleStatus ='New' then 1 else 0 end) as SentDeal_New,
sum (case when dc.respondsto='Used' and d.VehicleStatus ='Used' then 1 else 0 end) as SentDeal_Used,
sum (case when dc.respondsto='Both' and d.VehicleStatus in ('Used', ' New') then 1 else 0 end) as SentDeal_Both
from mmd_feed..dealview d
inner join mmd_feed..dealercontact dc on d.dealerid=dc.dealerid
inner join mmd_feed..dealresponse dr on dr. dealid=d.dealid
where d.dealerid in (select d.dealerid from mmd_feed..dart)
and d.dealstatus='sent'
and d.manualdeal='No'
and d.alternatedeal='No'
and dc.respondsto in ('New', 'Used', 'Both')
and dr.sentby is not null
and d.submittedtimestamp >= '2015-07-01' and d.submittedtimestamp <= '2015-07-14'
group by  dr.sentby,d.dealershipname,dc.respondsto
tanj1035Author Commented:
Thanks for your reply. I have to do new, used and both in the same column " sent deal"

So, I don't know how to do it.
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.

PortletPaulEE Topic AdvisorCommented:
You are using dc.respondsto in the group by clause. In other words, you will get 3 ROWS

respondsto  SentDeal
Both              ?
New              ?
Used             ?

i.e. for each ? above, the sum is for that respondsto value

So I think you only need this case expression:
SELECT
      dc.respondsto AS type
    , dr.sentby AS DealManager
    , d.dealershipname
    , SUM(CASE
            WHEN dc.respondsto = 'New' AND
                  d.VehicleStatus = 'New' THEN
                  1
            WHEN dc.respondsto = 'Used' AND
                  d.VehicleStatus = 'Used' THEN
                  1
            WHEN dc.respondsto = 'Both' AND
                  d.VehicleStatus IN ('Used', ' New') THEN
                  1
            ELSE
                  0
      END) AS SentDeal
FROM mmd_feed..dealview d
INNER JOIN mmd_feed..dealercontact dc
      ON d.dealerid = dc.dealerid
INNER JOIN mmd_feed..dealresponse dr
      ON dr.dealid = d.dealid
WHERE d.dealerid IN (
      SELECT
            d.dealerid
      FROM mmd_feed..dart
)
AND d.dealstatus = 'sent'
AND d.manualdeal = 'No'
AND d.alternatedeal = 'No'
AND dc.respondsto IN ('New', 'Used', 'Both')
AND dr.sentby IS NOT NULL
AND d.submittedtimestamp >= '2015-07-01'
AND d.submittedtimestamp <= '2015-07-14'
GROUP BY dr.sentby
       , d.dealershipname
       , dc.respondsto

Open in new window

It would be much easier to demonstrate if you supplied some sample data.

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:
Thanks a lot, That is what I am looking for.
PortletPaulEE Topic AdvisorCommented:
:)  it's a pleasure.
Thanks for closing off quickly.
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.