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
Related
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.
using SQL recursion, I am trying to get a formula from a given set of numbers.
Example, if only 1 number x is provided, the output should be x.
if 2 numbers x and y are provided, then output should be x + y + xy
if 3 numbers are provide, output should be x + y + z + xy + yz + zx + xyz
excerpt:
thanks.
Priya
I'm not 100% sure what you want the result set to really look like. But, you can do this using a recursive CTE:
with vals as (
select *
from (values ('x'), ('y'), ('z')) v(a)
),
cte as (
select convert(varchar(max), a) as a, convert(varchar(max), a) as max_a
from vals
union all
select convert(varchar(max), cte.a + vals.a) as a, convert(varchar(max), vals.a)
from cte join
vals
on vals.a > cte.max_a
)
select cte.a
from cte;
This produces the expressions on each row
Step1: I have a table called XYZ which contains following integer columns:
ID A B C
1 201507 20150810 20150311
2 20150812 201509 201510
I need to write a SQL query where if any values of A, B, and C is smaller than 8 digits then I need to consider it as 8 digits by adding zeros to the right of the value for step2 (I am not allowed to update the table.). For example:
ID A B C
1 20150700 20150810 20150311
2 20150812 20150900 20151000
How to add zeros to the right of the integer values through SQL query?
Step 2: I need to find for each record A<B, B<C or not. Please let me know how to write the query. I am using PostgreSQL. Thank you.
SELECT CAST(2015 AS VARCHAR(10))+REPLICATE('0',8-LEN(2015))
SELECT 2015 *(CAST('1'+REPLICATE('0',8-len(2015)) AS INT))
You can use rpad() to add trailing zeros, then cast the result back to an integer:
select id,
rpad(a::text, 8, '0')::int,
rpad(b::text, 8, '0')::int,
rpad(c::text, 8, '0')::int
from the_table;
To avoid repeating the expressions, use a derived table:
select *
from (
select id,
rpad(a::text, 8, '0')::int as a,
rpad(b::text, 8, '0')::int as b,
rpad(c::text, 8, '0')::int as c
from the_table
) t
where a < b or b < c --<< change that to the condition you want
just try this
select
ID,
A = LEFT(cast(a as varchar(100)+'00000000',8),
b = LEFT(cast(b as varchar(100)+'00000000',8),
C = LEFT(cast(c as varchar(100)+'00000000',8)
from xyz
Try this:
select cast(left(cast(A as varchar(20)) + '00000000', 8) as int) as [A],
cast(left(cast(B as varchar(20)) + '00000000', 8) as int) as [B],
cast(left(cast(C as varchar(20)) + '00000000', 8) as int) as [C]
from TABLE_NAME
If you want to avoid any casting, this might be solution:
select case when 8 - LEN(A) > 0 then A * Power(10, (8 - LEN(A))) else A end as [A],
case when 8 - LEN(B) > 0 then B * Power(10, (8 - LEN(B))) else B end as [B],
case when 8 - LEN(C) > 0 then C * Power(10, (8 - LEN(C))) else C end as [C]
from MY_TABLE
I have a table name Table and column name Item. Below is the data.
ABC123
ABC1234
ABC12345
HA11
K112
L1164
I need to remove the alphabets, replace them with leading 0, and the total character length must be 9. Below is the results.
000000123
000001234
000012345
000000011
000000112
000001164
I know how to change for ABC (certain alphabet set) only however I don't know to build the CASE statement. Below is what I have been successful.
select REPLICATE('0',9-LEN(A.B)) + A.B
from
(select replace(Item, 'ABC','') as B from Table) as A
I tried to combine CASE with SELECT and it doesn't seem like it.
Case when Item like '%ABC%' then
select REPLICATE('0',9-LEN(A.B)) + A.B
from
(select replace(Item, 'ABC','') as B from Table) as A
when Item like '%HA%' then
select REPLICATE('0',9-LEN(A.B)) + A.B
from
(select replace(Item, 'HA','') as B from Table) as A
when Item like '%K%' then
select REPLICATE('0',9-LEN(A.B)) + A.B
from
(select replace(Item, 'K','') as B from Table) as A
when Item like '%L%' then
select REPLICATE('0',9-LEN(A.B)) + A.B
from
(select replace(Item, 'L','') as B from Table) as A
else Item
End
Does anyone know how to achieve the result? I'm using SQL Server 2012.
Thank you.
I assumed, that you have letters only at the beginning of your data.
declare #s varchar(20) = 'ABS123'
-- we find index of first occurence of digit and then we cut out the letters
set #s = right(#s, len(#s) - patindex('%[0-9]%', #s) + 1)
-- here we just produce string with amount of zeros we need
select left('000000000', 9 - len(#s)) + #s
In terms of applying it to your table:
select left('000000000', 9 - len([Digits])) + [Digits] from (
select right([Item], len([Item]) - patindex('%[0-9]%', [Item]) + 1) as [Digits] from [Table]
)
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