SQL Query - Conditional Values in a User-defined Column - mysql

Hi Stack Overflow Community,
I am researching how to create a query that conditionally assigns values in a user-defined column based upon values in another column. I didn't know if this was entirely possible, as I couldn't find any references on this. I know that it's possible to create a user-defined column by just entering in something like 'Yellow' As Color, but these are limited to static values.
I have provided an example of the output below, and the end result would be the user-defined column values would be a string.
X(Column from Table) Color(User-Defined Column)
1 if X = 1, Color = 'Brown'
2 if X = 2, Color = 'Blue'
3 if X = 3, Color = 'Red'
4 if X = 4, Color = 'Orange'
5 if X = 5, Color = 'Purple'
X Color
1 Brown
2 Blue
3 Red
4 Orange
5 Purple
Any input would be greatly appreciated, and thank you in advance!
Daniel

For small amount of available values i think case will be most appropriate.
SELECT X,
CASE
WHEN X = 1 THEN "Brown"
WHEN X = 2 THEN "Blue"
WHEN X = 3 THEN "Red"
WHEN X = 4 THEN "Orange"
WHEN X = 5 THEN "Purple"
ELSE "No color"
END AS Color
FROM Table;

Related

How to add alphanumeric values in a speardsheet if they are comma separated?

Suppose, we have cells as below:
Cell Value Legend
==========================
A1 1,A // A = 1
A2 2,AA // AA = 2
A3 3,L // L = -1
A4 4,N // N = 0
I want the total to be calculated separately in other cells as:
A5 = SUM(1, 2, 3, 4) = 1 + 2 + 3 + 4 = 10
A6 = SUM(1*A, 2*AA, 3*L, 4*N) = 1 + 4 - 3 + 0 = 2
Considering it may require separate functions in App Script, I tried to use SPLIT and SUM them, but it's not accepting the values. I asked a related question: How to pass multiple comma separated values in a cell to a custom function?
However, being a novice in spreadsheet, I am not sure if my approach is correct.
How to add alphanumeric values separately as stated above?
you can create a small lookup table (legend) and then for the first sum try something like
=ArrayFormula(sum(iferror(REGEXEXTRACT(A1:A4, "[0-9-.]+")+0)))
and for the last
=sum(ArrayFormula(iferror(regexextract(A1:A4, "[0-9-.]+")*vlookup(regexextract(A1:A4, "[^,]+$"),D1:E4, 2, 0 ))))

Outputs of a program

I am new to programming and I would like to know how to solve questions like this. I was told to expect questions like this on the exam. Can someone please tell me how I would go about solving something like this? Thanks.
x = 0
for num in range(5):
if num % 2 == 0:
x = x + 2
else:
x = x + 1
print(x)
You need to work on a skill which is to "be the compiler", in the sense that you should be able to run code in your head. Step through line by line and make sure you know what is happening. In you code example, you have
for num in range(5) means you will be iterating with num being 0,1,2,3 and 4. Inside the for loop, the if statement num % 2 == 0 is true when num/2 does not have a remainder (how % mods work). So if the number is divisible by 2, x = x+2 will execute. The only numbers divisible by 2 from the for loop are 0,2 and 4. so x=x+2 will execute twice. The else statement x = x +1 runs for all other numbers (1,3) which will execute 2 times.
Stepping through the for loop:
num = 0 //x=x+2, x is now 2
num = 1 //x=x+1, x is now 3, print(x) prints 3
num = 2 //x=x+2, x is now 5
num = 3 //x=x+1, x is now 6, print(x) prints 6
num = 4 //x+x+2, x is now 8
Therefore the answer is that 3 and 6 will be printed
In my opinion,
Whatever language you are using, you need to learn some common elements of the modern programming languages, such as flow-control (if...else in your case), loop(for, in your case)
Some common used functions, in your case, you need to what does range do in Python,
docs.python.org is a good place for you.
As you are new to programming, you can go with the flow in you mind or draw it on the paper.
Using x to store our final result
loop through every item in [0, 1, 2, 3, 4] <- range(5)
a. if
the number is divisible by 2
then increase x by adding 2 to it.
b. else
increase x by adding 1 and print it out
So the result would be :
3
6

Multiple MySQL queries from same table

