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)

OR

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?
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.

awking00Commented:
Can you provide some sample data and what you expect the output to be?
0
PortletPaulfreelancerCommented:
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

select
      Custom_pct_Complete
    , Nailing_pct_Complete
    , (Custom_pct_Complete + Nailing_pct_Complete) / 2 as one_pct_complete
from your_table a
outer apply (
            select 
                  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

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
Vitor MontalvãoMSSQL Senior EngineerCommented:
Bianca, do you still need help with this question?
0
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.