I am using case in where clause but getting error: missing keyword
SQL:
SELECT *
FROM tmp t
WHERE
CASE :p_flag WHEN 'Y'
THEN t.call_destination NOT IN ('Premium', 'Satellite')
ELSE t.call_destination LIKE call_destination
END;
The predicate of a CASE expression has to be a single value, not another expression. If I read your WHERE clause correctly, you ought to be able to rephrase your query as follows:
SELECT *
FROM tmp t
WHERE
(:p_flag = 'Y' AND t.call_destination NOT IN ('Premium','Satellite')) OR
(:p_flag <> 'Y' AND t.call_destination LIKE call_destination);
-- ^^^ not sure about this
I have highlighted a LIKE comparison you are making which does not seem to make sense because it would always return true.
Related
I want to use Predicate notLike(Expression x, String pattern) to replace the following query:
select * from mytable
where mytable.myParameter not in ('ABC%','XYZ');
But when I execute my query using the following predicate:
Predicate notLike = builder.notLike(fromMyTable.get(MyTable_.myParameter), "ABC%|BCD");
is not working.
But if I create 2 predicates for the both strings: "ABC%" and "XYZ" is working.
Is my pattern written wrong or where is the problem? I tried to change the pattern in many ways but is still not working.
Thank you!
In SQL A not in (X,Y,Z) predicate is a shortcut for the following condition:
A <> X AND A <> Y AND A <> Z
this condition uses "not equal" operators, which compare operands for exact unequality.
It is not ossible to use IN or NOT IN operators for pattern-matching.
You must rewrite your query using LIKE operator which supports pattern matching:
select * from mytable
where mytable.myParameter not in ('XYZ')
and mytable.myParameter not LIKE 'ABC%';
or
select * from mytable
where mytable.myParameter <> 'XYZ'
and mytable.myParameter not LIKE 'ABC%';
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);
How do I store the result of a query into a variable in HiveQL and then use it in another select statement?
For example, whenever I store a normal variable and use it in a select statement it works just fine.
SET a=1; SELECT CASE WHEN b > ${hiveconf:a} THEN NULL ELSE 1 from my_table
But when I try and put a query into the variable, its seems to store the query instead of running it and storing the result. This then results in an error.
SET a=SELECT MAX(num) FROM my_other_table; SELECT CASE WHEN b > ${hiveconf:a} THEN NULL ELSE 1 from my_table
The error being: cannot recognize input near 'select' 'max' '(' in select clause
Does anyone know a work around to this? I am using Hive 0.13
You cant do that only by hive.
If your hive query is controlled by outer script like shell or python.You can perform the first query, get the output and then put it in the next sql.
Or you can change your sql to use join.Your example code can be changed to
select case when b > t.a then NULL else 1 from my_table
join (select max(num) a from my_other_table) t
I am trying to return a list of cell phone numbers from the S_CONTACT table of Siebel where the value contains anything other than numbers.
The query I am using is:
select cell_ph_num
from s_contact
where regexp_replace(cell_ph_num, '0|1|2|3|4|5|6|7|8|9', '') <> ''
But I get no results.
However, when I run the following query:
select regexp_replace(cell_ph_num, '0|1|2|3|4|5|6|7|8|9', '') from s_contact
I get a load of results.
Do these results not match the "does not equal empty string" clause?
'' is NULL in oracle.. so it has to be IS NOT NULL
select cell_ph_num
from s_contact
where regexp_replace(cell_ph_num, '0|1|2|3|4|5|6|7|8|9', '') IS NOT NULL
OR We can use REGEXP_LIKE this way by POSIX class
WHERE REGEXP_LIKE (cell_ph_num,'[^[:DIGIT:]]');
OR Perl style POSIX equivalent
WHERE REGEXP_LIKE (cell_ph_num,'\D');
hi guy i have a query that give me the followin error:
ORA-01791: not a SELECTed expression
this is the select expresison , please can you tell me why ?
declare
freqLettura varchar2(64);
billingcy varchar2(64);
begin
freqLettura := null;
billingcy := null;
for rec in ( select distinct(fn_get_facilityid(z.uidfacility) ) as a, 1 as b
from facilityhistory z,
locality l ,
plant p ,
ztmp_sam_tb_sdv zsdv ,
ztmp_sam_tb_plantcode zplant ,
sam_tb_ca_pdr sam,
meterhistory mh,
meter m ,
meterclass mc
where
Z.UIDLOCALITY = L.UIDLOCALITY and
p.UIDPLANT = L.UIDPLANT and
z.uidaccount = zsdv.uidaccount and
p.plantcode = zplant.plantcode and
sam.uidfacility = z.uidfacility and
z.stoptime is null and
sam.status = 'U' and
mh.uidfacility = z.uidfacility and
mh.uidmeter = m.uidmeter and
m.uidmeterclass = mc.uidmeterclass and
(billingcy is null or p.UIDBILLINGCYCLE = billingcy )
AND
(
(
(freqLettura = 'G') AND ( mh.corrmeterid is not null and mh.stoptime is null and mc.maxflowmeter >= SAM_FN_GET_PARAMETER_FLOAT('MAXFLOWMET_DETT_GIORN'))
)
OR
(
nvl(freqLettura,'nullo') <> 'G' AND (freqLettura is null or sam.readfrequency = freqLettura)
)
) and ROWNUM = 1 order by sam.stoptime, sam.uidsamtbpdr desc ) loop
begin
insert into ztmp_sam_tb_elab_pdr (facilityid, uidbatchrequest) VALUES (rec.a, rec.b);
exception
when dup_val_on_index then
null;
end;
end loop;
end;
Whenever you get an Oracle error message you don't understand, the first thing to do is look up the meaning. One way is simply to Google it. In this case the full description found in
Oracle9i Database Error Messages is:
ORA-01791 not a SELECTed expression
Cause: There is an incorrect ORDER
BY item. The query is a SELECT DISTINCT query with an ORDER BY clause.
In this context, all ORDER BY items must be constants, SELECT list
expressions, or expressions whose operands are constants or SELECT
list expressions.
Action: Remove the inappropriate ORDER BY item from the SELECT list
and retry the statement.
(Oddly this error message isn't documented in the 10G or 11G manuals, despite still being raised!)
This matches the statement you have written, which is a SELECT DISTINCT query where you are trying to order the results by a column that you did not select.
If you think about it, what you are asking for doesn't make sense: by selecting DISTINCT values that do not include sam.stoptime (for example) you may be consolidating many rows with different values for sam.stoptime, so which one would govern the ordering?
Also, as Noel's answer points out, there is no reason to have an ORDER BY clause in this code anyway, so the solution is simply to remove it.
If you are using DISTINCT in your SELECT query, then your ORDER BY clause should contain only those columns that your selecting. In this case sam.stoptime, sam.uidsamtbpdr are not there in SELECT statement. You can remove the ORDER BY clause, as it is not doing anything useful in your example.