SSRS report - Calculate a new field based on the value of a different field - reporting-services

I am relatively new to this and would appreciate some help.
What I am trying to do is take the current value of a field and multiply it by some factor based on the value of a different field.
For example, if I have one column (Column A) that has three different values (Z, X, and Y) and another column (Column B) with a variety of numbers, I want to calculate a third field (Column C) based on these two associated values. So if the value of column A is equal to 'Z' the value in Column C would be equal to Column B multiplied by 2. If the value in column A is equal to 'X' the value in Column C would be equal to the value in Column B multiplied by 1.5. Finally, if the value in column A is equal to 'Y' then the value in Column C would just be equal to the value in Column B.
Example of Question
Is there a way of doing this just as a calculated field?
Thanks in advance for any help!

Set the expression of the column to something like
= Fields!ColumnB.Value *
SWITCH(
Fields!ColumnA.Value = "X", 1.5,
Fields!ColumnA.Value = "Y", 1,
Fields!ColumnA.Value = "Z", 2,
True, 0
)
The True line acts like an ELSE so anything that does not match X, Y or Z will get multiplied by 0 (resulting in zero)

Related

MYSQL select rows whose values are positive across all the x

I would like to perform a select similar to the example below. Basically I have three columns name, x, value. I would like to return the name whose values are all positive across a given range of x.
for example, I want to return the names whose values are all positive for x between 1 and 3, this should return y3 and y4. could anyone please help me, thanks.
select name
from your_table
where x between 1 and 3
group by name
having min(value) >= 0

MySQL comparing a substring with another String and if it matches get me the match percentage

If I have a column `Full_Names' in table cust and another Column as 'Models' in same table cust. The Full_name column contains ABCD and Models column contain AB then how can I write a query to get Models matches Full_Names 50% since only first two letters matched the whole string.
Thanks in Advance.
You can do:
case when locate(models, full_names)
then char_length(models) / char_length(full_name)
else 0
end match_ratio
This can be shorten as:
(locate(models, full_names) > 0) * char_length(models) / char_length(full_name)
This gives you a numeric value between 0 and 1 that represents the match ratio. If you want a percentage, you can multiply that by 100/

SSRS Conditional Max

I would like my expression to return the max value of a column where another column equals "0".
example :
0 12
0 11
0 7
1 3
1 40
1 1
This should return 12.
I tried several things but can't make it work.
Any ideas?
Try something like.
=MAX(IIF(Fields!ColumnA.Value = 0, Fields!ColumnB.Value, -99999))
Column A and B refer to your unnamed columns in your sample data. The -99999 should be a value lower than the lowest Columb B value you will ever get. If Column B is always positive then any negative value or even 0 will suffice here.
The expression reads:
"for each row, look in Column A. If Column A is zero then return Column B's value, if Column A is not zero, return -99999. Now get the MAX value from these values"

Getting SUM of MySQL table data when there is floating point numbers

I an using MySQL database and I have a table called fertilizer_storage which is using both plus and minus values. It has 4 columns uria,TSP,MOP and TDM
I am using double as data type and getting sum of each column using follwing syntax,
SELECT SUM(uria),SUM(TSP),SUM(MOP),SUM(TDM) FROM `fertilizer_storage` WHERE `branch_ID`=1
The problem is for some columns I get unwanted floating points of 15 while all columns are containing numbers up to 4 floating points.
7.666900000000002
7.666900000000002
9.6109
9.9924
when I changed numbers in first two columns as other two it gives the correct answer. what should I do to correct this.
You can use ROUND(X, D):
Rounds the argument X to D decimal places. The rounding algorithm
depends on the data type of X. D defaults to 0 if not specified. D can
be negative to cause D digits left of the decimal point of the value X
to become zero.
SELECT Round(Sum(uria), 4),
Round(Sum(tsp), 4),
Round(Sum(mop), 4),
Round(Sum(tdm), 4)
FROM `fertilizer_storage`
WHERE `branch_id` = 1
See it in action

How to order mysql rows in custom order?

I want to order mysql records by one column whose values start with N,Y,F,P,U. Type of column is VARCHAR. I want to order by column according to first letter of this column's values.
Specific order is N - Y - F - P - U.
So first record must be that one with some column's value N, the second record has that column's value starting with Y and so on.
How to order by first letter of column's value?
SELECT
...
ORDER BY
CASE SUBSTR(one_column,1,1)
WHEN 'N' THEN 0
WHEN 'Y' THEN 1
WHEN 'F' THEN 2
WHEN 'P' THEN 3
WHEN 'U' THEN 4
ELSE 5
END
...or join to lookup table of values, or use IF functions to map the char to a number.