I have equation and I need to extract S1 value considering S2 = (M * S1) + I mod Y - module

Having this formula S2 = (M * S1) + I mod Y, how can I obtain S1 considering I have all the rest of variables available ?
Thanks.

Related

IBM DB2: SQL subtraction doesn't work in denominator

I am working with manufacturing costs and the value of the fabrication still left to go (the "Net Work In Process"). The SQL is straight arithmetic but the query doesn't result in a value if there is a minus sign (subtraction) in the denominator. The database columns:
A = Material issued cost
B = Miscellaneous cost adds
C = Labor
D = Overhead
E = Setup cost
F = Scrap cost
G = Received cost (cost of assemblies completed already)
H = Original Quantity ordered
I = Quantity deviation
J = Quantity split from order
K = Quantity received (number of assemblies completed already)
The Net WIP cost is nothing more than the total cost remaining divided by the total quantity remaining. So in short, I'm simply trying to do this:
select (A + B + C + D + E - F - G) / (H + I - J - K) from MyTable
The subtractions work fine in the numerator but as soon as I subtract in the denominator, the query simply returns no value (blank). I've tried stuff like this:
select (A + B + C + D + E - F - G) / (H + I - (J + K)) from MyTable
select (A + B + C + D + E - F - G) / (H + I + (-J) + (-K)) from MyTable
select (A + B + C + D + E - F - G) / (H + I + (J * -1) + (K * -1)) from MyTable
None of these work. Just curious if anyone has come across this on IBM's DB2 database?
Thanks.
If you are returning "blank" in a numeric calculation, then you have a NULL value somewhere. Try using coalesce():
nullif(coalesce(H, 0) + coalesce(I, 0) - coalesce(J, 0) - coalesce(K, 0), 0)
You have nulls in one of the columns H, I, J, or K. Search for the offending rows using:
select H, I, J, K
from MyTable
where H is null
or I is null
or J is null
or K is null;
Then, you can treat those special cases according you your own logic. Typically you'll replace those nulls with zeroes or other values using COALESCE().
Thanks all for your comments. I did scour the columns for NULLs and there aren't any. There are then plenty of conditions where, let's say, the factory sets the order to 10 and completes (receives) 10 with no splits and no deviation. In that case:
H + I + J + K = 0 (+10 +0 -0 -10) = 0
and I can't divide by zero. So I have a different workaround for that and thanks for everyone's help.

Time complexity for the following loop of code?

I just need someone to explain one line of code for me, I don't really understand.
* this is just pseudocode
m: = 1, l := 0, s:= 0:
while m <= n do
for j = n-m to n do :
l:= l + 1
od
for j = 1 to [log n] do
s: = s +1
od
m := 3m
od
I understand the second for loop is log n time, the while loop is log base 3 n time but I am confused on the first for loop? Can someone explain, is that just o(n)? What does j = n-m really do?
The first for loop runs from n-m to n. So it has m iterations. In the last iteration of the while loop m is basically n so it runs linear in n. But the overall complexity is better than O(n log n)
In total you have this many iterations
(1 + log n) + (3 + log n) + (9 + log n) + ... + (n + log n) ( log_3 n terms )
= log_3 n * log n + (1 + 3 + 9 + ... + n) ( n can be written as 3^(log_3 n) )
= log_3 n * log n + (3^0 + 3^1 + 3^2 + ... + 3^(log_3 n)) (see this as a base 3 number. 11 is smaller than 100)
<= log_3 n * log n + 3^(1 + log_3 n)
= log_3 n * log n + 3 * n
So the dominating term is n and thus the overall complexity is O(n).

Time complexity of the algorithm by counting the comparisons and we need to give a big o estimate

Time complexity of the algorithm by counting the comparisons and we need to give a big o estimate
How can we do?
for i := 1 to m // Loop 1
for j:= 1 to n // Loop 2
cij := 0
for q := 1 to k // Loop 3
cij := cij + aiqbqj
return C
Note that within loop 2, there are exactly k + 1 assignments, so while j loops from 1 to n, there are in total n * (k + 1) assignments.
Adding on top of that, while i loops from 1 to m there are in total m * n * (k + 1) assignments.
So the time complexity of this code is O(m * n * (k + 1)) = O(mnk).

