September 24, 2007 at 1:24 pm
Hi
I have a view which returns the data as below.
RegionDesc | MATotal | PartsTotal | DemandTotal | Total |
1 | 7349.35 | 40763.52 | 6282.14 | 54395.01 |
2 | 28647.27 | 4093.57 | 55642.37 | 88383.21 |
3 | 64866.01 | 2601.77 | 138190.18 | 205657.96 |
4 | 219336.09 | 155.09 | 67716.4 | 287207.58 |
5 | 28293.00 | 4619.79 | 42894.18 | 75806.97 |
I need to add Percentage column next to the Total.
Calculation should be Sum(Total) / Total of each row.
RegionDesc | MATotal | PartsTotal | DemandTotal | Total | Percentage |
1 | 7349.35 | 40763.52 | 6282.14 | 54395.01 | 13.08 |
2 | 28647.27 | 4093.57 | 55642.37 | 88383.21 | 8.05 |
3 | 64866.01 | 2601.77 | 138190.18 | 205657.96 | 3.46 |
4 | 219336.09 | 155.09 | 67716.4 | 287207.58 | 2.48 |
5 | 28293.00 | 4619.79 | 42894.18 | 75806.97 | 9.39 |
Please suggest me.
Thx
Vijji
September 24, 2007 at 1:39 pm
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
September 24, 2007 at 2:24 pm
Hi
calculations should be
Sum(Total) / Total of each row
Thx
vijji
September 24, 2007 at 2:35 pm
Could you be more specific? Please show us how this calculation should be done using the data you provided in your original post.
😎
September 24, 2007 at 2:45 pm
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
September 24, 2007 at 2:50 pm
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
😎
September 24, 2007 at 3:07 pm
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?
September 24, 2007 at 3:12 pm
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
😎
September 24, 2007 at 3:23 pm
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?
September 24, 2007 at 3:25 pm
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','
|
','165px');" style="DISPLAY: inline" onclick="InstantASP_OpenMenu('_ctl1_ctlTopic_ctlPanelBar_ctlTopicsRepeater__ctl6_smAuthorName','_ctl1_ctlTopic_ctlPanelBar_ctlTopicsRepeater__ctl6_smAuthorName_SimpleMenuDivLayer','
|
','165px');">Lynn Pettis
It is working perfectly, thank you so much for your help.
September 24, 2007 at 3:49 pm
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