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
Related
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,
Need to create a case statement for multiple criteria with varchar and date data types, my code sp far:
select
least(coalesce(case when 1.other_id=10 then 1.datestr else 0 end,
case when 2.info='Yes' then 2.date else 0 end))as date1,
from
test1 as 1
left join test2 as 2 on 1.id = 2.id
I essentially want to get the lowest date between 2 conditions. Both conditions have some filters (other_id=10 in first, info="Yes" in second). For those conditions I want to compare least dates.
I think this is the logic:
select (case when 1.other_id = 10 and 2.info = 'Yes'
then least(case when 1.other_id=10 then 1.datestr end,
case when 2.info='Yes' then 2.date end
)
when 1.other_id = 10 then 1.date
else 2.date
end)
Or, you can use more appropriate default values in your expression. Something like this (it might depend on the database):
select least(case when 1.other_id = 10 then 1.datestr else '9999-01-01' end,
case when 2.info = 'Yes' then 2.date else '9999-01-01' end)
) as date1,
I am trying to apply two conditions in one SQL Query.
(select DISTINCT (
CASE WHEN (
ABC.GemUserID = '99' )
OR ABC.GemUserID != '99'
THEN 'Yes'
ELSE 'No'
END)) AS AllWell
This gives me output as "Yes" where as the CASE is true only for 1 file like below :
Current Result:
99 , Yes
99 , Yes
99 , Yes
Expected Result:
99 , No
99 , No
99 , Yes
I am using the below query but the SQL Query Intellisence is identifying it as wrong.
Wrong Query:
(select DISTINCT (
CASE WHEN ( ABC.GEMUserID = '99' THEN 'Yes' else 'No'
CASE WHEN ( ABC.GEMUserID != '99' THEN 'No' else 'Yes'
END)) AS AllWell
After fixing the above Wrong Query:
(select DISTINCT
(CASE WHEN ABC.GemUserID = '99' THEN 'Yes' else 'No' END),
(CASE WHEN ABC.GemUserID != '99' THEN 'No' else 'Yes' END))
AS AllWell
But I am getting error:
Msg 116, Level 16, State 1, Line 17 Only one expression can be
specified in the select list when the subquery is not introduced with
EXISTS.
How to fix this?
select distinct is -- itself -- part of SQL syntax. The distinct is not a function. It should not be followed by parentheses. So, if I understand your question:
select DISTINCT
( CASE WHEN ABC.GEMUserID = '99' THEN 'Yes' else 'No' END),
( CASE WHEN ABC.GEMUserID <> '99' THEN 'No' else 'Yes' END) as AllWell
Do you plan on giving the first column a name?
select DISTINCT
CASE WHEN ABC.GEMUserID = '99' THEN 'Yes'
ELSE 'No' -- This is automatically When ABC.GEMUserID <> '99'
END AS AllWell
According to the error, your query is a subquery (probably behind IN?) in a larger SQL command. Therefore, it is not possible for such subquery to return more than one column.
So your first query, you've said:
CASE WHEN userID = 99 OR userID != 99
In other words:
CASE WHEN 1=1
This is why it returns yes for everything (not sure what the difference between your current and expected result should be considering that the userID is 99 for all rows).
For your erroneous query, seems you're returning that select in the middle of another select (since you alias it at the end). Due to that, you cannot return more than one column in your nested select. You do not need the second CASE statement, simply change your query to:
(select DISTINCT
CASE WHEN ABC.GemUserID = '99' THEN 'Yes' Else 'No' End) AS AllWell
Assuming that you hold the missing pieces to the query such as the FROM.
SELECT CASE WHEN name='AUTHUAT' then 'pramod' else 'ladkat' end from v$database;
output:
name
pramod
i Want output as :
__name____|_other name____
pramod | ladkat
To get the expected result you have to use the following query and you can avoid else part in the case if you want to display data in matched condition only. You can also set NULL or '' in else to display NULL or Blank value in case of mismatch:
SELECT
CASE WHEN name='AUTHUAT' THEN 'pramod' ELSE 'ladkat' END name,
CASE WHEN name='AUTHUAT' THEN 'ladkat' ELSE 'pramod' END other_name
FROM v$database;
You're maybe looking for conditional aggregation ?
SELECT MAX(CASE WHEN name='AUTHUAT' then 'pramod' else 'ladkat' end) as name,
MAX(CASE WHEN name='AUTHUAT' then 'pramod' else 'ladkat' end) as other_name
from v$database;
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,