SQL & PL/SQL :: Rewrite Query Using NOT EXISTS Instead Of NOT IN?
Jul 12, 2012How to rewrite this query using NOT EXISTS instead of NOT IN
SELECT STUD_ID,
join_date,
stud_nme
FROM code_balance
[code]...
How to rewrite this query using NOT EXISTS instead of NOT IN
SELECT STUD_ID,
join_date,
stud_nme
FROM code_balance
[code]...
How to re-write sql with NOT EXISTS using analytic functions?I have the following sql:
SELECT f1, f2
FROM t1 A2
WHERE NOT EXISTS (SELECT 1 FROM t1 A3 WHERE A3.f1=A2f1 AND A3.f2=A2.f2
AND A3.f3=A2.f3 AND A3.f4 = 0
)
can I rewrite the following query without using the 'ROW_NUMBER() OVER ' part.The query is supposed to pull out the records whose CODE is not NULL and has most recent date for UPDATE_DATE . The reason I wanted to do this is, When I embed this query in between many other queries along with JOINs, My oracle server is unable to execute. So, I thought its better to supplant 'ROW_NUMBER() OVER ' logic with something else and try it. .
CURRENT QUERY:
SELECT a.* FROM
(SELECT b.*, ROW_NUMBER() OVER (PARTITION BY b.PIDM
ORDER BY b.UPDATE_DATE DESC) AS Rno
FROM
(
SELECT *
FROM SHYNCRO WHERE CODE IS NOT NULL
)b
)a
WHERE a.Rno = 1
I am trying to create a materialized view with the following script :
CREATE MATERIALIZED VIEW "MRT"."MV_RV_SMALL_BUSINESS"
(
"batch_id"
,"small_business_flag"
,"account_count"
,"naics_count"
,"mra_count"
[code].......
IT GIVES ME THE FOLLOWING ERROR:
SQL Error: ORA-30353: expression not supported for query rewrite
30353. 00000 - "expression not supported for query rewrite"
*Cause: The select clause referenced UID, USER, ROWNUM, SYSDATE,
CURRENT_TIMESTAMP, MAXVALUE, a sequence number, a bind variable,
correlation variable, a set result,a trigger return variable, a
parallel table queue column, collection iterator, etc.
Is it possible to have query rewrite using MV's built from remote tables?
11.20.2
I want to create a materialized view for the last 10 days with the enable query rewrite option.
e.g. i want to create a view with the list of employees who joined the company in the last 10 days.
create materialized view M_Employee
refresh fast on commit
enable query rewrite as
select joining_date , name
from
employee
where joining_datde < TRUNC(sysdate) - 10
I seem to get the error SQL Error: ORA-30353: expression not supported for query rewrite
30353. 00000 - "expression not supported for query rewrite"
*Cause: The select clause referenced UID, USER, ROWNUM, SYSDATE.
This error is self explanatory , but is there any work around to have a query liek this to list the employees based on sysdate.
I am trying to use the below query
update t_emp set TTL_FLG =
CASE
WHEN EXISTS
(SELECT 1 from Schema1.T_STG_LW_EMP E
WHERE E.Employee = Schema2.T_emp.EMPLOYEE_NUMBER
AND E.JB_CODE like '%TP%' or E.JB_CODE like '%DGD%' or E.JB_CODE like '%PDD%'
or E.JB_CODE like '%YND%'
)
THEN 'Y'
ELSE 'N'
END;
SQL> select count(*) from emp where empno not in ( select mgr from emp );
COUNT(*)
----------
0
SQL> select count(*) from emp T1
2 where not exists ( select null from emp T2 where t2.mgr = t1.empno );
COUNT(*)
----------
9
I fired above query and I totally confused that why both are giving different output.
To rewrite this sql which is in ANSI 92 standard to ANSI 89 standard.
SELECT "PROJECT"."X_SAMPLED_DATE", SAMPLE"."SAMPLE_NUMBER"
FROM "SHIRE_PRD"."LimsUser"."SAMPLE" "SAMPLE" INNER JOIN "SHIRE_PRD"."LimsUser"."PROJECT" "PROJECT" ON"SAMPLE"."PROJECT"="PROJECT"."NAME"
WHERE ("SAMPLE"."SAMPLE_TYPE"='EM' OR "SAMPLE"."SAMPLE_TYPE"='WATER') AND "SAMPLE"."STATUS"<>'X' AND("PROJECT"."X_SAMPLED_DATE">={ts '2011-05-01 00:00:00'} AND "PROJECT"."X_SAMPLED_DATE"<{ts '2011-06-01 00:00:00'})
ORDER BY "SAMPLE"."PRODUCT"
How do I write this MSSQL statement so it works in Oracle?
update b1
set b1.b1_app_status = r3.application_status
from conv_app_status_update a, statyp r3, b1perm b1
where a.spc = r3.serv_code
and a.task_des = r3.r3_act_type_des
and a.task_status =r3.r3_act_stat_des
and a.process_code = r3.r3_process_code
and r3.application_status is not null
and a.spc = b1.serv_code
and a.id1 = b1.id1
and a.id2 = b1.id2
and a.id3 = b1.id3
I have a question i wanted to know that " Is it possible to write the Following Query Using NOT EXISTS
SELECT * FROM DEPT WHERE DEPTNO NOT IN (SELECT UNIQUE DEPTNO FROM EMP);
And one more doubt is there, can we use join to get same result.
i see in my alert.log this message:
Errors in file /oracle/BWP/saptrace/usertrace/bwp_ora_2728058.trc:
ORA-01114: IO error writing block to file 1030 (block # 602122)
ORA-27063: number of bytes read/written is incorrect
IBM AIX RISC System/6000 Error: 28: No space left on device
Additional information: -1
Additional information: 180224
But this file_id i don't have in my database, i am making these queries:
SQL> select FILE_ID from dba_temp_files order by FILE_ID;
FILE_ID
----------
1
2
3
4
5
6
7
8
9
[code]....
I don't have this file_id, why alert.log is showing me it? Of course, nobody has created this datafile and nobody has removed it too.
i have a procedure like create or replace procedure studrec( a in sid%rowtype)asi sid%rowtype;beginselect sid into ifrom students; i need to check whether sid is exist in the variable i or not
View 5 Replies View RelatedI have a script like this:
------------------------------------------------------------
DROP TABLE CON_TEST CASCADE CONSTRAINTS ;
CREATE TABLE CON_TEST (
IDI NUMBER(10,0),
USERID VARCHAR2(10),
PWD VARCHAR2(10),
NOTE VARCHAR(100)
)
----------------------
But i think if table CON_TEST doen't exist, an error message will appear. I know that in SQL Server we can check if table exists or not. So, i wonder if we can do that in Oracle?
By the way, is there any way to run a file script that contents TABLES, STORED, ... on a developed PC connect to oracle db server? (in case, i'm developing on PC, using Net Service Name to conect to Oracle DB Server)
following is a query which i find difficult to understand why EXISTS is failing. There are two scenarios where if i block LINE 30 and unblock line 31 of the code then one record is returned.
SELECT A.ENTITY_CODE,
A.VRNO,
A.VRDATE,
[Code]....
Which is the best way to check whether a record exists.
DECLARE
l_count NUMBER;
BEGIN
SELECT COUNT(*) INTO l_count FROM emp WHERE empno=7839;
IF l_count=1 THEN
dbms_output.put_line('exists');
ELSE
dbms_output.put_line('not exists');
END IF;
END;
[Code]...
how to write a function that returns top value if not exists then next top for combination of customer_id and hierarchy.For instance :
If I've got table
customer_id ,hierarchy, function_code
123 |1 | Z1
123 |2 |67
123 |3 |5B
678 |10 |S2
678 |11 |Z2
345 |2 |11
For the customer ID 123 I want to return Z1, for customer 678 I want to return S2 and for customer ID 345 I want 11
Problem is that I'm new to the concept of looping. I know how to write a function that accepts customer_id as a value write a cursor and then check IF hierarchy = 1 the return FUNCTION_CODE IF hierarchy - 2 THEN ...
but I need something more universal as some of the customers may have hierarchy function 1 and that would be the top one for him but others might have function of hierarchy 10 as top and checking all of the possibilities using if would be just stupid. So how to write something universal ? And of course if function did not find any customer_id then return null.
I have a list of values from a spreadsheet and want to know which values are NOT matched in columns of a table
here's the list (really 4000+ long)
1234,
2345,
3244,
and I want to find the values that are not in the table 'table_name' like this
....
where not exists (Select number_n from table_name
where number_n in ('1234', '2345', '3244', ...(the list above))
I have a table called TRANS, and a primary key field tran_id. How would i check if there is a record matching tran_id 'DUP7927' ?
View 25 Replies View RelatedAs per the earlier post,I am able to parse now.
i have also another concern as per below xml file.
My requirement is to identify perticular node ,whose having PriorValue attribute present in <pi:the Actual_Comp_Change> tag,those record should return.
<?xml version="1.0" encoding="UTF-8"?>
<pi:Extract_Employees xmlns:pi="urn:com.workday/picof">
<pi:Employee>
<pi:Employee_ID>1100</pi:Employee_ID>
[Code]....
OUTPUT:
EmployeeID_ Name JobTitle_ Grade ActualComp_Change_
1100 Surana Intern - Master¿s A 500000
1000 roy Intern - Master¿s B 216000
1000 roy Intern - Master¿s 00 266000
But my requirement is to display only those employeeID ,where Actual_Comp_Change tag having PriorValue attribute.
The required OutPUT should be :
EmployeeID_ Name JobTitle_ Grade ActualComp_Change_
1100 Surana Intern - Master¿s A 500000
is there any possibility to use ExistNode() function to the above quer or is there any alternative solution.
I have this sample:
the column data1 is datetime datatype with
t as ( select 'SMITH' nom,to_date('21/09/2013 07:30:00') data1
from dual union all select 'ALLEN',to_date('21/09/2013 07:40:00')
from dual union all -- select 'WARD',to_date('21/09/2013 07:50:00')
from dual union all
select 'JONES',to_date('21/09/2013 08:00:00')
from dual union all
[Code]..
How can I write a select to check that If I input 10 minutes to nom 'ALLEN' it's ok because the time 07:40 + 10 minutes = 07:50 the row not exists, (the next)but If input 20 it exists because the sum = 08:00 and row isn't free , indeed, there is 'JONES'?
what the difference between IN and EXISTS operator. Why should we use EXISTS operator?
View 1 Replies View RelatedI have a single table with a TOTAL_TIME column which I want to increment by a certain amount every time I get a request from a specific user. If the row for that user does not exist, it should be created and the TOTAL_TIME column should be set to the value that just came in. Otherwise, if it does exist, it should be incremented by the value passed in.
How can I accomplish this in oracle? I don't want to just first do a select, then insert, because that can cause race conditions. I want something that'll do the check and insert/update in one statement (locked).
where difference lies between these operators or clause, & whether there is any "except" operator in oracle, i know other three are used bt dont think oracle uses "except" too..as far as i have seen "NOT IN", "MINUS" , "NOT EXISTS" are exactly similar?
View 4 Replies View RelatedI am facing a problem in my database that whenever I load any kind of database objects in a particular tablespace then it gives:-
ORA-08103: object no longer exists
If I load in any other tablespace, everything works fine. I am getting error while export/import, create a table etc. How can I resolve it. I was wondering that may be it is facing problem while writing to a particular datafile.
I am using Forms 6i and oracle 10 g . I am able to use the datalink from the SQL. But when i put it in the form, and try to compile, it exists from the form builder.
View 10 Replies View RelatedI have a basic requirement to report rows from one table where the second table do not have the same record (basically same key).I understand that we could achieve this using not in or not exists clause. But I think using outer join probably be the simplest one. However, I could not achieve what I actually wanted using outer join
Test Case:
create table tab_1 (a number(1), b varchar2(20));
insert into tab_1 values (1,'one');
insert into tab_1 values (2,'two');
insert into tab_1 values (3,'three');
create table tab_2 (a number(1), b varchar2(20));
insert into tab_2 values (1,'one');
insert into tab_2 values (2,'two');
commit;
Now, I framed the query as
select y.a ya, x.a xa, x.b xb
from tab_1 x, tab_2 y
where x.a = y.a (+);
/
YA XA XB
---- ---- ----
1 1 one
2 2 two
3 three
In this case, as there is no corresponding record in second table, the value of YA is showing as null as shown above.Hence, I changed the query as
select y.a ya, x.a xa, x.b xb
from tab_1 x, tab_2 y
where x.a = y.a (+)
and y.a(+) is null;
/
YA XA XB
---- ---- ----
1 one
2 two
3 three
I dont understand why it is behaving in that way. I am rather expecting the output to come as:
YA XA XB
---- ---- ----
3 three
I have 2 queries one which gives me unique records and second which I created using EXISTS to eliminate duplication.i.e. First query gives me 4 records but when I put the table which is in the EXISTS block in the normal join, it gives me 8 records.
But the issue is I want data from the query which I have put in the EXISTS block.
if i have data like this
case 1
code start month end month
winter march march
summer april january
spring feburary feburary
case 2
winter January January
summer february september
spring october december
and if i am trying to enter again start month and end month . i should validate if the month i am giving is already present in the table for any of the season
i want a do the validate with a query so that i will not enter duplicates.
I have the following table with the some sample data:
|| Filename || receiving_site || originating_site ||
| fileA | siteA | HQ |
| fileA | siteA | siteA |
| fileB | siteA | HQ |
| fileA | siteB | HQ |
| fileA | siteB | siteB |
| fileB | siteB | HQ |
| fileB | siteB | siteB |
The table works in such way:
1. For each of the Filename, if originating_site is not the same as receiving_site, it means that the file has been sent to receiving_site but has not been acknowledged received yet.
2. For each of the Filename, if originating_site is the same as receiving_site, it means the file has been sent and received by the receiving_site.
My task is to list out all the Filename per receiving_site that has been sent, but not received yet by the receiving_site. For example from the sample data above, I am expecting to see that siteA, fileB has not been received yet.
How can I do that? I had tried MINUS and NOT EXISTS command, but I am just not able to get the result that I want.