Need help in adding Percentage column

  • Hi

    I have a view which returns the data as below.

    RegionDescMATotalPartsTotalDemandTotalTotal
    17349.3540763.526282.1454395.01
    228647.274093.5755642.3788383.21
    364866.012601.77138190.18205657.96
    4219336.09155.0967716.4287207.58
    528293.004619.7942894.1875806.97

    I need to add Percentage column next to the Total.

    Calculation should be Sum(Total) / Total of each row.

    RegionDesc     MATotalPartsTotalDemandTotalTotalPercentage
    17349.3540763.526282.1454395.0113.08
    228647.274093.5755642.3788383.218.05
    364866.012601.77138190.18205657.963.46
    4219336.09155.0967716.4287207.582.48
    528293.004619.7942894.1875806.979.39

    Please suggest me.

    Thx

    Vijji

  • how about this?

    SELECT

    RegionDesc,

    MATotal,

    PartsTotal,

    DemandTotal,

    Total,

    (CASE WHEN ISNULL(Total,0.00) = 0 then 0

         ELSE  ISNULL(MATotal,0.00)    +

               ISNULL(PartsTotal,0.00) +

               ISNULL(DemandTotal,0.00)   / Total END) * 100 As Percentage

    FROM SOMETABLE

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Hi

    calculations should be

    Sum(Total) / Total of each row

    Thx

    vijji

  • Could you be more specific?  Please show us how this calculation should be done using the data you provided in your original post.

    😎

  • Hi

    I am adding calculation column.

    RegionDesc MATotal  PartsTotal  DemandTotal  Total  Percentage 
    1     7,349.35   40,626.75         6,282.14    54,258.24 13.11(=711313.96 / 54258.24)
    2   28,647.27    4,093.57       55,642.37    88,383.21 8.05(=711313.96 / 88383.21)
    3   64,866.01    2,601.77     138,190.18  205,657.96 3.46(=711313.96 / 205657.96
    4 219,336.09       155.09       67,716.40  287,207.58 2.48(=711313.96 / 287207.58)
    5   28,293.00    4,619.79       42,894.18    75,806.97 9.38(=711313.96 / 75806.97)
     Total:   711,313.96

    Thx

    Vijji

  • Try this:

    ;with TotalAmt (

        GrandTotal

    ) as (

    select

        sum(Total)

    from

        dbo.yourView

    )

    select

        RegionDesc,

        MATotal,

        PartsTotal,

        DemandTotal,

        Total,

        cast(( GrandTotal/ Total) as decimal(5,2)) as Percent

    from

        dbo.yourView

        cross join TotalAmt

    😎

  • For what it's worth - that's the INVERSE of a percentage.

    Percent of total = group total/Grand total (NOT Grand total/Group Total).

    Might as well save you the pain before an accounting type comes along and wonders why they don't add up to 100%=1....

    Of course - other than that - Lynn's solution will work great.

    ----------------------------------------------------------------------------------
    Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?

  • Just giving to OP what was requested.  I'd prefer this for the percentage column:

    cast((Total/GrandTotal) * 100 as decimal(5,2)) as Percent

    😎

  • Understood Lynn - I noticed OP had calculation backwards.  I should have specified WHO the comment was directed at.  I haven't figured out the quote thingie in the new version of the boards yet:)

    ----------------------------------------------------------------------------------
    Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?

  • Hi

    <DIV id=_ctl1_ctlTopic_ctlPanelBar_ctlTopicsRepeater__ctl6_smAuthorName_SimpleMenuDivLayer onmouseover="InstantASP_OpenMenuMouseOver('_ctl1_ctlTopic_ctlPanelBar_ctlTopicsRepeater__ctl6_smAuthorName','_ctl1_ctlTopic_ctlPanelBar_ctlTopicsRepeater__ctl6_smAuthorName_SimpleMenuDivLayer','

    View Member\'s Profile
    Add to Buddy List
    Send Private Message
    Send Email To Member
    Find All Member\'s Posts

    ','165px');" style="DISPLAY: inline" onclick="InstantASP_OpenMenu('_ctl1_ctlTopic_ctlPanelBar_ctlTopicsRepeater__ctl6_smAuthorName','_ctl1_ctlTopic_ctlPanelBar_ctlTopicsRepeater__ctl6_smAuthorName_SimpleMenuDivLayer','

    View Member\'s Profile
    Add to Buddy List
    Send Private Message
    Send Email To Member
    Find All Member\'s Posts

    ','165px');">Lynn Pettis

    It is working perfectly, thank you so much for your help.

  • Actually, Matt, I was agreeing with you on your comment.

Viewing 11 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic. Login to reply