Intersection in Select statement
Hi,
Do any one know about some intersection option in select statement.
My case is : I have 2 database tables say TAB1 and TAB2 . TAB1 has data (A, B, C, D) and TAB2 has data (B, D) now I want the output as (A and C) after joining these 2 tables and selecting.
That is I want the values of Table1 which are not there in Table2.
I have the option of selecting Table1 and then for all entries fetch Table2, then Loop at the Table & delete if record exists.
But this is creating performance issue as the number of records in both tables are in Lakhs.
So I wanted to know is there any option like Left Intersecion or some thing like left outer join and all, so that I can pick the dat in one select.
Thanks,
Sameer
Issue solved :
Sample Code
TYPES : BEGIN OF X_VBAK,
VBELN TYPE VBAK-VBELN,
AUBEL TYPE VBRP-AUBEL.
TYPES : END OF X_VBAK.
DATA : I_VBAK TYPE STANDARD TABLE OF X_VBAK.
DATA : I_VBAS TYPE STANDARD TABLE OF X_VBAK.
SELECT AVBELN BAUBEL INTO CORRESPONDING FIELDS OF TABLE I_VBAK FROM ( VBAK AS A
LEFT OUTER JOIN VBRP AS B ON AVBELN EQ BAUBEL ).
SORT I_VBAK BY VBELN.
delete ADJACENT DUPLICATES FROM i_vbak COMPARING ALL FIELDS.
DELETE I_VBAK WHERE AUBEL NE ''.
SORT I_VBAK BY VBELN.
SORT I_VBAS BY VBELN.
Similar Messages
-
Using Select statement in IF condition?
hi all,
Can i use select statement in IF COndition in pl sql ?
eg like- if( select 1 from ASD) then
end if;There is no way to do any kind of select statement inside if conditions.
Why don't test simple cases like this first?
An example to show it.
SQL> begin
2 if exists (select 1 from dual) then
3 dbms_output.put_line('ok');
4 end if;
5 end;
6 /
if exists (select 1 from dual) then
ERRORE alla riga 2:
ORA-06550: line 2, column 5:
PLS-00204: function or pseudo-column 'EXISTS' may be used inside a SQL
statement only
ORA-06550: line 2, column 2:
PL/SQL: Statement ignored
SQL> begin
2 if ( (select count(*) from dual) > 0 ) then
3 dbms_output.put_line('ok');
4 end if;
5 end;
6 /
if ( (select count(*) from dual) > 0 ) then
ERRORE alla riga 2:
ORA-06550: line 2, column 8:
PLS-00103: Encountered the symbol "SELECT" when expecting one of the following:
( - + case mod new not null others <an identifier>
<a double-quoted delimited-identifier> <a bind variable> avg
count current exists max min prior sql stddev sum variance
execute forall merge time timestamp interval date
<a string literal with character set specification>
<a number> <a single-quoted SQL string> pipe
<an alternatively-quoted string literal with character set specification>
<an alternativ
ORA-06550: line 2, column 33:
PLS-00103: Encountered the symbol ")" when expecting one of the following:
. , @ ; for <an identifier>
<a double-quoted delimited-identifier> group having intersect
minus order partition start subpartition union where connect
SQL> begin
2 if ( 0 in (select count(*) from dual) ) then
3 dbms_output.put_line('ok');
4 end if;
5 end;
6 /
if ( 0 in (select count(*) from dual) ) then
ERRORE alla riga 2:
ORA-06550: line 2, column 12:
PLS-00405: subquery not allowed in this context
ORA-06550: line 2, column 2:
PL/SQL: Statement ignoredBye Alessandro -
Using Select Statement in the decoe function in Oralce Form10 g
Hello All:
Is it possible to call another Select Statement inside the Decode Function in Oracle Form? I tested the SQL on PL/SQL Plus, it ran without giving any errors. However, I got the following errors when I tried to compile the procedure in the Oracle Form right after the where condiction at the first select statement....
Error 103 at line number....
Encountered the symbol "," when expecting one of the following
) intersect minus order union .....
Select a.col1
a.col2,
decode((select min(test_date) pa_closr_date from table_name c where a.col1 = c.col1), null, a.col3,(select min(test_date) pa_closr_date from table_name c where a.col1 = c.col1)) test_date,
a.col4,
from table_name a;
thanks ahead for any help.
KMDYou don't need the Select within the decode. In SQL Plus, I tried your SQL using this:
Select
a.col1,
a.col2,
decode( (select min(test_date) pa_closr_date
from table_name c where a.col1 = c.col1), null, a.col3,
(select min(test_date) pa_closr_date
from table_name c where a.col1 = c.col1)
) test_date
from table_name a;And this gives the same result:
Select
a.col1,
a.col2,
decode(c.pa_closr_date, null, a.col3, c.pa_closr_date ) test_date
from table_name a,
( select col1, min(test_date) pa_closr_date
from table_name
group by col1 ) c
where c.col1(+) = a.col1; -
Cursor - Suggestions for Dynamic select statements
Hey,
Am trying to define a cursor like this -
cursor c1 is
select table_name from dba_tables INTERSECT select table_name from dba_tables@SOME_DBLINKMy need is to pass this dblink as IN parameter to the procedure and use it in the select statement for cursor. How can I do this?
Any suggestion is highly appreciated. Thanks!Well that was meant to be my point. If you had two, you wouldn't (I hope) call the second one "c2" - you would be forced to think about what it represented, and name it "c_order_history" or something. Sticking "1" on the end does not make an extensible naming convention for cursors any more than it does for variables, procedures, tables or anything else, and so the "1" in "c1" is redundant because there will never be a c2.
-
Help needed with a SELECT statement. How can I make it run faster?
Hi,
not sure if my brain is just too tired but I can't seem to crack this problem today.
Here is my scenario.
I have 2 tables
TABLE1 (searchId INTEGER, routeId INTEGER);
TABLE2 (routeId INTEGER, cityId INTEGER);
There are indexes on all 4 columns.
(routeId on TABLE1 is a primary key).
In the data I am using, a given search has more than 500 routes, each route has between 10 and 300 cities among more than 4000 possible different cities.
Now, what I want to create is the list of route couple, within a certain search, that do not have a single city in common.
That list should populate a table with the following structure
TABLE3 (searchId INTEGER, routeId1 INTEGER, routeId2 INTEGER)
Here is the fastest select statement I have found so far.
SELECT :searchId, t1.routeId, t2.routeId FROM table1 t1, table1 t2
WHERE t1.searchId=:searchId AND t2.searchId=:searchId
AND t1.routeId>t2.routeId
AND NOT EXISTS (
SELECT cityId FROM table2
WHERE routeId=t1.routeId
INTERSECT
SELECT cityId FROM table2
WHERE routeId=t2.routeId);
But it still seem really slow to me.
Any suggestion for an improved version is welcome.
Thanks,
Martin.
Title was edited by:
user453358I originaly posted this thread because I tought I was missing something "obvious" that would perform better that would make my SELECT statement perform better.
So I did not want to go as deep as using TKPROOF yet.
Here is the statistics I gets on my statement.
1 SELECT t1.searchId,t1.routeId, t2.routeId id2
2 FROM table1 t1, table1 t2
3 WHERE t1.searchid=t2.searchid
4 AND t1.searchId=91
5 AND t1.routeId>t2.routeId
6 AND NOT EXISTS (
7 SELECT cityId FROM table2
8 WHERE routeId=t1.routeId
9 INTERSECT
10 SELECT cityId FROM table2
11* WHERE routeId=t2.routeId)
SQL> /
43302 rows.
Tidsåtgång: 00:01:55.02
Körschema
0 SELECT STATEMENT Optimizer=ALL_ROWS (Cost=9 Card=1 Bytes=14)
1 0 FILTER
2 1 TABLE ACCESS (BY INDEX ROWID) OF 'table1' (TABLE) (Cost=1 Card=1 Bytes=7)
3 2 NESTED LOOPS (Cost=3 Card=1 Bytes=14)
4 3 TABLE ACCESS (BY INDEX ROWID) OF 'table1' (TABLE) (Cost=2 Card=1 Bytes=7)
5 4 INDEX (RANGE SCAN) OF 'table1_IDX1' (INDEX) (Cost=1 Card=1)
6 3 INDEX (RANGE SCAN) OF 'table1_IDX1' (INDEX) (Cost=0 Card=1)
7 1 INTERSECTION
8 7 SORT (UNIQUE) (Cost=3 Card=108 Bytes=864)
9 8 TABLE ACCESS (BY INDEX ROWID) OF 'table2' (TABLE) (Cost=2 Card=108 Bytes=864)
10 9 INDEX (RANGE SCAN) OF 'table2_IDX1' (INDEX) (Cost=1 Card=108)
11 7 SORT (UNIQUE) (Cost=3 Card=108 Bytes=864)
12 11 TABLE ACCESS (BY INDEX ROWID) OF 'table2' (TABLE) (Cost=2 Card=108 Bytes=864)
13 12 INDEX (RANGE SCAN) OF 'table2_IDX1' (INDEX) (Cost=1 Card=108)
Statistik
1 recursive calls
0 db block gets
2872765 consistent gets
0 physical reads
812 redo size
964172 bytes sent via SQL*Net to client
32245 bytes received via SQL*Net from client
2888 SQL*Net roundtrips to/from client
860256 sorts (memory)
0 sorts (disk)
43302 rows processed Looks like a big number of consistent gets! Any idea how to improve on that?
Martin. -
If statement in select statement alias
I have the following select statement. It has the alias Survivors, Deaths and "All Cases". Is it posible to use :P_LANGUAGE variable to say that -- IF :P_LANGUAGE = FRENCH THEN alias are Survivants for survivors, Décès for Deaths, Tous_les_cas for All Cases. Please advise
SELECT ALL T_NTR_MULTIBAR.CAT, T_NTR_MULTIBAR.NUM_CASES_LEFTBAR AS Survivors,
T_NTR_MULTIBAR.NUM_CASES_MIDDLEBAR AS Deaths, T_NTR_MULTIBAR.NUM_CASES_RIGHTBAR AS "All Cases"
FROM T_NTR_MULTIBAR
WHERE INSTANCE_NUM = :P_INSTANCENUM
order by ORDERSYou may not be able to add this condition inside the SQL Statement. But you can add this condition outside the statement, if you're using PL/SQL...
IF :p_language = french THEN
SELECT ALL t_ntr_multibar.cat,
t_ntr_multibar.num_cases_leftbar AS survivors,
t_ntr_multibar.num_cases_middlebar AS deaths,
t_ntr_multibar.num_cases_rightbar AS "All Cases"
ELSE
END IF; -
How to get all values from an interval using select statement
Hi,
Is it possible to write a select statement that returns all values from an interval? Interval boundaries are variable.
something like this:
select (for x in 1,1024 loop x end loop) from dual
(this, of course, doesn't work)
The result in this example should be 1024 rows of numbers from 1 to 1024. These numbers are parameters, so it is not possible to create a table with predefined values.
Thanks in advance for your help,
Mia.For your simple case, with a lower boundary of 1, you can use:
SELECT rownum
FROM all_objects
WHERE rownum <= 1024For a set of number between say 50 - 100, you can use something like:
SELECT rownum + (50 - 1)
FROM all_objects
WHERE rownum <= (100 - 50 + 1)Note, that all_objects was used only because it generally has a lot of rows. Any table with at least the number of rows in your range will work.
TTFN
John -
Select statement operators in ecc 6.
Hi Experts,
I have a small doubt about the '>=' ( greater than or equal to ) operator usage in select statement. Is this operator by any chance perform not as desired in ECC 6.0. Is it a good option to use 'GE' instead of '>='. ?
It may sound a bit awkward, but still I would like to know. I am facing a situation, which could be related to this. An early response would be highly appreciated.
I would request,you NOT TO REPLY with links/explanations which says how to use select statement. Only answer if you have the answers related to this query.
Regards,
Sandipan>
Jaideep Sharma wrote:
> Hi,
> The only difference is GE will take a little more time than >= as system need to convert the keyword into actual operator when fetching data from Database.
>
> KR Jaideep,
????? Every Open SQL statements is translated to the SQL slang the underlying database is talking regardless if you type GE or >=
If the result differs using >= or GE i would open a call at SAP instead of asking in SDN. -
How to find the number of fetched lines from select statement
Hi Experts,
Can you tell me how to find the number of fetched lines from select statements..
and one more thing is can you tell me how to check the written select statement or written statement is correct or not????
Thanks in advance
santoshHi,
Look for the system field SY_TABIX. That will contain the number of records which have been put into an internal table through a select statement.
For ex:
data: itab type mara occurs 0 with header line.
Select * from mara into table itab.
Write: Sy-tabix.
This will give you the number of entries that has been selected.
I am not sure what you mean by the second question. If you can let me know what you need then we might have a solution.
Hope this helps,
Sudhi
Message was edited by:
Sudhindra Chandrashekar -
Use of LIKE in where clause of select statement for multiple records
Hi Experts,
I have a account number field which is uploaded from a file. Now this account numbers uploaded does not match fully with sap table account numbers but it contains all of the numbers provided in the file mostly in the upright positions.
For example in file we have account number as 2ARS1 while in sap table the value is 002ARS1.
And i want to fetch data from sap table based on account number uploaded. So, i am trying to use LIKE with for all entries but its not working as mentioned below but LIKE is not working with FOR ALL ENTRIES.
data : begin of t_dda occurs 0,
dda(19) type c,
end of t_dda.
data : begin of t_bukrs occurs 0,
bukrs type t012k-bukrs,
end of t_bukrs.
data : dda type t012k-bankn,
w_dda type t012k-bankn.
CONCATENATE '%'
'2ARS1'
INTO W_DDA.
MOVE W_DDA TO T_DDA-DDA.
APPEND T_DDA.
CLEAR T_DDA.
free t_bukrs.
SELECT BUKRS
FROM T012K
into TABLE t_bukrs
for all entries in t_dda
WHERE BANKN like t_dda-dda.
Can anybody suggest what should i use to get the data for multiple account numbers using one select statement only instead on using SELECT UP TO 1 ROWS in LOOP....ENDLOOP ?
Thanks in advance,
AkashHi,
yes, For All entries won't work for LIKE with '% '.
I think the other alternative is go for Native SQL by writing sub-query
sample code is here:
data: begin of i_mara occurs 0,
matnr like mara-matnr,
matkl like mara-matkl,
end of i_mara.
exec sql.
select matnr, matkl from mara where matnr in (select matnr from marc) and matnr like '%ma' into :i_mara
endexec.
loop at i_mara.
write:/ i_mara-matnr, i_mara-matkl.
endloop.
hope u got it.
regards
Mahesh
Edited by: Mahesh Reddy on Jan 21, 2009 2:32 PM -
What is the use of additon in up to 1 rows in SELECT statement
Hi All,
What is the use of up to 1 rows in select statement.
for example
SELECT kostl
FROM pa0001
INTO y_lv_kostl UP TO 1 ROWS
WHERE pernr EQ pernr
AND endda GE sy-datum.
ENDSELECT.
I'm unable to get in wat situations we hav to add up to 1 rows
please help me out...
Thanks,
santosh.Hi,
Use "select up to 1 rows" only if you are sure that all the records returned will have the same value for the field(s) you are interested in. If not, you will be reading only the first record which matches the criteria, but may be the second or the third record has the value you are looking for.
The System test result showed that the variant Single * takes less time than Up to 1 rows as there is an additional level for COUNT STOP KEY for SELECT ENDSELECT UP TO 1 ROWS.
The 'SELECT .... UP TO 1 ROWS' statement is subtly different. The database selects all of the relevant records that are defined by the WHERE clause, applies any aggregate, ordering or grouping functions to them and then returns the first record of the result set.
Regards,
Bhaskar -
Secondary Index Select Statement Problem
Hi friends.
I have a issue with a select statement using secondary index,
SELECT SINGLE * FROM VEKP WHERE VEGR4 EQ STAGE_DOCK
AND VEGR5 NE SPACE
AND WERKS EQ PLANT
%_HINTS ORACLE
'INDEX("&TABLE&" "VEKP~Z3" "VEKP^Z3" "VEKP_____Z3")'.
given above statement is taking long time for processing.
when i check for the same secondary index in vekp table i couldn't see any DB index name with vekp~z3 or vekp^z3 or vekp____z3.
And the sy-subrc value after select statement is 4. (even though values avaliable in VEKP with given where condition values)
My question is why my select statement is taking long time and sy-subrc is 4?
what happens if a secnodary index given in select statement, which is not avaliable in that DB Table?Hi,
> ONe more question: is it possible to give more than one index name in select statement.
yes you can:
read the documentation:
http://download.oracle.com/docs/cd/A97630_01/server.920/a96533/hintsref.htm#5156
index_hint:
This hint can optionally specify one or more indexes:
- If this hint specifies a single available index, then the optimizer performs
a scan on this index. The optimizer does not consider a full table scan or
a scan on another index on the table.
- If this hint specifies a list of available indexes, then the optimizer
considers the cost of a scan on each index in the list and then performs
the index scan with the lowest cost. The optimizer can also choose to
scan multiple indexes from this list and merge the results, if such an
access path has the lowest cost. The optimizer does not consider a full
table scan or a scan on an index not listed in the hint.
- If this hint specifies no indexes, then the optimizer considers the
cost of a scan on each available index on the table and then performs
the index scan with the lowest cost. The optimizer can also choose to
scan multiple indexes and merge the results, if such an access path
has the lowest cost. The optimizer does not consider a full table scan.
Kind regards,
Hermann -
BI Publisher : SELECT statement in RTF template
Hi Guys
I have written a BI Publisher Report using XML file created from Oracle Reports(in Oracle Apps).
Repors runs from Oracle Apps perfectly ok. Now I need to fetch some data from couple of tables and display on the Report.
I am wondering whether I can directly code SELECT statement in RTF file rather than messing with Oracle Report(.rdf) file.
Please advise.
Thanks and Regards
VijayHey Vijay,
You cannot query in RTF using select :)..
You have to mess/play with RDF to do it ;)
Oh wait, did i say , we cannot in RTF, we can , but that is difficult approach to go with., keep this as an end of the world option. -
Can we use is null in our select statement in ABAP program
hi,
I want to use 'is nul' or 'not null' in select statement of my ABAP program for any field. I have written below query but I am getting sy-subrc = 4 and getting no data. Can anyone resolve this.Hi,
I think you've posted your question on the wrong forum. This is the SAP Business One development forum which is not part of ERP and doesn't include any ABAP or Netweaver programming.
For a list of forums please see here:
http://forums.sdn.sap.com/index.jspa
Kind Regards,
Owen -
Performance issue - Select statement
Hi I am having the 10 lack records in the KONP table . If i am trying to see all the records in SE11 , it is giving the short dump TSV_TNEW_PAGE_ALLOC_FAILED . I know this is because of less memory in the Server . Is there any other way to get the data ? How to optimise the below SELECT statement if i have large data in the table .
i_condn_data - is having 8 lack records .
SELECT knumh kznep valtg valdt zterm
FROM konp
INTO TABLE i_condn_data_b
FOR ALL ENTRIES IN i_condn_data
WHERE knumh = i_condn_data-knumh
AND kschl = p_kschl.
Please suggest .Hi,
try to use "UP TO n ROWS" to control the quantity of selected data in each Loop step.
Something like this:
sort itab by itab-knumh.
flag = 'X'.
while flag = 'X'.
SELECT knumh kznep valtg valdt zterm
FROM konp
INTO TABLE i_condn_data_b UP TO one_million ROWS
WHERE knumh > new_value_for_selection
AND kschl = p_kschl.
describe table i_condn_data_b lines i.
read table i_condn_data_b index i.
new_value_for_selection = i_condn_data_b-knumh.
*....your logic for table i_condn_data_b
if one_million > i.
clear flag.
endif.
endwhile.
Regards
Maybe you are looking for
-
Hi, Was wondering if Firefox OS have a feature that alerts or notify's you if an incoming call has been redirected or diverted to your phone (from another phone)? This feature was common in basic phones and feature phones but is absent on Android, Ap
-
Photoshop CS4 Extended Trial Won't Extract
I just finally managed to get the download manager working and downloaded PS CS4 Trial and when it hits 99% extracting says I don't have enough hdd space or privlages. I'm admin, have plenty of hdd space, using windows xp sp2.
-
i have a airport extreme base station and i reset it and now the wifi connection is open to everyone... so how do i get a password back on it so everyone cant use it
-
Hi, 1.how many line items a PREQ, PO can accomodate? 2. there is an user exit by which we can increase the line items in PREQ, PO? which is that one? 3. by using user exit then how many maximum lines can be achieved? kavita
-
CE Flash files always start at the beginning?
I have had an online Continuing education course designed, which has the main contents embedded into .asp pages. The problem is that when customers "log-out", then return and "log in" again whithout closing thier browser, the fllash file always start