If statement in select (ORACLE) - sql

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.

Related

Can a calculated Column result be used in a second Column as an input? Oracle SQL

I have a SQL query in Oracle that has a calculated Column using case statements. I would like to use the newly created Column as an input to a second calculated column. Please see my code below.
When I try running this, i get the error "PAYER_TYPE": invalid identifier. So would there be a way to use "PAYER_TYPE" values in the second column?
SELECT
CASE
WHEN TYP IN ('Swap', 'Ton')
THEN
CASE
WHEN INDX_PAY = 'FIXED'
THEN 'FIXED'
ELSE 'FLAT'
END
END AS PAYER_TYPE,
CASE
WHEN TYPE = 'Swaption'
THEN
CASE
WHEN PAYER_TYPE = 'FIXED'
THEN PAY_LEG
ELSE REC_LEG
END
ELSE 0
END AS STRIKE
FROM Hedge.Details
You are receiving the error because PAYER_TYPE is not a column in Hedge.Details. Try the following if you really want to use PAYER_TYPE:
SELECT
C1.PAYER_TYPE,
CASE
WHEN C1.TYPE = 'Swaption'
THEN
CASE
WHEN C1.PAYER_TYPE = 'FIXED'
THEN C1.PAY_LEG
ELSE C1.REC_LEG
END
ELSE 0
END AS STRIKE
FROM
(
SELECT
CASE
WHEN TYP IN ('Swap', 'Ton')
THEN
CASE
WHEN INDX_PAY = 'FIXED'
THEN 'FIXED'
ELSE 'FLAT'
END
END AS PAYER_TYPE,
TYPE,
PAY_LEG,
REC_LEG
FROM Hedge.Details
) AS C1
Your better choice may be to keep it simple and not use the subquery approach as in the following:
SELECT
CASE
WHEN TYP IN ('Swap', 'Ton')
THEN
CASE
WHEN INDX_PAY = 'FIXED'
THEN 'FIXED'
ELSE 'FLAT'
END
END AS PAYER_TYPE,
CASE
WHEN TYPE = 'Swaption'
THEN
CASE
WHEN TYP IN ('Swap', 'Ton') AND INDX_PAY = 'FIXED'
THEN PAY_LEG
ELSE REC_LEG
END
ELSE 0
END AS STRIKE
FROM Hedge.Details
A simple option is an inline view, or - recently - a CTE (common table expression) aka the WITH factoring clause. Something like this:
with your_data as
(select case when ... then ...
when ... then ...
end paper_type --> PAPER_TYPE gets its value here ...
from some_table
where ...
)
select case when y.paper_type = 'A' then ... --> ... and is reused here
when y.paper_type = 'B' then ...
end result
from your_data y
You can generate that column PAYER_TYPE in the subquery, and then use in the outer part
SELECT
PAYER_TYPE,
CASE
WHEN TYP = 'Swaption'
THEN
CASE
WHEN PAYER_TYPE = 'FIXED'
THEN 'PAY_LEG'
ELSE 'REC_LEG'
END
ELSE '0'
END AS STRIKE
FROM
(
WITH Details(TYP,INDX_PAY) AS
(
SELECT 'Swap', 'FIXED' FROM DUAL UNION ALL
SELECT 'Ton' , 'FIX' FROM DUAL
)
SELECT
CASE
WHEN TYP IN ('Swap', 'Ton')
THEN
CASE
WHEN INDX_PAY = 'FIXED'
THEN 'FIXED'
ELSE 'FLAT'
END
END PAYER_TYPE,
TYP
FROM Details
)
PAYER_TYPE STRIKE
---------- ------
FIXED 0
FLAT 0

How to use Case-When in another Case-When Condition in Oracle?

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

Case Expression on a created column using Microsoft SQL

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

Case SQL If there is no record between dates

I'm new to SQL and am struggling with a case.
I would like to make the case where if an account (account_ID) doesn't have a record (ON billing_id) between current_date-302 and current_date-62 THEN MARK WITH A "1"
Query below:
Thanks in advance
SELECT
billing_date_local_time
,account_id
,contract_owner_name
,date_first_feature_partner
,deal_starts_at
,contract_id
,new_partner_type
,sum(voucher_sold) AS Vouchers
,sum(gross_bookings_local) AS GB
,sum(gross_revenue_local) AS GR
,is_G2
,Case when billing_date_local_time between current_date-302 and current_date-62 = 0 THEN 'YES' ELSE 'NO' End
FROM EMEA_ANALYTICS.eu_deal_flat
WHERE
country_id = 206
and billing_date_local_time between current_date-400
and current_date-2
GROUP BY 1,2,3,4,5,6,10,11
You'll need to do a correlated subquery; something like this:
select
a.billing_date_local_time
,a.account_id
,...
, CASE WHEN EXISTS (SELECT * FROM EMEA_ANALYTICS.eu_deal_flat b WHERE a.account_id = b.account_id AND b.billing_date_local_time between current_date-302 and current_date-62 ) THEN 'YES' ELSE 'NO' END
from
FROM EMEA_ANALYTICS.eu_deal_flat a
WHERE ...
You need to apply an aggregate function like this:
min(case when billing_date_local_time
between current_date-302 and current_date-62
then 0
else 1
end)

Using same calculation for different SQL Aliases

so I have two column aliases that are using the same calculation:
,case when bi.PolicyFeeFactor = 0
then 0
else
CAST(ROUND(nb.AnnualPolicyFee * bi.PolicyFeeFactor,2)AS DECIMAL(6,2))
end
as UNIT_POLFEE_Y
,case when bi.PolicyFeeFactor = 0
then 0
else
CAST(ROUND(nb.AnnualPolicyFee * bi.PolicyFeeFactor,2)AS DECIMAL(6,2))
end
as UNIT_PUPFEE_Y
I am looking for a way where I can just write the calculation once and create both column aliases from that one statement eg:
,case when bi.PolicyFeeFactor = 0
then 0
else
CAST(ROUND(nb.AnnualPolicyFee * bi.PolicyFeeFactor,2)AS DECIMAL(6,2))
end
as UNIT_PUPFEE_Y, UNIT_POLFEE_Y
Wrap it inside a subquery, ex
SELECT Result AS UNIT_PUPFEE_Y,
Result AS UNIT_POLFEE_Y
FROM
(
SELECT CASE .... END AS Result
FROM tableName
) s
You may write like this,
SELECT *,UNIT_PULFEE_Y AS UNIT_PUPFEE_Y FROM
(SELECT *
,case when bi.PolicyFeeFactor = 0 then 0 else CAST(ROUND(nb.AnnualPolicyFee * bi.PolicyFeeFactor,2)AS DECIMAL(6,2)) end as UNIT_PULFEE_Y
FROM Table )A