SQL, One of the sum case result has a duplicate value.

Hi Experts,

Query A and Query B returned me different results for "Sold Deal". Both query A and B have the same condition. So, I do not know why the result is different for "Sold Deal" . Sum case works well on other columns, but not  on  "sold deal". Thanks.

In Query A, the value of "sent deal" , "accepted deal", "manual deal", "alternate deal" returns the correct value which is based on distinct dr.dealid, as dr.dealid has duplicate value.
but "sold deal" returns the value based on a duplicate dr. dealid. (i.e. 33)

In Query B, "sold deal" returns a correct value which is based on the distinct dr.dealid (i.e.5)



---------QUERY A--------

SELECT
      dc.respondsto AS type
    , a.sentby AS DealManager
    , d.dealershipname
    , SUM (CASE
            WHEN dc.respondsto = 'New' AND
                  d.VehicleStatus = 'New'
                             and d.dealstatus='sent'
                  and d.manualdeal='No'
                  and d.alternatedeal='No' THEN
                  1
            WHEN dc.respondsto = 'Used' AND
                  d.VehicleStatus = 'Used' 
                             and d.dealstatus='sent'
                  and d.manualdeal='Yes'
                  and d.alternatedeal='Yes'THEN
                  1
            WHEN dc.respondsto = 'Both' AND
                  d.VehicleStatus IN ('Used', 'New')
                             and d.dealstatus='sent'
                  and d.manualdeal='No'
                  and d.alternatedeal='No' THEN
                  1
            ELSE
                  0
      END) AS SentDeal,
SUM (CASE
            WHEN dc.respondsto = 'New' AND
                  d.VehicleStatus = 'New'
                             and d.dealstatus='sent'
                  and d.manualdeal='Yes'
                  and d.alternatedeal='Yes' THEN
                  1
            WHEN dc.respondsto = 'Used' AND
                  d.VehicleStatus = 'Used' 
                             and d.dealstatus='sent'
                  and d.manualdeal='Yes'
                  and d.alternatedeal='No'THEN
                  1
            WHEN dc.respondsto = 'Both' AND
                  d.VehicleStatus IN ('Used', 'New')
                             and d.dealstatus='sent'
                  and d.manualdeal='Yes'
                  and d.alternatedeal='No' THEN
                  1
            ELSE
                  0
      END) AS ManualDeal,
SUM (CASE
            WHEN dc.respondsto = 'New' AND
                  d.VehicleStatus = 'New'
                             and d.dealstatus='sent'
                  and d.manualdeal='No'
                  and d.alternatedeal='Yes' THEN
                  1
            WHEN dc.respondsto = 'Used' AND
                  d.VehicleStatus = 'Used' 
                             and d.dealstatus='sent'
                  and d.manualdeal='No'
                  and d.alternatedeal='Yes'THEN
                  1
            WHEN dc.respondsto = 'Both' AND
                  d.VehicleStatus IN ('Used', 'New')
                             and d.dealstatus='sent'
                  and d.manualdeal='No'
                  and d.alternatedeal='Yes' THEN
                  1
            ELSE
                  0
      END) AS Alternatedeal,
SUM (CASE
            WHEN dc.respondsto = 'New' AND
                  d.VehicleStatus = 'New'
                             and d.dealstatus='sent'
                  and DealerResponseStatus=4  THEN
                  1
            WHEN dc.respondsto = 'Used' AND
                  d.VehicleStatus = 'Used' 
                             and d.dealstatus='sent'
                and DealerResponseStatus = 4 THEN
                  1
            WHEN dc.respondsto = 'Both' AND
                  d.VehicleStatus IN ('Used', 'New')
                             and d.dealstatus='sent'
                  and DealerResponseStatus = 4  THEN
                  1
            ELSE
                  0
      END) AS AcceptedDeal,
SUM (CASE
            WHEN dc.respondsto = 'New' AND
                  d.VehicleStatus = 'New'
                             and d.dealstatus='Sent'
                  and d.archivedealstatus in ('Customer Sold','Sold- Switched Vehicle')THEN
                  1
            WHEN dc.respondsto = 'Used' AND
                  d.VehicleStatus = 'Used' 
                             and d.dealstatus='Sent'
                and d.archivedealstatus in ('Customer Sold','Sold- Switched Vehicle')THEN
                  1
            WHEN dc.respondsto = 'Both' AND
                  d.VehicleStatus IN ('Used', 'New')
                             and d.dealstatus='Sent'
                  and d.archivedealstatus in ('Customer Sold','Sold- Switched Vehicle') THEN
                  1
            ELSE
                  0
      END) AS SoldDeal
from mmd_feed..dealview d
inner join mmd_feed..dealercontact dc on d.dealerid=dc.dealerid 
inner join 
(select distinct dealid,sentby, sentbydealercontactid,DealerResponseStatus, timestamp from makemydeal_com..dealresponse2 dr where sentby is not null
and cast(timestamp as date) >= '2015-06-01' and cast(timestamp as date) <='2015-07-14'
) a 
on a.sentbydealercontactid=dc.dealercontactid 
and a.dealid= d.dealid
where 
d.dealerid in (select d.dealerid from mmd_feed..dart)
and dc.respondsto in ('New', 'Used', 'Both')
and cast(timestamp as date)>= '2015-06-1' and cast(timestamp as date) <= '2015-07-14'
--and d.SubmittedTimeStamp>= '2015-06-1' and d.SubmittedTimeStamp <='2015-07-14'
and d.dealerid= 241
group by  dc.respondsto,d.dealershipname, a.sentby

