when execute the following SQL in Oracle
SELECT (CASE
WHEN (CASE
WHEN (1=1)
THEN (1=1)
ELSE (1=0)
END)
THEN 1
ELSE 0
END) "m1"
FROM "mock_table_1" "t0";
it will throw the following error message
ORA-00907: missing right parenthesis
So what should I do if I want to use Case-When in another Case-When Condition?
(You can omit the parentheses ) FROM DUAL is just for test
SELECT CASE WHEN CASE
WHEN 1=1 THEN 1
ELSE 0
END =1 THEN 1
ELSE 0 END "m1"
FROM DUAL;
CASE returns a value. So after END you can do your next condition
Related
I am doing the following in a query as such
case WHEN PreAllocationDespatch IS NULL THEN 0 ELSE PreAllocationDespatch END AS 'Allocated'
case WHEN LineQuantity IS NULL THEN 0 ELSE LineQuantity END AS 'Ordered'
case WHEN LineQuantity IS NULL THEN 0 ELSE LineQuantity - PreAllocationDespatch END AS 'Balance'
Which is fine and does what its meant to do but I need to be able to say
Where Balance != LineQuantity
to give me a list of products that have not been actioned yet. But cause it is a dynamic column I can't appear to do that. The columns in question are both ints.
PreAllocationDespatch
LineQuantity
You have to do a second simple second select with your where statement or use a having if your query allows it (we don't have the full query so we wouldn't know)
You could use a subquery, CTE, or lateral join to define the values.
However, it might be simpler to express the logic as:
where coalesce(LineQuantity, 0) <> PreAllocationDespatch
for a resulting value you could use having
having Balance != LineQuantity
or repeat the case in where
where (case WHEN LineQuantity IS NULL THEN 0 ELSE LineQuantity - PreAllocationDespatch END ) != LineQuantity
Within a view I put put together a select statement within a case and delcared it as a column. The column name is 'IR2'
How can I case off of the column 'IR2'?
I end up getting an error which says 'Invalid Column Name 'IR2'.
What are my work around options?
case when r.ana = 'nh3' and r.serv='energy' and exists( select 1 from results x where x.no=r.no and x.ana='nh3' and x.sa='rejected' and x.serv <> 'energy')
then '*' else r.sa end as IR2,
CASE IR2 WHEN 'Released' then
''
ELSE
'*'
END AS IR
You can use a subquery or CTE. But another fun way in SQL Server is using outer apply:
select v.IR2,
(case IR2 when 'Released' then '' else '*' end) as ir
from . . . outer apply
(values (case when r.ana = 'nh3' and r.serv='energy' and
exists( select 1 from results x where x.no=r.no and x.ana='nh3' and x.sa='rejected' and x.serv <> 'energy')
then '*' else r.sa
end)
) v(IR2)
CTE would be the best choice. If you want to continue with current statement, you need to put a copy of the case statement in other case statement. Very messy code.
SELECT
case when r.ana = 'nh3' and r.serv='energy' and
exists( select 1 from results x where x.no=r.no and x.ana='nh3' and x.sa='rejected' and x.serv <> 'energy')
then '*' else r.sa end as IR2,
CASE
(case when r.ana = 'nh3' and r.serv='energy'
and exists( select 1 from results x where x.no=r.no and x.ana='nh3' and x.sa='rejected' and x.serv <> 'energy')
then '*' else r.sa end)
WHEN 'Released' then
''
ELSE
'*'
END AS IR
I have a SQL Statement as below:
SELECT * ,(CASE WHEN DRV_ID IS NOT NULL THEN CASE WHEN (DRV_ID)<'100' thenconcat(rental_type_c,'*')ENDELSE '9' end) as testing99FROM dmtb_driver;
just wondering why it will prompt me error "From key world no found." If i change my statement in such way:
SELECT DRV_Name,DRV_ID ,(CASE WHEN DRV_ID IS NOT NULL THEN CASE WHEN (DRV_ID)<'100' thenconcat(rental_type_c,'*')ENDELSE '9' end) as testing99FROM dmtb_driver;
it working perfectly. Just curious isn't i need to type out all the field name 1 by 1 instead of using '*'?
You need to alias your table.
SELECT
d.*,
CASE WHEN DRV_ID IS NOT NULL THEN
CASE WHEN (DRV_ID)<'100' THEN
CONCAT(rental_type_c,'*')
END
ELSE
'9'
END as testing99
FROM
dmtb_driver d;
I think your logic could use some simplification too.
Ii think you missed space before from and concat, also I don't think so two case statement is required.
SELECT * ,(CASE WHEN DRV_ID IS NOT NULL AND (DRV_ID)<'100' then concat(rental_type_c,'*') ELSE '9' end) as testing99
FROM dmtb_driver;
I am attempting to create a row called Flag that will keep a count of when Value is above 2. Later I will need to sum flag as a count.
I currently have:
CASE
WHEN Value > 2
THEN 1
ELSE 0
END AS 'Flag',
CASE
WHEN 'Flag' = 1
THEN 1
ELSE 0
END AS 'FollowedUpCorrectly'
I receive the error:
Conversion failed when converting the varchar value 'Flag' to data
type int.
How can I force the 1 or 0 to be an INT in order to do later math?
I've looked around and I can't seem to find a way that fits.
To be able to use previously created columns in the select, you'll need to use for example outer apply, with something like this:
select
*
from table1
outer apply (
select CASE WHEN Value > 2 THEN 1 ELSE 0 END AS Flag
) X
outer apply (
select CASE WHEN X.Flag = 1 THEN 1 ELSE 0 END AS FollowedUpCorrectly
) Y
Test this in SQL Fiddle
You could use CTE or a subquery to create a flag and then do your case statement as needed in the outer query like this:
;WITH q1
AS (
SELECT
col1
,col2
,col3
,CASE
WHEN Value > 2
THEN 1
ELSE 0
END AS 'Flag'
FROM your_table --change this to match your table and column name
)
SELECT q1.col1
,q1.col2
,q1.col3
,CASE
WHEN q1.Flag = 1
THEN 1
ELSE 0
END AS 'FollowedUpCorrectly'
FROM q1;
I might misunderstand what you are after.
CASE
WHEN Value > 2
THEN 1
ELSE 0
END AS 'Flag',
CASE
WHEN 'Flag' = 1
THEN 1
ELSE 0
END AS 'FollowedUpCorrectly'
If these two lines are in the same code block, 'Flag' is unknown in the second Case Statement.
Update: As Siyual has pointed out, Flag is a string literal. Try changing the name to something that is not a reserved word.
You are comparing a string ('Flag') to an int (1). Perhaps you meant to refer to the first case that you named 'Flag'. If so, try referring to it without using the single quotes. Then the analyzer will recognize it and accept it as an int, which it is. But 'Flag' is a string. Flag is an int.
Hi I have simply select and works great:
select 'CARAT Issue Open' issue_comment, i.issue_id, i.issue_status, i.issue_title, i.ISSUE_summary ,i.issue_description, i.severity,
gcrs.Area_name, gcrs.sector_name,
substr(gcrs.stream_name,1,case when instr(gcrs.stream_name,' (')=0 then 100 else instr(gcrs.stream_name,' (')-1 end) ISSUE_DIVISION,
case when gcrs.STREAM_NAME like 'NON-GT%' THEN 'NON-GT' ELSE gcrs.STREAM_NAME END as ISSUE_DIVISION_2
from table(f_carat_issues_as_of('31/MAR/2013')) i
inner join v_gcrs_with_stream gcrs on i.segment_id = gcrs.segment_id
where UPPER(ISSUE_STATUS) like '%OPEN%'
Now I want to call two columns:
ISSUE_DIVISION and ISSUE_DIVISION_2
if they are equal in new columns should be value 1 if are not equal should be 0,
how can I do it ?
my full code:
select 'CARAT Issue Open' issue_comment, i.issue_id, i.issue_status, i.issue_title, i.ISSUE_summary ,i.issue_description, i.severity,
gcrs.Area_name, gcrs.sector_name,
substr(gcrs.stream_name,1,case when instr(gcrs.stream_name,' (')=0 then 100 else instr(gcrs.stream_name,' (')-1 end) ISSUE_DIVISION,
case when gcrs.STREAM_NAME like 'NON-GT%' THEN 'NON-GT' ELSE gcrs.STREAM_NAME END as ISSUE_DIVISION_2
from table(f_carat_issues_as_of('31/MAR/2013')) i
inner join v_gcrs_with_stream gcrs on i.segment_id = gcrs.segment_id
where UPPER(ISSUE_STATUS) like '%OPEN%' and
CASE WHEN ISSUE_DIVISION = ISSUE_DIVISION_2 THEN
CASE WHEN ISSUE_DIVISION is null then "Null Value found"
Else 1 End
ELSE 0 END As Issue_Division_Result
but I get error on line:
ELSE 0 END As Issue_Division_Result
ORA-00920: invalid relational operator :(
SELECT (CASE WHEN ISSUE_DIVISION = ISSUE_DIVISION_2 THEN 1 ELSE 0 END) AS ISSUES
-- <add any columns to outer select from inner query>
FROM
( -- your query here --
select 'CARAT Issue Open' issue_comment, ...., ...,
substr(gcrs.stream_name,1,case when instr(gcrs.stream_name,' (')=0 then 100 else instr(gcrs.stream_name,' (')-1 end) ISSUE_DIVISION,
case when gcrs.STREAM_NAME like 'NON-GT%' THEN 'NON-GT' ELSE gcrs.STREAM_NAME END as ISSUE_DIVISION_2
from ....
where UPPER(ISSUE_STATUS) like '%OPEN%'
)
WHERE... -- optional --
So simple you can use case statement here.
CASE WHEN ISSUE_DIVISION = ISSUE_DIVISION_2 THEN
CASE WHEN ISSUE_DIVISION is null then "Null Value found" //give your option
Else 1 End
ELSE 0 END As Issue_Division_Result
In one line, answer is as below;
[ CASE WHEN COLUMN_NAME = 'VALUE' THEN 'SHOW_THIS' ELSE 'SHOW_OTHER' END as ALIAS ]
use the variable, Oracle does not support SQL in that context without an INTO. With a properly named variable your code will be more legible anyway.