Inconsistency in sql query output using OR after WHERE - sql

I'm doing a query on a complex db:
SELECT *
FROM
table1,
table2,
table3,
table4,
table5,
table6,
table7,
table8
WHERE
a = b and
c = d and
e = d and
(
(strfldvar = 'BROKEN_ARROW' AND x = g)
OR (strfldvar = 'BROKEN_BOX' AND y = g)
) and
f = h and
i = j
Only works when strfldvar = 'BROKEN_BOX' and not when strfldvar = 'BROKEN_ARROW'. When I replace
(
(strfldvar = 'BROKEN_ARROW' AND x = g)
OR (strfldvar = 'BROKEN_BOX' AND y = g)
) and
with either x = g and or y = g and it works fine in two seperate queries runs like that. The error message for the case strfldvar = 'BROKEN_ARROW' is:
ORA-01013: user requested cancel of current operation
Before this error message comes the computer goes into deep thought for I guess 2 minutes.
What am I doing wrong here?
f.y.i. I looked at the names of the fields of the of the two seperate runs and they appear idendical. I mean the scema of the output looks the same for both. But I'm not 100% sure they are the same, if that matters i.e.
Thanks for your help

When strfldvar = 'BROKEN_ARROW' AND x = g (or if strfldvar is not BROKEN_ARROW or BROKEN_BOX), the y = g part is not evaluated, which seems to be causing the query to run for longer than you expect - until it's eventually killed by you, your client or resource limits. I suspect that's the only join condition for whichever table y is from, so you end up with a cartesian product.
When strfldvar = 'BROKEN_BOX' then both x = g and y = g will be evaluated, so you wouldn't get the same cartesian product, against either of the tables providing x and y.
If you are essentially deciding which table to include in the query based on that flag then you'll need to redesign this; possibly with a union of two queries, one which joins to x and the other on y; or with separate queries and you decide which to run; or maybe even with outer joins. But it depends on what you're really trying to do and what the data looks like. The code you have shown is a too generic to guess what will be appropriate.

Related

Power BI while loop

I'm trying to do a while loop in Power BI M language. But all of the logic are all over my head!
How would you translate a very simple loop like this into M language?
while X == True:
do abcdef
if Y == True:
end
Thanks very much!
Loops in M are probably best handled with the List.Generate function.
This article does a pretty good job at explaining how it works:
https://potyarkin.ml/posts/2017/loops-in-power-query-m-language/
Using this function, let's look at a more specific implementation of a while loop, say to find Fibonacci numbers less than 1000.
a = 1
b = 1
while b < 1000
b = a + b
a = b - a
would translate to M something like this:
let
data =
List.Generate(
() => [ a = 1, b = 1 ],
each [b] < 1000,
each [ b = [a] + [b], a = [b] ]
),
output = Table.FromRecords(data)[a]
in output
I'm not sure the best way to handle your break condition Y. It might depend on the specific problem.

Single Value Decomposition algorithm not working

I wrote the following function to perform SVD according to page 45 of 'the deep learning book' by Ian Goodfellow and co.
def SVD(A):
#A^T
AT = np.transpose(A)
#AA^T
AAT = A.dot(AT)
#A^TA
ATA = AT.dot(A)
#Left single values
LSV = np.linalg.eig(AAT)[1]
U = LSV #some values of U have the wrong sign
#Right single values
RSV = np.linalg.eig(ATA)[1]
V = RSV
V[:,0] = V[:,0] #V isnt arranged properly
values = np.sqrt(np.linalg.eig(ata)[0])
#descending order
values = np.sort(values)[::-1]
rows = A.shape[0]
columns = A.shape[1]
D = np.zeros((rows,columns))
np.fill_diagonal(D,values)
return U, D, V
However for any given matrix the results are not the same as using
np.linalg.svd(A)
and I have no idea why.
I tested my algorithm by saying
abs(UDV^T - A) < 0.0001
to check if it decomposed properly and it hasn't. The problem seems to lie with the V and U components but I can't see what's going wrong. D seems to be correct.
If anyone can see the problem it would be much appreciated.
I think you have a problem with the order of the eigenpairs that eig(ATA) and eig(AAT) return. The documentation of np.linalg.eig tells that no order is guaranteed. Replacing eig by eigh, which returns the eigenpairs in ascending order, should help. Also don't rearrange the values.
By the way, eigh is specific for symmetric matrices, such as the ones that you are passing, and will not return complex numbers if the original matrix is real.

SQL Distinct Count on Multiple Columns

I am new to SQL so don't know much about it. Please help.
I have a database something like this:
User Model
A X
A X
A X
B Y
C Y
C Y
D X
D X
E Z....
I want to calculate the frequency of the each unique model respective to unique users. I mean the output should be something like this:
Model Count
X 2
Y 2
Z 1
as A and D use model X, so X=2. Similarly B and C use model Y, so Y=2. and same goes for Z(only user E). How do I achieve this?
Use group by.
select model,count(distinct user)
from tbl
group by model

Name the output of an expression in Tensorflow

I wonder whether it's possible to give a name to the output of a certain expression to retrieve it from the graph at another part of the code.
For example
def A(a, b):
c = a + b
d = d * a
return d
For debug purposes it would be nice if I could pull out c at another position without returning it through the entire function hierarchy.
Any ideas?
Thanks in advance!
I'm assuming a and b are tensors.
Either you give a name to c using tf.identity
def A(a, b):
c = a + b
c = tf.identity(c, name = "c")
d = d * a
return d
Either you use the tf.add operation instead of +:
def A(a, b):
tf.add(a, b, name = "c")
d = d * a
return d
Either way, you get retrieve c with tf.get_variable('c:0') (You might need to precise the scope if any.)

Multy If Not Statment VB

If x = y And Not (z(0) = w(0) And z(1) = w(1) And z(2) = w(2)) then .....
I want to test if x = y and z(0) is not equal to w(0) and z(1) is not equal to w(0) and z(2) is not equal to w(2). Basicly if x=y and any of the rest is not equal to the other it should do the code in the if-statement
Will one And Not (....) work ?
Let me know if you have alternative solutions or if this will work thanks for the help
Your code is not doing what you say you want. It is checking if x = y and any of the other terms are unequal. Think about it, your if statement is asking if two things are true, the first is that x = y and the second is that the expression in parentheses is not true (and the expression in parentheses will not be true if and of the z and w terms are unequal).
If you want to check that all the other terms are unequal, you need
If x = y And z(0) <> w(0) And z(1) <> w(1) And z(2) <> w(2) then ...