IBM DB2: SQL subtraction doesn't work in denominator - sql

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.

Related

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).

SSRS - Median by Group - Jagged Array?

I'm faced with a challenge in two parts. I've been requested to replace 3 columns in a Matrix that aggregates on Name in the row group. The 3 columns are outside of the Column group.
Challenge 1 - Matrices want to summarize the Data pane. There seems to be no way to show the Raw Data (and then hide it, in order for these rows during runtime to populate an array).
Challenge 2 - I need to calculate the median BY name. This means either, during runtime, I need to calculate one median for each name at a time, reset the array, and start fresh for the next name value, OR, I need a multi-dimensional array where each ordinal is itself an array corresponding to a name.
I'm also a total code monkey at VB.
Here's what I've currently borrowed from an online post about calculating Median in SSRS.
Dim values As System.Collections.ArrayList
Function AddValue(ByVal newValue As Decimal)
If (values Is Nothing) Then
values = New System.Collections.ArrayList()
End If
values.Add(newValue)
End Function
Function GetMedian() As Decimal
Dim count As Integer = values.Count
If (count > 0) Then
values.Sort()
GetMedian = values(count / 2)
End If
End Function
I think I have a SQL solution that doesn't utilize a loop.
;WITH Counts AS
(
SELECT SalesPerson, c = COUNT(*)
FROM dbo.Sales
GROUP BY SalesPerson
)
SELECT a.SalesPerson, Median = AVG(0.+Amount)
FROM Counts a
CROSS APPLY
(
SELECT TOP (((a.c - 1) / 2) + (1 + (1 - a.c % 2)))
b.Amount, r = ROW_NUMBER() OVER (ORDER BY b.Amount)
FROM dbo.Sales b
WHERE a.SalesPerson = b.SalesPerson
ORDER BY b.Amount
) p
WHERE r BETWEEN ((a.c - 1) / 2) + 1 AND (((a.c - 1) / 2) + (1 + (1 - a.c % 2)))
GROUP BY a.SalesPerson;

How to force addition when a value is missing in SQL?

I want to do an addition in a select statement like this:
select (I + j + k) as total from MyTable ...
As expected, if any of the I, j, k is null, total is returned as null.
How do I code this select so that when any of the I, j, k is null (missing), the missing value is considered 0 for the purpose of addition (so that total is never null)? Thanks.
SELECT total = COALESCE(l,0) + COALESCE(j,0) + COALESCE(k,0)
FROM dbo.MyTable;
select ISNULL(I,0) + ISNULL(j,0) + ISNULL(k,0) as total from MyTable ...
or
select IFNULL(I,0) + IFNULL(j,0) + IFNULL(k,0) as total from MyTable ...
on mysql

Counting in a sql query

So I'm having a table like
Now I need to get this packed into a datagridview when a choice is made in a combobox filled with the uv ='owner'.
If I make a choice of the uv eg MG. I get a list of all his files/dosno he worked in and the times he spend working on the file.
I do this with this query :
SELECT kbpres.uv,
dbo.doss.dosno,
SUM(dbo.kbpres.uur) AS somuur,
SUM(dbo.kbpres.minuut) AS somminuut,
CAST (( SUM(dbo.kbpres.uur) + SUM(dbo.kbpres.minuut) / 60 ) AS VARCHAR(4)
) +
'u ' + CAST (( SUM(dbo.kbpres.minuut) % 60 ) AS VARCHAR(2)) + 'm' AS
[derivedColumn],
doss.behdr
FROM dbo.kbpres
INNER JOIN dbo.doss
ON dbo.kbpres.ino = dbo.doss.ino
WHERE ( dbo.kbpres.uv LIKE #cboBeheerder )
GROUP BY kbpres.uv,
dbo.doss.dosno,
doss.behdr
(Allthough I would only like to group by UV, and have to add the dosno and behdr as well ??)
The problem is now, how can I count the correct cost, as it is per record different.
for MG it would be :
10 * 60 for dosno 88888
20 * 76 for 66666
60*10 + (28hours+10minutes * 10) + 10*2 for 12345
Any idea if this is even possible ??
SELECT dosno,
SUM(uur)*60 + SUM(minuut) AS Time,
(SUM(uur)*60 + SUM(minuut)) * cost AS TotalCost
FROM dbo.kbpres k
INNER JOIN dbo.doss d ON k.ino = d.ino
GROUP BY dosno,k.ino,d.ino,cost
WHERE k.uv = 2
As cost seems to be a function of uv and dosno try
SELECT dosno,SUM(Time) AS Time,SUM(TotalCost) AS TotalCost FROM
(
SELECT dosno,
uur*60 + minuut AS Time,
(uur*60 + minuut) * cost AS TotalCost
FROM dbo.kbpres k
INNER JOIN dbo.doss d ON k.ino = d.ino
GROUP BY dosno,k.ino,d.ino,cost
WHERE k.uv = 2
) t
GROUP BY dosno