mysqli, change all rows by increasing by a number, but not over a certain value - mysqli

"UPDATE score SET
points = red_chips WHERE red_chips > current_turn"
I want to have points be equal to several different chip colours combined, but the MAXIMUM any individual chip colour can give you points must be limited by the current_turn, if the chip colour is higher it should default to only adding the current_turn.
"UPDATE score SET
points = red_chips + blue_chips + green_chips + yellow_chips + white_chips"
^ this but with EACH colour individually limited by the current_turn as a maximum amount.
I am looking for a way to do this without having to make a new query for each chip colour.

I think what you're looking for is LEAST(current_turn, [xyz]_chips). This will use the chip value if it is less than current_turn but the value of current_turn if the chip value is greater than that.
That is, if you want points to be equal to the sum of all of chip values with each limited to the value of current_turn, you want something like this:
UPDATE score SET points = LEAST(current_turn, red_chips) + LEAST(current_turn, blue_chips) + LEAST(current_turn, green_chips) + LEAST(current_turn, yellow_chips) + LEAST(current_turn, white_chips)
That's assuming that the [xyz]_chips values contain the actual net value (correcting for the value of each color). Depending on what current_turn represents relative to the chip values/colors you may want to multiple by the chips relative values. But if what you're looking for a count of the chips, not necessarily accounting for color, or if the [xyz]_chips column contains a color-adjusted value, then you should be fine.
Also, to clarify a bit about my edit, MIN is a aggregate function that acts on columns finding the minimum value from the rows. LEAST is takes two or more arguments and returns the one with the smallest value (or NULL if any of them are NULL), so it acts on the values of each individual row.

Related

Indexing a grid with row and column number?