Open in new window


-----------QUERY B------------


------------SOLD DEAL----------
select a. dealid, dc.respondsto AS type,d.archivedealstatus
    , a.sentby AS DealManager
    , d.dealershipname
from mmd_feed..dealview d
inner join mmd_feed..dealercontact dc on d.dealerid=dc.dealerid
inner join makemydeal_com..dealrespon<wbr ></wbr>se2 a on a. sentbydealercontactid=dc.d<wbr ></wbr>ealerconta<wbr ></wbr>ctid and d.dealid=a.dealid
where 
d.dealstatus='sent'
and d.archivedealstatus in ('Customer Sold','Sold- Switched Vehicle')
and d.dealerid=100
and  cast(timestamp as date) >= '2015-06-01' and cast(timestamp as date) <='2015-07-14'
--and d.SubmittedTimeStamp>= '2015-06-1' and d.SubmittedTimeStamp <='2015-07-14'
and a.sentby is not null
AND dc.respondsto = 'Both' 
AND d.VehicleStatus IN ('Used', 'New')

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.

ste5anSenior DeveloperCommented:
First of all: The second query is not syntactically correct.
Second: The conditions are clearly different.

Query 1:

WHERE   d.dealerid IN ( SELECT  d.dealerid
                        FROM    mmd_feed..dart )
        AND dc.respondsto IN ( 'New', 'Used', 'Both' )
        AND CAST(timestamp AS DATE) >= '2015-06-1'
        AND CAST(timestamp AS DATE) <= '2015-07-14'
        AND d.dealerid = 241

Open in new window


Query 2:

WHERE   d.dealstatus = 'sent'
        AND d.archivedealstatus IN ( 'Customer Sold', 'Sold- Switched Vehicle' )
        AND d.dealerid = 100
        AND CAST(timestamp AS DATE) >= '2015-06-01'
        AND CAST(timestamp AS DATE) <= '2015-07-14'
        AND a.sentby IS NOT NULL
        AND dc.respondsto = 'Both'
        AND d.VehicleStatus IN ( 'Used', 'New' );

Open in new window


And last but not least, you're using a GROUP BY in the first query, but not in the second.  So the assumption must be: Your data is not what you've expected. Otherwise you would have the same results.

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
Vitor MontalvãoMSSQL Senior EngineerCommented:
Both query A and B have the same condition.
As stated above the conditions are not equal. Only the time period are identical but the rest.

So, I do not know why the result is different for "Sold Deal"
Now do you know why?
tanj1035Author Commented:
Thanks for your replies.

I have used different way to write the code for the query A and B. But their conditions are the same,
The conditions are
dc.respondsto = 'Both'
d.VehicleStatus IN ('Used', 'New')
d.archivedealstatus in ('Customer Sold','Sold- Switched Vehicle')
d.dealstatus='Sent'
cast(timestamp as date)>= '2015-06-1' and cast(timestamp as date) <= '2015-07-14'

I picked out part of my original query, please see them below. Thanks.


Query A  

SUM (
            WHEN dc.respondsto = 'Both' AND
                  d.VehicleStatus IN ('Used', 'New')
                             and d.dealstatus='Sent'
                  and d.archivedealstatus in ('Customer Sold','Sold- Switched Vehicle') THEN
                  1
            ELSE
                  0
      END) AS SoldDeal

where 
d.dealerid in (select d.dealerid from mmd_feed..dart)
and dc.respondsto in ('New', 'Used', 'Both')
and cast(timestamp as date)>= '2015-06-1' and cast(timestamp as date) <= '2015-07-14'
and d.dealerid= 100
group by  dc.respondsto,d.dealership<wbr ></wbr>name, a.sentby

Open in new window


Query B
 
where 
d.dealstatus='sent'
and d.archivedealstatus in ('Customer Sold','Sold- Switched Vehicle')
and d.dealerid=100
and  cast(timestamp as date) >= '2015-06-01' and cast(timestamp as date) <='2015-07-14'
and a.sentby is not null
AND dc.respondsto = 'Both' 
AND d.VehicleStatus IN ('Used', 'New')

Open in new window



To Stean5's question, if the assumption is because of the Group by, the other sum case results are correct, like sent deal, accepted deal etc...
Active Protection takes the fight to cryptojacking

While there were several headline-grabbing ransomware attacks during in 2017, another big threat started appearing at the same time that didn’t get the same coverage – illicit cryptomining.

ste5anSenior DeveloperCommented:
I don't get it. Why should be for example this predicate in

Query A: dc.respondsto in ('New', 'Used', 'Both')

the same as

Query B: dc.respondsto = 'Both'

???
tanj1035Author Commented:
Sorry for the confusing. I was doing the data validation for Dealer ID =100, whose dc.respondsto = Both. So, I twisted  the query a little bit for you to get a better understanding for the condition part.
tanj1035Author Commented:
I found out the reason, in query A
(select distinct dealid,sentby, sentbydealercontactid,DealerResponseStatus, timestamp from makemydeal_com..dealresponse2 dr where sentby is not null
and cast(timestamp as date) >= '2015-06-01' and cast(timestamp as date) <='2015-07-14'
) a
did not do a real distinct dealID because of different timestamp.

Thanks for your help.
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.