Can I use case expression to build where like this?
select *
from table
where
case
when x=y then z= j and t=v
when x=k then q= p and s=l
end
;
I need change where clause depending on the value of x variable.
Use or:
select *
from table
where (x = y and z = j and t = v) or (x = k and q = p and s = l);
An alternative to using OR is to use nested CASE statements:
SELECT *
FROM table_name
WHERE CASE
WHEN x = y THEN CASE WHEN z = j AND t = v THEN 1 ELSE 0 END
WHEN x = k THEN CASE WHEN q = p AND s = l THEN 1 ELSE 0 END
ELSE 0
END = 1;
or you could simplify it to:
SELECT *
FROM table_name
WHERE CASE
WHEN x = y AND z = j AND t = v THEN 1
WHEN x = k AND q = p AND s = l THEN 1
ELSE 0
END = 1;
However, you should check whether Oracle can use column indexes or if a separate function-based index is required with these solutions.
I'm trying the hacker rank type of triangle below where based on the 3 sides it has to be determined if the triangle is equilateral, isosceles, scaelene, or not a triangle.
https://www.hackerrank.com/challenges/what-type-of-triangle/problem
I'm not sure why the code below isn't passing the test case. Unfortunately, I can't download the test case to see why it isn't working.
SELECT CASE WHEN A = B AND B = C AND A = C THEN 'Equilateral'
WHEN (A = B AND B != C AND A != C) OR (B = C AND A != B AND A != C) OR (A = C AND A != B AND B != C) THEN 'Isosceles'
WHEN ((A + B) < C) OR ((B + C) < A) OR ((C + A) < B) THEN 'Not a triangle'
ELSE 'Scalene' END
FROM Triangles
Try something like this:
SELECT
CASE
WHEN A + B > C AND A + C > B AND B + C > A THEN
CASE
WHEN A = B AND B = C THEN 'Equilateral'
WHEN A = B OR B = C OR A = C THEN 'Isosceles'
ELSE 'Scalene' END
ELSE 'Not A Triangle' END
FROM TRIANGLES
Only test for the type of triangle when it is a triangle.
Try this :
SELECT CASE WHEN A + B > C AND A+C>B AND B+C>A THEN
CASE WHEN A = B AND B = C THEN 'Equilateral'
WHEN A = B OR B = C OR A = C THEN 'Isosceles'
WHEN A != B OR B != C OR A != C THEN 'Scalene'
END
ELSE 'Not A Triangle' END FROM TRIANGLES;
The condition for scalene triangle does not include the following condition in your original query : Side 1 + Side 2 <= Side 3
Function quadratic (a As double, b As double, c As double) As Double
Dim x,y As Double
if (a = 0) then
Console.Writeline("no solution for a = 0")
else if
((b * b - 4 * a * c) <0)
Console.Writeline("no real solutions")
else
x = ((- b + Math.Sqrt(b * b - 4 * a * c)) / ( 2 * a))
y = ((- b - Math.Sqrt(b * b - 4 * a * c)) / ( 2 * a))
if (x > y) then
Console.Writeline(x)
else
Console.Writeline(y)
End if
End if
End Function
I am not going to check your math for you :-) but if you change this:
else if
((b * b - 4 * a * c) <0)
Console.Writeline("no real solutions")
To this:
ElseIf ((b * b - 4 * a * c) < 0) Then
Console.Writeline("no real solutions")
It compiles and runs...
This is my Octave code
for K= 1:10
while ( p < 1 )
ceil(log2(K)) + 1/(1-(1-p)^K) %function
p = p + sens;
K
endwhile;
endfor
K
and here is an output:
ans = 10.000
K = 1
ans = 5.0000
K = 1
ans = 3.3333
K = 1
ans = 2.5000
K = 1
ans = 2
K = 1
ans = 1.6667
K = 1
ans = 1.4286
K = 1
ans = 1.2500
K = 1
ans = 1.1111
K = 1
ans = 1
K = 1
K = 10
So, as you can see -- in inner while statement value of K is fixed to 1. What I am supposed to do to vary this value between 1 and 10. Why it is not working? I have no idea why this inner while statement is proceed only once.
ANSWER: There should be p= initial_value after for K=...
There should be p= initial_value after for K=...
That is, like this:
for K = 1:10
p = somevalue;
while ( p < 1 )
...
Is there a very simple algorithm to figure out which of 4 numbers is the greatest?
var lst = new List<int>() { 1, 7, 3, 4 };
var max = lst.Max();
I got no VB, but you get the idea.
If they are in an array, something like this should work:
VB:
Dim ar As Integer() = {3, 6, 9, 12}
Dim largest As Integer = ar(0)
For i As Integer = 1 To ar.Length - 1
If ar(i) > largest Then
largest = ar(i)
End If
Next
C#:
int[] ar = {3, 6, 9, 12};
int largest = ar[0];
for(int i = 1; i < ar.Length; i++) {
if(ar[i] > largest) {
largest = ar[i];
}
}
If you're using a language that supports some sort of max function or array sorting definitely use those features. Or choose any of the other sane answers in this thread. However, just for fun:
maximum = (var1 > var2 ? var1 : var2) > (var3 > var 4 ? var3 : var 4) ?
(var1 > var2 ? var1 : var2) :
(var3 > var 4 ? var3 : var 4);
Put the numbers into an array, sort the array, then select the one whose index is array length -1.
Or you could put the numbers into an array, sort the array, reverse the array, and then select index 0.
If you need to write your own sorting algorithm, the simplest one to implement is likely to be the bubble sort.
With VB.Net you could the following and it will work for any number of numbers
Public Function Max(ParamArray items As Integer()) As Integer
if items.Length = 0 Then
throw New ArgumentException("need at least 1 number")
End IF
return items.Max()
End Function
Then you can now do
Max(1,2,3,4)
There are plenty of ways you could do this.
A really naive approach would be:
#Pseudocode
If number1 > number2 and number1 > number3 and number1 > number4: return number1
Else if number2 > number3 and number2 > number4: return number2
Else if number3 > number4: return number3
Else: return number4
It's more practical to use arrays but if you're starting that could be more complicated than simple if blocks.
If they're in an array - and doing it explicitly rather than using sort:
int max = int.MinValue; // i.e. the "largest" negative number
int largest = -1;
for (int index = 0; index < array.Length; index++)
{
if (array[index] > max)
{
max = array[index];
largest = index;
}
}
The greatest value will be max and it's index in largest.
Nate's answer is more efficient as it uses the first element of the array as the initial value. So the first three lines of my solution would become:
int max = array[0];
int largest = 0;
for (int index = 0; index < array.Length; index++)
Get A, B, C, D from user;
largest = A;
if B > largest then
Largest = B;
if C > largest then
Largest = C;
if D > largest then
largest = D;
Print largest
In Java, if a is an int[4]:
Math.max(Math.max(a[0], a[1]), Math.max(a[2], a[3]))
Dim MyValues As New List(Of Double)
Dim MaxValue As Double
Dim tValue As Double
MyValues.Add(12.58)
MyValues.Add(3.58)
MyValues.Add(518.6)
MyValues.Add(244)
MyValues.Add(31.25)
For Each tValue In MyValues
If MaxValue < tValue Then
MaxValue = tValue
End If
Next
MsgBox(MaxValue)
My first question would be why? Second would be, if it's only four numbers then it really doesn't matter. Whatever takes your fancy. I personally would go with the fewest lines of code. Which would be to use the built in array.Sort method, then take the last item.
I would also consider using LINQ, just because you can.
Or Math.Max in a nasty nested way so Math.Max(Number1,Math.Max(Number2,Math.Max(Number3,Number4))))
If there could be hundreds of numbers, then I would try and pick a better algorithm. Probably the one suggested by #ChrisF, although it would depend on where the numbers are coming from, EG a database could find the max much easier, or if the numbers are being read from somewhere sequentially then you could store the max as you read the numbers.
this is my own analization. i made this code to display the lowest and highest numbers among the 4 inputted numbers from the textbox,. it will display the lowest and highest to appointed labels. if u input two same lowest or highest numbers, a msgbox appear to notify u somehow that u inputted same highest or lowest numbers and it displays back to its appropriate label. i used labels for the display of lowest and highest. here's my fb: iver saladaga anoos , 2ndyear student of JHCSC tambulig, zamboanga del sur, philippines..
so here it is! it worked for me. im using vb6 enterprise edition. :)
Private Sub Command1_Click()
Dim A, B, C, D As Long
A = Text1.Text
B = Text2.Text
C = Text3.Text
D = Text4.Text
If A < B And A < C And A < D Then
Label9.Caption = A
Else
If A > B And A > C And A > D Then
Label10.Caption = A
End If
End If
If A < B And A < D And A < C Then
Label9.Caption = A
Else
If A > B And A > D And A > C Then
Label10.Caption = A
End If
End If
If A < C And A < B And A < D Then
Label9.Caption = A
Else
If A > C And A > B And A > D Then
Label10.Caption = A
End If
End If
If A < C And A < D And A < B Then
Label9.Caption = A
Else
If A > C And A > D And A > B Then
Label10.Caption = A
End If
End If
If A < D And A < C And A < B Then
Label9.Caption = A
Else
If A > D And A > C And A > B Then
Label10.Caption = A
End If
End If
If A < D And A < B And A < C Then
Label9.Caption = A
Else
If A > D And A > B And A > C Then
Label10.Caption = A
End If
End If
If B < C And B < A And B < D Then
Label9.Caption = B
Else
If B > C And B > A And B > D Then
Label10.Caption = B
End If
End If
If B < C And B < D And B < A Then
Label9.Caption = B
Else
If B > C And B > D And B > A Then
Label10.Caption = B
End If
End If
If B < A And B < C And B < D Then
Label9.Caption = B
Else
If B > A And B > C And B > D Then
Label10.Caption = B
End If
End If
If B < A And B < D And B < C Then
Label9.Caption = B
Else
If B > A And B > D And B > C Then
Label10.Caption = B
End If
End If
If B < D And B < C And B < A Then
Label9.Caption = B
Else
If B > D And B > C And B > A Then
Label10.Caption = B
End If
End If
If B < D And B < A And B < C Then
Label9.Caption = B
Else
If B > D And B > A And B > C Then
Label10.Caption = B
End If
End If
If C < A And C < B And C < D Then
Label9.Caption = C
Else
If C > A And C > B And C > D Then
Label10.Caption = C
End If
End If
If C < A And C < D And C < B Then
Label9.Caption = C
Else
If C > A And C > D And C > B Then
Label10.Caption = C
End If
End If
If C < B And C < A And C < D Then
Label9.Caption = C
Else
If C > B And C > A And C > D Then
Label10.Caption = C
End If
End If
If C < B And C < D And C < A Then
Label9.Caption = C
Else
If C > B And C > D And C > A Then
Label10.Caption = C
End If
End If
If C < D And C < A And C < B Then
Label9.Caption = C
Else
If C > D And C > A And C > B Then
Label10.Caption = C
End If
End If
If C < D And C < B And C < A Then
Label9.Caption = C
Else
If C > D And C > B And C > A Then
Label10.Caption = C
End If
End If
If D < A And D < B And D < C Then
Label9.Caption = D
Else
If D > A And D > B And D > C Then
Label10.Caption = D
End If
End If
If D < A And D < C And D < B Then
Label9.Caption = D
Else
If D > A And D > C And D > B Then
Label10.Caption = D
End If
End If
If D < B And D < A And D < C Then
Label9.Caption = D
Else
If D > B And D > A And D > C Then
Label10.Caption = D
End If
End If
If D < B And D < C And D < A Then
Label9.Caption = D
Else
If D > B And D > C And D > A Then
Label10.Caption = D
End If
End If
If D < C And D < B And D < A Then
Label9.Caption = D
Else
If D > C And D > B And D > A Then
Label10.Caption = D
End If
End If
If D < C And D < A And D < B Then
Label9.Caption = D
Else
If D > C And D > A And D > B Then
Label10.Caption = D
End If
End If
Command2.Enabled = True
If A = D And A > C And A > B Then
MsgBox "Same highest numbers (" + A + ") is inputted."
highest = A
Label10.Caption = highest
Else
If A = D And A < C And A < B Then
MsgBox "Same lowest numbers (" + A + ") is inputted."
lowest = A
Label9.Caption = lowest
End If
End If
If A = B And A > C And A > D Then
MsgBox "Same highest numbers (" + A + ") is inputted."
highest = A
Label10.Caption = highest
Else
If A = B And A < C And A < D Then
MsgBox "Same lowest numbers (" + A + ") is inputted."
lowest = A
Label9.Caption = lowest
End If
End If
If B = D And B > A And B > C Then
MsgBox "Same highest numbers (" + B + ") is inputted."
highest = B
Label10.Caption = highest
Else
If B = D And B < A And B < C Then
MsgBox "Same lowest numbers (" + B + ") is inputted."
lowest = B
Label9.Caption = lowest
End If
End If
If B = C And B > D And B > A Then
MsgBox "Same highest numbers (" + B + ") is inputted."
highest = B
Label10.Caption = highest
Else
If B = C And B < D And B < A Then
MsgBox "Same lowest numbers (" + B + ") is inputted."
lowest = B
Label9.Caption = lowest
End If
End If
If C = A And C > D And C > B Then
MsgBox "Same highest numbers (" + C + ") is inputted."
highest = C
Label10.Caption = highest
Else
If C = A And C < D And C < B Then
MsgBox "Same lowest numbers (" + C + ") is inputted."
lowest = C
Label9.Caption = lowest
End If
End If
If C = D And C > B And C > A Then
MsgBox "Same highest numbers (" + C + ") is inputted."
highest = C
Label10.Caption = highest
Else
If C = D And C < B And C < A Then
MsgBox "Same lowest numbers (" + C + ") is inputted."
lowest = C
Label9.Caption = lowest
End If
End If
End Sub
Private Sub Command2_Click()
Text1.Text = Clear
Text2.Text = Clear
Text3.Text = Clear
Text4.Text = Clear
Label9.Caption = Clear
Label10.Caption = Clear
Command2.Enabled = False
Command1.Enabled = False
Text1.SetFocus
End Sub
Private Sub Command3_Click()
End
End Sub
Private Sub Form_Load()
Command2.Enabled = False
Command1.Enabled = False
End Sub
Private Sub Text1_Change()
Command1.Enabled = True
End Sub