Very new to QGIS. I'm using it to analyse agricultural field microplots for research.
I need to give each field plot its own object which I've done so by using the "create grid" tool. But I need to assign a column and row number to each object in the attribute table to make it easier to sort the data. This should start in the bottom left (1:1) and ascend in row for objects above (2:1) and ascend in column for objects to the right (1:2).
I've orientated the grid to start in the bottom left and learned that by using #row_number, I can re-number the objects from 1 to the total number of objects. But I need to add column number and row number to the attribute table.
Is there a simple way of doing this?
I think the most accessible option here is using the field calculator to add two integer columns to the attribute table. For a rectilinear grid with cells_y cells in the y direction (24 in this case) the expressions are something like
floor(("id" - 1) / cells_y) + 1
where id is the attribute table column with the cell number (set automatically by Vector -> Research Tools -> Create grid or, in the OP's case, to #rownumber).
For the y index it's
cells_y + 1 - if(("id" % cells_y) = 0, cells_y, "id" % cells_y)
This is for the ones based indexing asked for here; remove the + 1 bits for zero-based. Similarly, subtracting (as is done with y here) or not subtracting (x here) the modulus part flips the directions in which the cells are numbered. This approach can also be used in PyQGIS.
There's a few grid plugins for QGIS and several similar StackOverflow questions (like this one) which may also be of interest.

Leave null celles without colors

I have Tableau table with few measures.
I want my pallette to take place in every cell with value and leave cells with null values(blank) without color.
The current situation is that null values are colored as 0 values. How can I distinct the nulls from the 0 values?
I tried to create calculated field but tableau doesn't allow to create calculated field on aggregations
Create a calculated field [COLORSCALE YOUR FIELD] where the null-values are replaced by something off-scale. e.g. if your number range is between 0.0 and 100.0 you could use:
IIF(ISNULL([YOUR FIELD]),-100,[YOUR FIELD]*X)
Where X is a scaling factor. Adjust the scaling factor and the "Start"/"End" options in the "Color" Menue to suit your purpose.
Another option based on mzunhammer's suggestion would be to use the min() function instead of assigning a hard coded value to replace the nulls, and may allow you to avoid the use of the scaling factor.
iif(isnull([Your Field]), min([Your Field])-y, [Your Field])
that way the null is always going to be y less than the lowest value even if the lowest values are changing as the dataset updates.

MATLAB-How can I randomly select smaller values with higher probabilities?

I have a column vector "distances", and I want to select a value randomly from this vector such that smaller values have a higher probability of being selected. So far I am using the following, where "possible_cells" is the randomly selected value:
w=(fliplr(1:numel(distances)))/100
possible_cells=randsample((sort(distances)),1,true,w)
Basically, I flipped the distance vector to create probabilities of selection "w" (if I am understanding randsample correctly), so that the smallest value has the probability of being selected equal to the highest value. To check how well this works, I randomly drew 50 values and by using a histogram, I see that the values are higher than I would expect. Does anyone have any idea on how else to do what I described above?
0 Comments
How about something like this?
let's start with 10 sample distances with lengths no greater than 20 just to demonstrate:
d = randi(20,10,1);
Next, since we want smaller values to be more likely, let's take the reciprocal of those distances:
d_rec = 1./d;
Now, let's normalize so we can create a distribution from which to select our distance:
d_rec_norm = d_rec ./ sum(d_rec);
This new variable reflects the probability with which to select each given distance. Now comes a little trick... we choose the distance like this:
d_i = find(rand < cumsum(d_rec_norm),1);
This will give us the index of our chosen distance. The logic behind this is that when cumulatively summing the normalized values associated with each distance (d_rec_norm) we create "bins" whose widths are proportional to the likelihood of selecting each distance. All that is left is to pick a random number between 0 and 1 (rand) and see which "bin" it falls in.
I'm a new poster here, so let me know if this is unclear and I can try to improve my explanation.

How to find a corresponding value in a table in Matlab

In Matlab, I am given a table with two columns. Now I want to find the corresponding value of the left column to the maximum value of the right column:
P1_sat = P1(ismember(P2,max(P2)))
This works, however, the maximum is identical at 3 values of the left column, P1. These 3 values are right next to each other. So I want to consider the mid value. Is there a "consider the mid value" - command?
Adding the following code line will complete my goal:
P1_sat = sum(P1_sat,1) / length(P1_sat)
The three different values of P1, whose corresponding value of P2 is the maximum of P2, are added and then divided by 3. This gives the average value, which is also the mid point.

dynamic colour formatting in tableau

i have forecast bias values being displayed as percentages and i want to color those values based on certain thresholds like it should be RED if its value greater than 100% and less than -100% and green if between -99% to 99 % so i created a calculated field as
If [Forecast Bias] >= 100 THEN "Red"
elseif [Forecast Bias] <= -100 THEN "Red"
elseif [Forecast Bias] > -100 THEN "Green"
END
but i am getting some color field as Nul and wrong color on the values.I feel its taking actual values of forecast bias rather than percentage value for comparison.
Assuming you want to separate data rows based on their value for the [Forcecast Bias] field and treat rows separately depending on whether the value of [Forecast Bias] is within your range, then I suggest creating the following calculated field, called, say, "Bias_in_Range".
abs([Forecast Bias]) < 100
This defines a boolean valued dimension calculated field.
You can then place this field on the Color shelf to partition data according to that dimension.
To make the color legend more readable, you can create aliases for the dimension members, True and False in this case, to display labels like "In Range" and "Out of Range" to get readable color legend.
You could replace the hard coded 100 with a parameter, say "Bias Threshold", and show the parameter control to allow users to adjust the threshold dynamically.
abs([Forecast Bias]) < [Bias Threshold]
You will see null values for this field only for data rows that have a null value for [Forecast Bias]. If you don't like that, you have the choice of:
filtering out nulls,
fixing the source data to supply the missing field values, or
adjusting the definition of your calculated field to
treat the rows with null [Forcecast Bias] as either in range or out of range as you prefer, using a function like isNull() to test for null values
I can't speak to why you may see the wrong color without seeing more info about your data