SQL, SYNTAX ERROR

Hi Expert,

for "Lease percentage" , I have to use case when, otherwise, it will return null value sometimes. But the query in bold part got some syntax error, Can you someone help me to fix it?  Thanks.

error message : Incorrect syntax near ')'  before "end as"

select  ld.dealershipname, ld.dealerid, impressions, pencils, isnull(leasecompare,0) as Leasecompare, isnull(totallease,0), isnull(newtotalfinancedeals,0), isnull(financecompare,0) as Financecompare, isnull(soldarchived,0),
case when isnull(totallease,0) =0 then 0 else cast (totallease as float)
/(case when isnull(totallease,0) =0 then 0 else cast (totallease as float)+case when isnull (newtotalfinancedeals,0) = 0 then 0 else cast (newtotalfinancedeals as float)) end as Leasepercentage,

case when isnull(financecompare,0) =0 then 0 else cast(financecompare as float)/cast(totallease as float) end  as Leaseswitch
from #leasingdealers ld
left join #leasecompare2 lc on ld.dealerid=lc.dealerid
left join #financecompare2 fc on fc. dealerid=lc.dealerid
left join #deals d on ld.dealerid=d.dealerid
order by ld.dealershipname asc
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.

dsackerContract ERP Admin/ConsultantCommented:
On the CASE right after your division slash (/), you need an "end" (just before the plus sign).
ste5anSenior DeveloperCommented:
The problem is your denominator. There are two END's for each CASE missing:

SELECT  ld.dealershipname ,
        ld.dealerid ,
        impressions ,
        pencils ,
        ISNULL(leasecompare, 0) AS Leasecompare ,
        ISNULL(totallease, 0) ,
        ISNULL(newtotalfinancedeals, 0) ,
        ISNULL(financecompare, 0) AS Financecompare ,
        ISNULL(soldarchived, 0) ,
        CASE WHEN ISNULL(totallease, 0) = 0 THEN 0
             ELSE CAST (totallease AS FLOAT) / ( CASE WHEN ISNULL(totallease, 0) = 0 THEN 0
                                                      ELSE CAST (totallease AS FLOAT)
                                                 END + CASE WHEN ISNULL(newtotalfinancedeals, 0) = 0 THEN 0
                                                            ELSE CAST (newtotalfinancedeals AS FLOAT)
                                                       END )
        END AS Leasepercentage ,
        CASE WHEN ISNULL(financecompare, 0) = 0 THEN 0
             ELSE CAST(financecompare AS FLOAT) / CAST(totallease AS FLOAT)
        END AS Leaseswitch
FROM    #leasingdealers ld
        LEFT JOIN #leasecompare2 lc ON ld.dealerid = lc.dealerid
        LEFT JOIN #financecompare2 fc ON fc.dealerid = lc.dealerid
        LEFT JOIN #deals d ON ld.dealerid = d.dealerid
ORDER BY ld.dealershipname ASC; 

Open in new window


btw, when you really need the CAST, then you don't need thoses CASE's anyway:

        CASE WHEN ISNULL(totallease, 0) = 0 THEN 0
             ELSE CAST (totallease AS FLOAT) / ( CAST (ISNULL(totallease, 0) AS FLOAT) + CAST (ISNULL(newtotalfinancedeals, 0) AS FLOAT) )
        END AS Leasepercentage ,

Open in new window


And now we see, that we are having an incorrect guard clause against the div-by-0. This should be correct:

        CASE WHEN ( CAST (ISNULL(totallease, 0) AS FLOAT) + CAST (ISNULL(newtotalfinancedeals, 0) AS FLOAT) ) = 0 THEN 0
             ELSE CAST (ISNULL(totallease, 0) AS FLOAT) / ( CAST (ISNULL(totallease, 0) AS FLOAT) + CAST (ISNULL(newtotalfinancedeals, 0) AS FLOAT) )
        END AS Leasepercentage

Open in new window


And last, but not least: You're already using table alias names. Please qualify all columns always with the correct alias name.

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.