SSRS Alternate Row Colour based on a value - reporting-services

I understand that the following will produce an alternate row colour on a row in a table: =IIF(ROWNUMBER(NOTHING) MOD 2, "LIGHTBLUE", "SILVER")
What I am looking for is to change colour based on set values, for example I have the following result:
RecId Seq
28292952799 0
28292952799 1
28292952799 2
28292970802 0
28292970802 1
28292980070 0
28292980070 1
28292980070 2
Based on the above, I would want it to colour based on each incrementation of the RecId Value.
My reason behind this is they do not want it grouped and it makes it more readable per ID.
Any help or advice is appreciated!

=IIF(RUNNINGVALUE(Fields!RecId.Value, COUNTDISTINCT,"MainDataSet") MOD 2 = 0,
"LightBlue",
"Silver")
This worked for me when i wanted to do something similar.
The expression is giving every distinct name a row number and using the modulo operation. If the row number divided by 2 has a remainder (odd numbers) then the color will be Silver

Related

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"

Mysql how to select rows by 2 criteria and only show the ones where rest of the columns = null?

I have a table called item configurations and it consists of ItemID, Color, Weight, Height.
If I do a select * from ItemConfigurations where color = 'red' and Weight = '100', I get more rows than I want.
For instance:
row 1: item 234, color red and weight 100 AND HEIGHT = 10
row 2: item 234, color red and weight 100
row 3: item 234, color red and weight 100 AND HEIGHT = 20 AND fabric = cloth
I wanted only the second row (red/100), but since the other rows also matches those criteria, they are also in the result.
The number of configurations (=columns) can go up in time when people add more things like for instance fabric = cloth.
So I can't just say select * from ItemConfigurations where color = 'red' and Weight = '100' and HEIGHT = null because once the fabric is added, I would also have to add fabric = null to the where clause.
Any advice about how to only get only the row I want (row 2 with where color = red and weight = 100) with a growing number of columns in this table?
I thought about adding a 'check' column and just insert the sum of all the values in the columns upon inserting / updating a row in the table and putting that check into my select. But it doesn't feal like the right way to go about this. (all values are integers btw as it's normalized)
Although I work with php, I would like to do the above inside a mysql query. Hopefully someone knows how to got about this.
Thanks for any advice!

How do I make a google query that selects for row?

Example:
If my data set is
A B C
1 2 3
5 6 7
4 5 6
I could have "1", "5" and "4" show up by typing =query(A:C, select A).
I could have "1" and "4" show up by typing =query(A:C, select A where B < 6).
Lets say I wanted to query only entries that appeared after a certain row. In this case, row 3 is 4, 5, 6. So if I want only results that are row 3 or below, I could add a fourth column D somewhere, fill column D with =row(), and then have only **** show up by typing
=query(A:C, select A where D >= 3).
But I don't want to have to add a fourth column somewhere and fill it with the =row() formula. The query should be able to do this on its own.
Query parametres
try:
=QUERY(A:C,"select * offset 2",0)
offset parameter is zero base:
0 -- start from row 1
1 -- start from row 2
2 -- start from row 3
so on
You may find more usuful query tips here. Use special words: offset, limit, skipping. For example, to select only odd rows use:
=QUERY(A:C,"select * skipping 2",0)
Filter function
To have full control of rows you select, use this construction:
=filter(A:C,isodd(row(A:C))) -- only odd rows
=filter(A:C,row(A:C)=3) -- only 3-d row
=filter(A:C,row(A:C)>=3) -- all rows >= 3-d row
=query(filter(A:C,row(A:C)>=3),"select *") use filter + query
So, I had an issue like this, and here is what I did. I made a virtual column.
=query(A:C, select A where D >= 3) would then be something like
=query({A:C, ROW(A:C)}, "Select Col1 where Col4 >=3")
You have to make an array to query a column you do not want in your sheet. In doing so you can no longer use Column letters, so then A, B, C then becomes Col1, Col2, Col3 and so on.

How to create query with simple formula?