How to create letter sequence?

How to create letter sequence in SQL Server ? Like A,B,C....Z,AA,AB... I refer this
Given Letter, Get Next Letter in Alphabet link but it will not work after letter Z.How to do this?
You can just use arithmetic. This is going to assume that you have numbers in a table (however you want to generate them):
select (case when (n / 26) % 27 = 0 then ''
else char(ascii('A') + (n / 26) % 27 - 1)
end) +
char(ascii('A') + n % 26)
from numbers n;
For instance:
select (case when (n / 26) % 27 = 0 then ''
else char(ascii('A') + (n / 26) % 27 - 1)
end) +
char(ascii('A') + n % 26)
from (select top 100 row_number() over (order by (select null)) - 1 as n
from master..spt_values
) n;
Here is a SQL Fiddle. Longer sequences can be generated using the same logic.
An alternative method is to put together a sequence of letters and then just cross join to put them together:
with letters as (
select top 26
char(ascii('A') + row_number() over (order by (select null)) - 1) as letter
from master..spt_values
)
select l1.letter as seq
from letters l1
union all
select l1.letter + l2.letter
from letters l1 cross join letters l2
union all
select l1.letter + l2.letter + l3.letter
from letters l1 cross join letters l2 cross join letters l3
order by len(seq), seq;
This is a bit challenging because the sequences have different lengths.
Here is a SQL Fiddle illustrating this approach.
here we use generate_series() function to generate a series of number.Specify numbers from where to where you need to generate sequence of numbers. And chr() fucntion returns the ASCII value of those number
from
select chr(generate_series(65,90));

Need SQL Server Query to solve 3rd Order Polynomial Regression

