Not adding Percentage, when I used "Computed Column" for Percentage Field - sql-server-2017

Not adding Percentage, when I used "Computed Column" for Percentage Field
I try to calculate the percentage of the student marks using "Computed Column" but I'm unable to do that
Can any one please tell me where I did the mistake to calculate the percentage
thanks in advance .

You are getting 0 because you are working only with integers
If you divide by 125.0 it will force SQL Server to implicitely cast values to a decimal using order-of-precedence.

Related

SSRS expression for variance %

I have two "Total" columns across two tablix as below:
one is SUM(Previous) and the other is SUM(Current)
i'm trying to calculate Variance between these columns as "Expressions" in a new Tablix, I'm trying to get the PreviousTotal and CurrentTotal through SQL and then to calculate Variance as an expression.
how to calcualte variance in ssrs expression between these two columns ?
You don't mean the variance, I think you mean something like "percentage deviation" from a previous to the current value. Why would you want to do this with a SUM? The difference between a "current sum" and a "previous sum" usually is just a "current value", and if you set this in relation to a 'SUM', it might look very small. But ok, in some scenarios this might make sense.
So, If you really want to calculate the "percentage deviation" of SUM(Previous) and SUM(Current), the formula would be:
(SUM(Current) - SUM(Previous)) / SUM(Previous)
Format the value as a percentage.
Reference the PreviousTotal and CurrentTotal by Textbox ID.Value, and do math on them.

SQL Long Decimal Value Comparison

So I have two identical values that are results of sum functions with the exact same length (no rounding is being done). (Update, data type is float)
Value_1 = 29.9539194336501
Value_2 = 29.9539194336501
The issue I'm having is when I do an IF statement for Value_1 = Value_2, it comes up as FALSE.
Value_1:
SELECT SUM([INVN_DOL])/SUM([AVG_DLY_SLS_LST_35_DYS]) end as DSO
FROM TABLE A
Value_2:
SELECT SUM ([Total_Inventory_Val]) / SUM ([Daily_Independent_Demand])
FROM TABLE B
Any idea why they may not be exactly equal and what I can do to get a TRUE value since they do match?
Thanks in advance
The issue you are having here is that your are using a calculated value that is held within a float, which will by design be slightly imprecise at higher levels of precision, which is why you are getting your mismatch.
Use data types like decimal with a defined precision and scale to hold your values and calculation results and you should get consistent results.
You can make use ROUND to limit the decimal points
Or
Try the ABS and see if that works out.

SSRS Divide Sums from Different Scopes Returns 0

I have a grouped dataset. From the parent group down, the group names are:
Company > Plant > Details. In the end, I want to be able to take the sales of each plant and see what percentage it is of the entire company's sales. Let's say the company has a total sales of $500 and the sales of "Plant A" were $100 and "Plant B" sales were $400. I would image that I would need an expression at the Plant grouping level that was like:
=Sum(Sales)/Sum(Sales, "Company")
And I would get .2 for Plant A and .8 for Plant B. But if I do that, I get 0. I am at a complete loss. Any help with this would be greatly appreciated.
Additional information:
My exact setup is a little more complex than the example I gave below, but I believe the general idea still holds the same. I have a total of 6 groups:
The circled group is the equivalent of the "Plant" Group in my example. Here is the row group in my tablix for the GroupBy group (the one with the arrow pointing to it).
The expression that is circled in the picture above is the expression in question to get my percentage (right now really just a decimal, not formatted to be a percentage yet).
=sum(Fields!ActualCurrent.Value)/sum(Fields!ActualCurrent.Value, "Company")
Fields!ActualCurrent.Value is the equivalent of "Sales" in my example above. The expression above returns 0 for all groups. But yet, if I change it to
=sum(Fields!ActualCurrent.Value)+sum(Fields!ActualCurrent.Value, "Company")
It will produce the equivalent of $600 for "Plant A" and $900 for "Plant B."
I can't seem to find how it reacts as expected when adding the two sums, but produces 0 when I divide them.
It would be useful to see where are you using that expression to determine what is wrong here, but I think you can use this guide to get your desired result.
Create a tablix like this:
Note I've added Company and Plant fields as groups. Also I've deleted details group. Right click details and select Delete group and set Delete group only option.
Now in the percentage column use the following expression:
=FORMAT(
SUM(Fields!Sales.Value,"Plant")/SUM(Fields!Sales.Value,"Company")
,"P2"
)
The sum of every plan divided by the sum of the whole company group. It is not necessary but I am using FORMAT function to format the float value returned by the expression to percentage format using two decimal places.
It should show something like this:
UPDATE: Try scoping the sum to your specific group: GroupBy
=sum(Fields!ActualCurrent.Value, "GroupBy")/sum(Fields!ActualCurrent.Value, "Company")
UPDATE 2: Format the cell to show decimal digits.
Use thiss expression:
=FORMAT(
sum(Fields!ActualCurrent.Value, "GroupBy")/sum(Fields!ActualCurrent.Value, "Company"),
"F2"
)
It will format the value returned by the expression as a float with two decimal digits.
If you want to show the value in percentage format replace F2 in the expression for P2 (Percentage format with two decimal digits.)
Let me know if this helps.

SQL "IF" expression coding

Im using a program that uses SQL in the background and I have run into a snag and I just cant figure it out.
It has asked for a SQL Expression to calculate cost mark up in a data field. I sort of get how it works because Ive used Excel and have done some complex calculations but SQL is different and I am having a hard time grasping it. Anyway ...
I need to have the expression code say:
If column A is between $0-$9.99 add 70%
If column A is between $10-$14.99 add 30%
If column A is between $15-$24.99 add 21%
If column A is between $25-$89.99 add 14.9%
If column A is between $90-$244.99 add 10%
If column A is between $245-$499.99 add 9%
If column A is between $500-1999.99 add 6.9%
If column A is $3000 and up add 7.5 %
It should be all one string / expression
Also it should round to the 2nd decimal place.
If I am missing something or using the wrong wording forgive me -SQL newbie here
Please help me!!
Thank you
Use a case:
select cast(
case
when columnA between 0 and 9.99 then columnA * 1.7
when columnA between 10 and 14.89 then columnA * 1.3
-- etc for other value ranges
else columnA
end as decimal(10, 2)) as columnA_plus_fee

SQL - View column that calculates the percentage from other columns

I have a query from Access where I caluclated the percentage score of three seperate numbers Ex:
AFPercentageMajor: [AFNumberOfMajors]/([AFTotalMajor]-[AFMajorNA])
which could have values of 20/(23-2) = 95%
I have imported this table into my SQL database and tried to write a expression in the view (changed the names of the columns a bit)
AF_Major / (AF_Major_Totals - AF_Major_NA)
I tried adding *100 to the end of the statement but it only works if the calculation is at 100%. If it is anything less than that it puts it as a 0.
I have a feeling it just doesn't like the combincation of the three seperate column names. But like I said I'm still learning so I could be going at this completely wrong!
SQL Server does integer division. You need to change one of the values to a floating point representation. The following will work:
cast([AFNumberOfMajors] as float)/([AFTotalMajor]-[AFMajorNA])
You can multiply this by 100 to get the percentage value.