Hey is there any way to create query with simple formula ?
I have a table data with two columns value_one and value_two both are decimal values. I want to select this rows where difference between value_one and value_two is grater then 5. How can i do this?
Can i do something like this ?
SELECT * FROM data WHERE (MAX(value_one, value_two) - MIN(value_one, value_two)) > 5
Example values
value_one, value_two
1,6
9,3
2,3
3,2
so analogical difs are: 5, 6, 1, 1 so the selected row would be only first and second.
Consider an example where smaller number is subtracted with a bigger number:
2 - 5 = -3
So, the result is a difference of two numbers with a negation sign.
Now, consider the reverse scenario, when bigger number is subtracted with the smaller number:
5 - 2 = 3
Pretty simple right.
Basically, the difference of two number remains same, if you just ignore the sign. This is in other words called absolute value of a number.
Now, the question arises how to find the absolute value in MySQL?
Answer to this is the built-in method of MySQL i.e. abs() function which returns an absolute value of a number.
ABS(X):
Returns the absolute value of X.
mysql> SELECT ABS(2);
-> 2
mysql> SELECT ABS(-32);
-> 32
Therefore, without worrying about finding min and max number, we can directly focus on the difference of two numbers and then, retrieving the absolute value of the result. Finally, check if it is greater than 5.
So, the final query becomes:
SELECT *
FROM data
WHERE abs(value_one - value_two) > 5;
You can also do complex operations once the absolute value is calculated like adding or dividing with the third value. Check the code below:
SELECT *
FROM
data
WHERE
(abs(value_one - value_two) / value_three) + value_four > 5;
You can also add multiple conditions using logical operators like AND, OR, NOT to do so. Click here for logical operators.
SELECT *
FROM
data
WHERE
((abs(value_one - value_two) / value_three) + value_four > 5)
AND (value_five != 0);
Here is the link with various functions available in MySQL:
https://dev.mysql.com/doc/refman/5.0/en/mathematical-functions.html
No, you would just use a simple where clause:
select *
from data
where abs(value_one - value_two) > 5;

Select random row from MySQL (with probability)

I have a MySQL table that has a row called cur_odds which is a percent number with the percent probability that that row will get selected. How do I make a query that will actually select the rows in approximately that frequency when you run through 100 queries for example?
I tried the following, but a row that has a probability of 0.35 ends up getting selected around 60-70% of the time.
SELECT * FROM table ORDER BY RAND()*cur_odds DESC
All the values of cur_odds in the table add up to 1 exactly.
If cur_odds is changed rarely you could implement the following algorithm:
1) Create another column prob_sum, for which
prob_sum[0] := cur_odds[0]
for 1 <= i <= row_count - 1:
prob_sum[i] := prob_sum[i - 1] + cur_odds[i]
2) Generate a random number from 0 to 1:
rnd := rand(0,1)
3) Find the first row for which prob_sum > rnd (if you create a BTREE index on the prob_sum, the query should work much faster):
CREATE INDEX prob_sum_ind ON <table> (prob_sum);
SET #rnd := RAND();
SELECT MIN(prob_sum) FROM <table> WHERE prob_sum > #rnd;
Given your above SQL statement, whatever numbers you have in cur_odds are not the probabilities that each row is selected, but is instead just an arbitrary weighting (relative to the "weights" of all the other rows) which could instead be best interpreted as a relative tendency to float towards the top of the sorted table. The actual value in each row is meaningless (e.g. you could have 4 rows with values of 0.35, 0.5, 0.75 and 0.99, or you could have values of 35, 50, 75 and 99, and the results would be the same).
Update: Here's what's going on with your query. You have one row with a cur_odds value of 0.35. For the sake of illustration, I'm going to assume that the other 9 rows all have the same value (0.072). Also for the sake of illustration, let's assume RAND() returns a value from 0.0 to 1.0 (it may actually).
Every time you run this SELECT statement, each row is assigned a sorting value by multiplying its cur_odds value by a RAND() value from 0.0 to 1.0. This means that the row with a 0.35 will have a sorting value between 0.0 and 0.35.
Every other row (with a value of 0.072) will have sorting values ranging between 0.0 and 0.072. This means that there is an approximately 80% chance that your one row will have a sorting value greater than 0.072, which would mean that there is no possible chance that any other row could be sorted higher. This is why your row with the cur_odds value of 0.35 is coming up first more often than you expect.
I incorrectly described the cur_odds value as a relative change weighting. It actually functions as a maximum relative weighting, which would then involve some complex math to determine the actual relative probabilities involved.
I'm not sure what you need can be done with straight T-SQL. I've implemented a weighted probability picker many times (I was even going to ask a question about best methods for this this morning, ironically) but always in code.