Applying ISNULL inside of a CASE expression - sql

I need to have a way to apply ISNULL in a CASE expression that will replace the NULL values with 0. The below code runs but still returns NULL values. Is there any way to accomplish this? There are two stock types - 'A' and blank, I am trying to get the sum of the quantity for each type.
I've tried using ISNULL inside the CASE
CASE WHEN MRP.stock_type = 'A'
THEN ISNULL(SUM(MRP.QUANTITY),0)
END AS 'Uncovered_Quantity',
CASE WHEN MRP.stock_type = ' '
THEN ISNULL(SUM(MRP.QUANTITY),0)
END AS 'Blank_Quantity',

I think you probably intended conditional aggregation:
SUM(CASE WHEN MRP.stock_type = 'A' THEN MRP.QUANTITY ELSE 0 END) as Uncovered_Quantity,
SUM(CASE WHEN MRP.stock_type = ' ' THEN MRP.QUANTITY ELSE 0 END) as Blank_Quantity,

Related

Using CASE or NVL

i would like to change code below by using SUM(NVL). any suggestion.
sum(CASE WHEN D.CURRENCY ='MYR' OR D.CURRENCY IS NULL THEN 1 ELSE 0 END) as Cur
previously i create this, but i cannot sum the 'CURRENCY' column which is 'MYR'. this column have null value.
NVL(D.CURRENCY,'MYR') CUR4
the red one i bold must display 'MYR'
First you have sum() function which actually counting the no of MYR in CURRENCY column rather than doing the sum
So, you just filter the NULLs by either COALESCE() or NVL()
SUM(CASE WHEN COALESCE(D.CURRENCY,'MYR') = 'MYR' THEN 1 ELSE 0 END) CUR4

adding condition when case statement is true in postgresql

I have a query that where i need to put condition when case statement is true.I have tried like this but not geeting the correct value.
SELECT
name,count(CASE WHEN date_part('year',time_stamp) = 2016 THEN answ_count end) AS Year15
FROM
companies companies
where
(CASE when no_answer='f' then value_s IS not NULL or value_n IS not NULL end )
SELECT name,
count(CASE WHEN date_part('year',time_stamp) = 2016 THEN answ_count end) AS Year15
FROM
companies companies
where
CASE when no_answer='f' then value_s ELSE '1' end IS not NULL
OR CASE when no_answer='f' then value_n ELSE '1' end IS not NULL
CASE is an expression, you can only specify a value after the THEN part, not a condition like you did THEN value_s IS NOT NULL
You can't use case like that.
It's hard to figure out what you are trying to do, but test the result of the case, rather than putting the test inside the case, like this:
...
where CASE when no_answer='f' then value_s else value_n end
IS not NULL

SQL Replacing NULL with 0 in a query

I'm trying to replace a Null value with a 0 and then do a calculation on this field, but not able to make it work. In all 4 of the examples below I'm still getting a Null value.
Any suggestions with how to write this?
Here's what I've tried:
select
ISNULL(posamt, 0) as total1,
coalesce (posamt, 0) as total2,
case when PosAmt = 0 then '0' else CONVERT(varchar(11), isnull(posamt, 0))end as total3,
CONVERT(varchar(11),isnull(posamt,0)) as total4
from mytable
I believe you need to have the ISNULL(posamt,0) in the CASE WHEN statements. It will never be equal to 0 when posamt is NULL.

Multiple SUM values using WHERE clauses in Oracle

I am writing a SQL statement against an Oracle 10g database. I want to obtain the SUM of a field with three different conditions. Can I do this with one query?
This is pseudo-SQL for what I want:
SELECT SUM(CP) AS CPTotal,
(SUM(CP) FROM tasks WHERE Code='P') AS CPProd,
(SUM(CP) FROM tasks WHERE Code='S') AS CPSupp
FROM tasks;
A conditional SUM() can be had via CASE statements:
SELECT SUM(CP) AS CPTotal,
SUM(CASE WHEN Code = 'P' THEN CP ELSE 0 END) AS CPProd,
SUM(CASE WHEN Code = 'S' THEN CP ELSE 0 END) AS CPSupp
FROM tasks;
The ELSE portion is not needed as NULL results when a value does not match any criteria in a CASE statement, and NULL is ignored on aggregation, but some prefer to include it.
You can use CASE to conditional check for the value of code.
SELECT SUM(CP) AS CPTotal,
SUM(CASE WHEN Code = 'P' THEN CP END) AS CPProd,
SUM(CASE WHEN Code = 'S' THEN CP END) AS CPSupp
FROM tasks

SQL case with different fields to check from same table

I have the following problem:
I have a select statement that includes a case part. Up til there it is easy the problem is that the case includes a check against another field in the same table.
select h.id,
case h.value
when 'P' then 'test'
when '' then 'failed'
when 'D' then 'passed'
else null end
as info,
b.text,
case h.diag
when h.value = '' [or 'failed' not sure tried both and didn't work]
else h.diag end
as diag1, h.date from valuetab h, texttab b where h.id=b.id
I want to have h.diag only to show values when h.value is not failed.
I always get the mistake that the = should be concat.. but that doesn't make sense in my eyes.
Any ideas??
Thats for all your help.
You can also write a case statement with your expression in a different place i.e.
SELECT CASE WHEN X = 1 THEN 'Y' WHEN X = 2 THEN 'Z'
I think what you want to do is something more like this:
SELECT CASE WHEN h.value = '' THEN h.diag end
Use the other form of case statement, which doesn't specify a column you want to look at:
select case
when column1 = 2 then 'Foo'
when other_column = 'blah' then 'Bar'
end
from table
The problem with using case column1 when... is that it implicitly compares column1 to each when clause. You can't then include a comparison to some other column in it.
You are missing a THEN portion of the WHEN clause, and specifying a condition where you could specify a value:
case h.value
when '' THEN NULL
else h.diag end
Ok got it....
after the 2nd case the "h.diag" must be removed....
so it is
case
when h.value = '' then null
else h.diag end
as diag1,