SQL formula assistance to calculate percentages

I have 2 tasks that save data for Total Qty and Completed Qty, Nailing and Custom.

Custom Completed / Custom Total = Custom % Complete
COALESCE (a.Custom_Completed / NULLIF (a.Custom_TotalQty, 0), 0)

Nailing Completed / Nailing Total = Nailing % Complete
COALESCE (dbo.Manuf_Schedule_Nailing.NAIL_Custom_CompletedQty / NULLIF (dbo.Manuf_Schedule_Nailing.NAIL_Custom_TotalQty, 0), 0)

I need to combine the two totals for one % complete but the coalesce and nullifs are giving me incorrect values.

COALESCE ((dbo.Manuf_Schedule_Nailing.NAIL_Custom_CompletedQty + a.Custom_Completed) / (a.Custom_TotalQty + dbo.Manuf_Schedule_Nailing.NAIL_Custom_TotalQty), 1)


COALESCE (a.Custom_Completed / NULLIF (a.Custom_TotalQty, 0), 0) + COALESCE (dbo.Manuf_Schedule_Nailing.NAIL_Custom_CompletedQty / NULLIF (dbo.Manuf_Schedule_Nailing.NAIL_Custom_TotalQty, 0), 0)
Norm-alNetwork EngineerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

awking00Information Technology SpecialistCommented:
Can you provide some sample data and what you expect the output to be?
PortletPaulEE Topic AdvisorCommented:
Try using an APPLY operator. These are very convenient because you can give each calculation an alias, then use those aliases elsewhere in the query.

I have simply added the 2 percentages and divided by 2

    , Nailing_pct_Complete
    , (Custom_pct_Complete + Nailing_pct_Complete) / 2 as one_pct_complete
from your_table a
outer apply (
                  COALESCE (a.Custom_Completed / NULLIF (a.Custom_TotalQty, 0), 0)
                , COALESCE (dbo.Manuf_Schedule_Nailing.NAIL_Custom_CompletedQty / NULLIF (dbo.Manuf_Schedule_Nailing.NAIL_Custom_TotalQty, 0), 0)
            ) as OA (Custom_pct_Complete,Nailing_pct_Complete)

Open in new window

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:
Bianca, do you still need help with this question?
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.