tsql conditional maths equation - sql-server

I need to perform some summations during a select query however depending on 2 values i will need to perform a different equation. hopefully an example will demonstrate
basically i need to perform the following summations
if x > y then (x - y + z) or
if x < y then (x - x + z) basically i am setting this to 0.
So far i thought that i could use 2 tables to dump the x > y values and the x < y values and then perform the relevant equations.
any ideas

You can use a case expression.
select case
when x > y then x - y + z
when x < y then x - x + z
else 0 -- x = y
end
from YourTable

Related

How does half of the dimension give the number of layers of NxN matrix?

Assume a matrix of dimension N*N
It seems that to figure out the layers it is composed of the number of them is N/2 but although I can verify it, somehow I can not conceptually connect how does the half of the N gives this layer number.
Example:
4x4 => 4/2 = 2 layers
x x x x
x x x x
x x x x
x x x x
layers:
x x x x
x x x x
x x x x
x x x x
Can someone help me unblock on this?
For even N
Focus on a middle row of the matrix. The layers in the following example (N=6) are denoted with x, y, and z for clarity.
x x x x x x
x y y y y x
x y z z y x <- For example, this row
x y z z y x
x y y y y x
x x x x x x
Because you're in the middle, you will go through every layer. In fact, you'll "enter" every layer once and "exit" that layer later. Each time you enter or exit a layer, there is one element of the matrix. For instance, in the above example, going from left to right we have:
x: enter layer x
y: enter layer y
z: enter layer z
z: exit layer z
y: exit layer y
x: exit layer x
As you can see, we traversed N elements on the row, and each layer needs to be entered and exited, so we deduce that there are N/2 layers.
For odd N
If N is odd, the reasoning is mostly the same, except that the innermost layer (which is just one element) is "entered" and "exited" at the same time. The number of layers is (N+1)/2. We can derive this by temporarily ignoring the innermost layer. The number of elements in the row (ignoring the innermost layer) is N-1, we divide by 2 to get the number of layers (ignoring the innermost layer), and we add 1 (to account for the innermost layer). Then (N-1)/2 + 1 = (N+1)/2.

Variable elimination in Bayes Net on a node with single child

If we have a node X, that has a child Y in a Bayes net, why is it correct to express P(Y) as P(Y|X)P(X)? Does it then follow that X is a necessary condition for Y?
Bayes Networks
An edge in a Bayes Network means the variable is conditionally dependent. If nodes are not connected by any path, they are conditionally independent.
Having a node X with a child Y means you need to learn:
Given X is True, what is the probability of Y being True?
Given X is False, what is the probability of Y being True?
More generally: If X can have n values and Y can have m values, then you have to learn n * (m - 1) values. The - 1 is there because the probabilities need to sum up to 1.
Example
Let's stick with the simple case that both variables are binary and use the following from Wikipedia:
Say X is RAIN and Y is SPRINKLER. You want to express Y (SPRINKLER) in terms of X (RAIN).
The Bayes theorem states:
P(Y|X) = P(X|Y) * P(Y) / P(X)
<=> P(Y) = P(Y | X) * P(X) / P(X | Y)
Now we apply the Law of total probability for X. This means, for X we simply go through all possible values:
P(Y) = P(Y | X = true) * P(X = true) +
P(Y | X = false) * P(X = false)
I guess this is what you refer to. P(X=true | Y) = 1, because the X=true means we already know that X=true happened. It doesn't matter what Y is.
To continue our case, we now look up the values in the tables (X is RAIN, Y is SPRINKLER):
P(Y) = 0.01 * 0.2 + 0.4 * 0.8
= 0.322

How to iterate upwards and downwards to infinity with one variable

I am just getting started with Prolog, and I am really stuck, I have been trying for days to get this to work for ages so really hope someone can help. I am being asked to make it so that if someone enters
userInput(X).
What will come out is
X=0; X=1; X=-1; X=2; X=-2;
and so on for infinity.
I am aware of how to do this infinitely upwards, using:
between(0, inf, X).
I am also aware of how to do this infinitely downwards, using:
example(X):- var(X), X=0.
example(X):- var(X), example(Y), X is Y-1.
I understand that this could also be done positively iterating upwards using + instead.
But to have X switch from positive to negative counting is not working at all.
Any help would be greatly appreciated.
You can use an accumulator that starts with 0, is updated throught the sequence and each time "emits" its result, so:
userInput(X) :-
userInput(0,X).
Now each time you unify X with the accumulator:
userInput(X,X).
the recursive case(s) of course has to determine the next item, and you can use the following guide:
if the number X is less than or equal to zero, the new X2 is X2 is -X+1; and
if the number X is greater than zero, the new X2 is X2 is -X.
You can write these as:
userInput(X,R) :-
X =< 0,
!,
X2 is -X+1,
userInput(X2,R).
userInput(X,R) :-
X2 is -X,
userInput(X2,R).
Or putting it all together:
userInput(X) :-
userInput(0,X).
userInput(X,X).
userInput(X,R) :-
X =< 0,
!,
X2 is -X+1,
userInput(X2,R).
userInput(X,R) :-
X2 is -X,
userInput(X2,R).
This generates:
?- userInput(X).
X = 0 ;
X = 1 ;
X = -1 ;
X = 2 ;
X = -2 ;
X = 3 ;
X = -3 ;
X = 4 ;
X = -4 ;
X = 5 ;
X = -5 ;
X = 6 ;
X = -6 ;
X = 7 ;
X = -7
Alternative:
an alternative that is even easier is simply first stating that 0 is a userInput/1:
userInput(0).
and then use an accumulator for the other cases that starts with 1:
userInput(X) :-
userInput(1,X).
Now each iteration, you unify X as a result, and -X as a result, and in the recursive case, you increment X. So:
userInput(X,X).
userInput(X,NX) :-
NX is -X.
userInput(X,R) :-
X1 is X+1,
userInput(X1,R).
Or putting it all together:
userInput(0).
userInput(X) :-
userInput(1,X).
userInput(X,X).
userInput(X,NX) :-
NX is -X.
userInput(X,R) :-
X1 is X+1,
userInput(X1,R).

Supress/remove repeated values in Jdeveloper/ADF Table column

I need to supress the repeated values in a table so it will look like:
x y z
- - u
- b z
- - u
y y z
- - u
instead:
x y z
x y u
x b z
x b u
y y z
y y u
In Oracle OBI I can do that editing column properties of a table:
But I can't find this option in column/table properties from Jdeveloper, there is a way to do this?
You can't do this in a table.
You can use a pivot table to do this.
Or you can use an af:iterator to create this type of UI.

How to define some variables as non-commutative in Maxima

For example, I'd like to define x and y as non-commutative, and a and b as commutative (as usual). In other words,
x y ≠ y x, a x = x a, a b = b a .
Further,
(x + a y) (x - a y) = x^2 + a (y x - x y) - a^2 y^2 .
What is a code for defining x and y, and a symbol for multiplication (such as * and . ) ?
You can work with Maxima's commutative * and non-commutative . products in the way that you want by following the next two steps:
Declare the symbols a and b as scalars:
declare([a, b], scalar)$
Enable dotscrules:
dotscrules: true$
This simplifies non-commutative products involving scalars to commutative products (i.e., a.x becomes a*x).
Now you are ready. For example,
expand((a*x + b*y) . (a*x - b*y))
returns
a*b*y.x - b^2*y^^2 - a*b*x.y + a^2*x^^2
(note that ^^ is the non-commutative exponentiation operator).

Resources