Thanks in advance for the help.
I have a table labelled attributes. In that table is product item numbers (pin), and attribute numbers. Each attribute for a pin is in a separate row
Ex.
pin attribute
111 4
111 5
111 10
112 4
112 5
...
I am trying to find a query that will allow me to say "Select pin if attribute = 4 and attribute = 5"
The attributes would be color, size, etc.. so get all records that are red (4) and size Small (5).
In the example above, it would return pins 111 and 112.
Mike
This should work for you using count with distinct:
select pin
from attributes
where attribute in (4,5)
group by pin
having count(distinct attribute) = 2
This will return any pins that have both attributes 4 and 5.
SQL Fiddle Demo
This is an example of a "set-within-sets" query. I advocate doing this with aggregation and a having clause. For your example, this would look like:
select pin
from attributes
group by pin
having sum(attribute = 4) > 0 and
sum(attribute = 5) > 0;
The reason I like this approach is because it is flexible. If you wanted attributes 4 or 5, the query would be:
having sum(attribute = 4) > 0 or
sum(attribute = 5) > 0;
If you wanted 4 and 5 and nothing else:
having sum(attribute = 4) > 0 and
sum(attribute = 5) > 0 and
sum(attribute not in (4, 5)) = 0;
select pin,
group_concat(distinct attribute order by attribute) as atts
from attributes
where attribute in (4,5)
group by pin
having (atts = '4,5');
fiddle

Available Filters With Specified Ranges In SSRS

I am working on a Chart in my report.
As I have too many records where CountId = 1, I have set up a filter showing an available values list like this:
CountId :
1
2
3
Between 4 to 6
Between 7 to 9
Above 10
If I set the available value 1 or 2 or 3 it shows results, but I don`t know how to set a filter for between and above.
I want a filter some thing like this - available filters are:
1
2
3
4
Above 5 or greater than equal to 5
You've got a mix of operators, so maybe you should look at an expression based filter to try and handle these different cases, something like:
Expression (type Text):
=Switch(Parameters!Count.Value = "1" and Fields!Count.Value = 1, "Include"
, Parameters!Count.Value = "2" and Fields!Count.Value = 2, "Include"
, Parameters!Count.Value = "3" and Fields!Count.Value = 3, "Include"
, Parameters!Count.Value = "4 to 6" and Fields!Count.Value >= 4 and Fields!Count.Value <= 6, "Include"
, Parameters!Count.Value = "7 to 9" and Fields!Count.Value >= 7 and Fields!Count.Value <= 9, "Include"
, Parameters!Count.Value = "Above 10" and Fields!Count.Value >= 10, "Include"
, true, "Exclude")
Operator:
=
Value:
Include
This assumes a string parameter Count populated with the above values.
This works by calculating the parameter and field combinations to produce a constant, either Include or Exclude, then displaying all rows that return Include.
As mentioned in a comment, it's difficult to follow exactly what you're asking here. I've done my best but if you have more questions it would be best to update the question with some sample data and how you'd like this data displayed.

Alternate rows background color in a group

I have following situation:
Report with nested groups, whether group column has value 1 or 2. Next child group has 2 groups for each parent group, so for parent group 1 there is two child groups (1-1 and 1-3), and also for parent group 2 I have two child groups (2-2 and 2-4).
My question is, how to set color to WhiteSmoke for rows from 1-1, and color White for rows from 1-3, and again WhiteSmoke for 2-2 and White for 2-4.
If there is order like 1, 2, 3, ..., then will be easy with using Mod operator, but my order is 1, 3, 2, 4 and i cannot find algorithm for alternating color
EDITED
I found the answer.
=IIF(RunningValue("NameofGrouptoAlternateColor", CountDistinct, "NameofParentGroup") Mod 2, "White", "Wheat")
This worked for me and I think it's what the original poster was trying to accomplish.
=IIF(RunningValue(Fields!FieldToGroupOn.Value, CountDistinct, "ParentGroupName") Mod 2, "LightGrey", "Transparent")
Where:
FieldToGroupOn is the field in the dataset you are grouping on and wish to produce alternating colors.
ParentGroupName is the name of the row group in the tablix that is the parent of the group you wish to alternate colors for.
assuming that this is the order: 1,3,2,4,5,7,6,8... you want to color only 3,4,7,8..
Then the expression should be:
=iif(RowNumber(Nothing) Mod 4 <> 0 AND
((RowNumber(Nothing)+1) Mod 4 <> 0), "White", "WhiteSmoke")
If I understood correctly you want to alternate colors on "group change".
If so, this is the solution:
IIf(RowNumber("TheNameOfYourGroup") Mod 2 = 0, "White", "WhiteSmoke")
That's the expression you use for your Background Color property.
I've forced to use custom code to achieve your goal, here is my solution:
Custom code section
Public Shared ReverseLookup = True
Public Function GetColor(ByVal currentValue As Integer, ByVal previosValue As Integer) As String
If ReverseLookup = True
If currentValue = previosValue Then
GetColor = "Gray"
Else
GetColor = "Green"
ReverseLookup = False
End If
Else
If currentValue = previosValue Then
GetColor = "Green"
Else
GetColor = "Gray"
ReverseLookup = True
End If
End If
End Function
And in the BackgroundColor property :
=Code.GetColor(Fields!secondid.Value, Previous(Fields!secondid.Value))
input parameters are current secondid value (value from paren group of details) and previos secondid value.
And here my result:
I believe that it is exactly what you need :)