ORACLE - If in SELECT Statement - oracle

I have a select and I need to hide data in a column when some other column meets criteria. Is there a way to do that in Select statement ?
Here is what I tried:
SELECT acc.account_no, (case substr(acc.bank,18,3) when acc.account_no like'%011006000%' then ' ' end) "BANK_ACCOUNT", sum(acc.bills) "BILLS"
FROM BNK.Bank_Transaction acc
WHERE acc.Bill_Date BETWEEN '01.05.2018' AND '29.05.2018'
AND (acc.account_no LIKE '%011006000%'
OR acc.account_no LIKE '%011076000%')
I receive ORA-00905: missing keyword error in upper select.

You may need:
select case when account_no like '%011006000%'
then null
else substr(..)
end as yourColumn,
...
from ...

Related

how to add a checkbox with condition in oracle apex

I have created one tabular form(legacy page) in oracle apex and in some columns i have given type checkbox.I just want to add a condition that if that column value is 'y' then it is already checked otherwise unchecked.
please help me regarding this.
You can do it by adding case statement in the select query and give the conditions there.
Set SQL query something like this:
select ID, name, my_column
APEX_ITEM.CHECKBOX2(
p_idx => 2, p_value => ID,
p_checked_values => case when my_column = 'Y' then ID else null end
) checkbox
from my_table;

can I use `=` sign operator with sub-query instead of `IN`

I am just wondering if use = sign operator with sub-query instead of IN
Is it correct way ? and meet the oracle standard ?
Example
select column_name from my_table_1 where id = (select max(id) from my_table_2);
The Difference is related to the number of rows returned. If you have only one row returned from nested sql you may prefer both = or in operators. But if multiple rows returned from nested query, use in operator.
So, in your sql example you may prefer using any of the operators. Since, max functions returns only one row.
As you are fetching maximum value from subquery to compare with id, Both(= and IN )will work fine. But If you are trying to fetch more than one row then you have to use IN keyword.
If you have 1 result in sub query you are fine with using = sign, except when data type is wrong, for example , checking with same data type of dummy VARCHAR2(1)
select * from dual where 'X' = (select max(dual.dummy) from dual);
Is similar to using in (also same explain plain)
select * from dual where 'X' in (select max(dual.dummy) from dual);
But checking with different/wrong data type will result with exception ORA-01722 Invalid number
select * from dual where 1 =(select max(dual.dummy) from dual);

ORDER BY CASE WHEN: ORDER BY items must appear in the select list

I have this example code of something I'm trying to run. Only he table names and column names were changed. What I want to do is have a result set of states and have 'NULL' be the first value and the rest of the results appear below 'NULL' in ascending order and I can't for the life of me make it work. I get the error at the bottom. This may be a very "noobish" question, but can anyone help? Much appreciated everyone!
SELECT DISTINCT
State
FROM TABLE1 (NOLOCK)
WHERE COLUMN1 NOT LIKE '%THAT%'
AND COLUMN1 NOT LIKE '%THIS%'
UNION
SELECT 'NULL'
ORDER BY ( CASE WHEN State = 'NULL' THEN 0
ELSE 1
END );
Error Message:
ORDER BY items must appear in the select list if the statement contains a UNION, INTERSECT or EXCEPT operator.
You need to select that column... even if you don't use it later, the order by requires it to be present in the select.
SELECT DISTINCT
State,
CASE WHEN State = 'NULL' THEN 0
ELSE 1 END orderId
FROM TABLE1 (NOLOCK)
WHERE COLUMN1 NOT LIKE '%THAT%'
AND COLUMN1 NOT LIKE '%THIS%'
ORDER BY ( CASE WHEN State = 'NULL' THEN 0
ELSE 1
END );

Using a variable from Oracle AS clause

I'm using the following as part of a query
SELECT
MEMO_COMMENT as MEMO,
INSTR(MEMO, 'PD') AS PastDue
FROM table
The error I receive is "MEMO": invalid identifier
I also tried
` WITH MEMO AS (select MEMO_COMMENT FROM table)
select INSTR(MEMO, 'PD') AS PastDue`
FROM table
and received the same error
Is there a way to declare the field like this? I feel like I'm missing something obvious.
In the first one, you can't use the alias as a select field at the same nesting level, so it is the second reference that is wrong:
SELECT
MEMO_COMMENT as MEMO,
INSTR(MEMO, 'PD') AS PastDue
FROM table
Should be:
SELECT
MEMO_COMMENT as MEMO,
INSTR(MEMO_COMMENT , 'PD') AS PastDue
FROM table
Or you can reference the alias from a higher nesting level, for example:
SELECT
INSTR(MEMO , 'PD') AS PastDue
FROM ( SELECT
MEMO_COMMENT as MEMO
FROM table )
For the second, the WITH statement, you are aliasing the dataset as a table named MEMO - not the field. This would work though:
WITH MEM_DATA AS (select MEMO_COMMENT as MEMO FROM table)
select INSTR(MEMO, 'PD') AS PastDue`
FROM MEM_DATA;

Oracle Subselect in NVL (Group By required)

I'm facing a problem in Oracle.
I had a SQL where some values were fixed. Now I started replacing them with values from a parameter-table. Some of these fixed values where in a NVL().
Simply said my statement is like this.
SELECT NVL(MAX(t.datefield), to_date('01011900','DDMMYYYY'))
FROM table t;
That works fine.
Now I want to replace the fixed date to a date from my parameter-table with a subselect, which doesn't work at all.
// Works
SELECT NVL(MAX(NULL), 'hello') FROM DUAL;
// Doesn't work
SELECT NVL(MAX(NULL), (SELECT 'hello' FROM DUAL)) FROM DUAL;
The error is:
ORA-00937: .... "not a single-group group function"
I have no idea how to group by a subselect.
Any help is very appreciated! Thanks!
You can't group by a sub-select. However, in order to achieve this your sub-select is only going to be able to return one row. So, change it into a Cartesian join and group by that.
SELECT NVL(MAX(NULL), str)
FROM DUAL
CROSS JOIN ( SELECT 'hello' as str FROM DUAL )
GROUP BY STR
More generally every column that is not included in an aggregate function must be included in the GROUP BY. Plus NVL() is bad; use COALESCE() or CASE instead.

Resources