Can anyone help with some SQL query code to provide estimates of the co-efficients for a 3rd order Polynomial regression?
Please assume that I have a table of X and Y data values and want to estimate a, b and c in:
Y(X) = aX + bX^2 + cX^3 + E
APPROXIMATE but fast solution would be to sample 4 representative points from the data and solve the polynomial equation for these points.
As for the sampling, you can split the data into equal sectors and compute average of X and Y for each sector - the split can be done using quartiles of X-values, averages of X-values, min(x)+(max(x)-min(x))/4 or whatever you think is the most appropriate.
To illustrate the sampling by quartiles (i.e. by row numbers):
As for the solving, i used numberempire.com to solve these* equations for variables k,a,b,c:
k + a*X1 + b*X1^2 + c*X1^3 - Y1 = 0,
k + a*X2 + b*X2^2 + c*X2^3 - Y2 = 0,
k + a*X3 + b*X3^2 + c*X3^3 - Y3 = 0,
k + a*X4 + b*X4^2 + c*X4^3 - Y4 = 0
*Since Y(X) = 0 + ax bx^2 + cx^3 + ϵ implicitly includes [0, 0] point as one of the sample points, it would create bad approximations for data sets that don't include [0, 0]. I took the liberty of solving Y(X) = k + ax bx^2 + cx^3 + ϵ instead.
The actual SQL would go like this:
select
-- returns 1 row with columns labeled K, A, B and C = coefficients in 3rd order polynomial equation for the 4 sample points
-(X1*(X2p2*(X3p3*Y4-X4p3*Y3)+X2p3*(X4p2*Y3-X3p2*Y4)+(X3p2*X4p3-X3p3*X4p2)*Y2)+X1p2*(X2*(X4p3*Y3-X3p3*Y4)+X2p3*(X3*Y4-X4*Y3)+(X3p3*X4-X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y4)+(X3*X4p3-X3p3*X4)*Y2+(X2*(X4p3-X3p3)-X3*X4p3+X3p3*X4+X2p3*(X3-X4))*Y1)/(X1*(X2p2*(X4p3-X3p3)-X3p2*X4p3+X3p3*X4p2+X2p3*(X3p2-X4p2))+X2*(X3p2*X4p3-X3p3*X4p2)+X1p2*(X3*X4p3+X2*(X3p3-X4p3)+X2p3*(X4-X3)-X3p3*X4)+X2p2*(X3p3*X4-X3*X4p3)+X1p3*(X2*(X4p2-X3p2)-X3*X4p2+X3p2*X4+X2p2*(X3-X4))+X2p3*(X3*X4p2-X3p2*X4)) as b,
(X1*(X2p2*(Y4-Y3)-X3p2*Y4+X4p2*Y3+(X3p2-X4p2)*Y2)+X2*(X3p2*Y4-X4p2*Y3)+X1p2*(X3*Y4+X2*(Y3-Y4)-X4*Y3+(X4-X3)*Y2)+X2p2*(X4*Y3-X3*Y4)+(X3*X4p2-X3p2*X4)*Y2+(X2*(X4p2-X3p2)-X3*X4p2+X3p2*X4+X2p2*(X3-X4))*Y1)/(X1*(X2p2*(X4p3-X3p3)-X3p2*X4p3+X3p3*X4p2+X2p3*(X3p2-X4p2))+X2*(X3p2*X4p3-X3p3*X4p2)+X1p2*(X3*X4p3+X2*(X3p3-X4p3)+X2p3*(X4-X3)-X3p3*X4)+X2p2*(X3p3*X4-X3*X4p3)+X1p3*(X2*(X4p2-X3p2)-X3*X4p2+X3p2*X4+X2p2*(X3-X4))+X2p3*(X3*X4p2-X3p2*X4)) as c
from (select
samples.*,
-- precomputing the powers should give better performance (at least i hope it would)
power(X1,2) X1p2, power(X2,2) X2p2, power(X3,2) X3p2, power(X4,2) X4p2,
power(Y1,3) Y1p3, power(Y2,3) Y2p3, power(Y3,3) Y3p3, power(Y4,3) Y4p3
from (select
avg(case when sector = 1 then x end) X1,
avg(case when sector = 2 then x end) X2,
avg(case when sector = 3 then x end) X3,
avg(case when sector = 4 then x end) X4,
avg(case when sector = 1 then y end) Y1,
avg(case when sector = 2 then y end) Y2,
avg(case when sector = 3 then y end) Y3,
avg(case when sector = 4 then y end) Y4
from (select x, y,
-- splitting to sectors 1 - 4 by row number (SQL Server version)
ceiling(row_number() OVER (ORDER BY x asc) / count(*) * 4) sector
from original_data
)
) samples
)
According to developer.mimer.com, these optional features need to be enabled in SQL Server:
T611, "Elementary OLAP operations"
F591, "Derived tables"
SQL Server has a built-in ranking function NTILE(n) which will more easily create your sectors. I replaced:
ceiling(row_number() OVER (ORDER BY x asc) / count(*) * 4) sector
with:
NTILE(4) OVER(ORDER BY x ASC) [sector]
I also needed to add several "precomputed powers" to allow for the full column range as selected. The full list appears below:
POWER(samples.X1, 2) AS [X1p2],
POWER(samples.X1, 3) AS [X1p3],
POWER(samples.X2, 2) AS [X2p2],
POWER(samples.X2, 3) AS [X2p3],
POWER(samples.X3, 2) AS [X3p2],
POWER(samples.X3, 3) AS [X3p3],
POWER(samples.X4, 2) AS [X4p2],
POWER(samples.X4, 3) AS [X4p3],
POWER(samples.Y1, 3) AS [Y1p3],
POWER(samples.Y2, 3) AS [Y2p3],
POWER(samples.Y3, 3) AS [Y3p3],
POWER(samples.Y4, 3) AS [Y4p3]
Overall, great answer by #Aprillion! Well explained and the numberempire.com h/t was very helpful.