Optimizing the select query
Hi All,
I would like to know what would be alternate table or optimal way to make the following queries work in a more efficient manner.
1) select sfakn from vbrk into table tl_sfakn for all entries in tl_vbrp WHERE sfakn = tl_vbrp-vbeln.
2) SELECT vbeln FROM vbrp INTO TABLE tl_vbrp FOR ALL ENTRIES IN tl_vbap[] WHERE vgbel = tl_vbap-vbeln.
any help would be appreciated.
thanks
vijay
Hello Vijay,
How doing?. plz find my suggestions
1. fetch vgbel ( sales document) also into tl_vbrp in previous select. Now make use of document flow as shown
select vbeln from vbfa
into table tl_vbfa for all entries in tl_vbrp
where vbelv EQ t_vbrp-vgbel and
VBTYP_N IN ('M','N','O','P'). "billing docs
if tl_vbfa[] is not inital.
select vbeln sfakn
into table tl_sfakn
from vbrk for all entries in tl_vbfa
where vbeln EQ tl_vbfa-vbeln.
if sy-subrc EQ 0.
delete tl_sfakn where sfakn is initial.
endif.
endif.
2. u can use VBFA as shown above , use preceding and subsequent document category appropriately
thanks
sreejith
Similar Messages
-
Using case when statement in the select query to create physical table
Hello,
I have a requirement where in I have to execute a case when statement with a session variable while creating a physical table using a select query. let me explain with an example.
I have a physical table based on a select table with one column.
SELECT 'VALUEOF(NQ_SESSION.NAME_PARAMETER)' AS NAME_PARAMETER FROM DUAL. Let me call this table as the NAME_PARAMETER table.
I also have a customer table.
In my dashboard that has two pages, Page 1 contains a table with the customer table with column navigation to my second dashboard page.
In my second dashboard page I created a dashboard report based on NAME_PARAMETER table and a prompt based on customer table that sets the NAME_ PARAMETER request variable.
EXECUTION
When i click on a particular customer, the prompt sets the variable NAME_PARAMETER and the NAME_PARAMETER table shows the appropriate customer.
everything works as expected. YE!!
Now i created another table called NAME_PARAMETER1 with a little modification to the earlier table. the query is as follows.
SELECT CASE WHEN 'VALUEOF(NQ_SESSION.NAME_PARAMETER)'='Customer 1' THEN 'TEST_MART1' ELSE TEST_MART2' END AS NAME_PARAMETER
FROM DUAL
Now I pull in this table into the second dashboard page along with the NAME_PARAMETER table report.
surprisingly, NAME_PARAMETER table report executes as is, but the other report based on the NAME_PARAMETER1 table fails with the following error.
Error Codes: OPR4ONWY:U9IM8TAC:OI2DL65P
State: HY000. Code: 10058. [NQODBC] [SQL_STATE: HY000] [nQSError: 10058] A general error has occurred. [nQSError: 16001] ODBC error state: S1000 code: 1756 message: [Oracle][ODBC][Ora]ORA-01756: quoted string not properly terminated. [nQSError: 16014] SQL statement preparation failed. (HY000)
SQL Issued: SET VARIABLE NAME_PARAMETER='Novartis';SELECT NAME_PARAMETER.NAME_PARAMETER saw_0 FROM POC_ONE_DOT_TWO ORDER BY saw_0
If anyone has any explanation to this error and how we can achieve the same, please help.
Thanks.Hello,
Updates :) sorry.. the error was a stupid one.. I resolved and I got stuck at my next step.
I am creating a physical table using a select query. But I am trying to obtain the name of the table dynamically.
Here is what I am trying to do. the select query of the physical table is as follows.
SELECT CUSTOMER_ID AS CUSTOMER_ID, CUSTOMER_NAME AS CUSTOMER_NAME FROM 'VALUEOF(NQ_SESSION.SCHEMA_NAME)'.CUSTOMER.
The idea behind this is to obtain the data from the same table from different schemas dynamically based on what a session variable. Please let me know if there is a way to achieve this, if not please let me know if this can be achieved in any other method in OBIEE.
Thanks. -
How to optimize the select query that is executed in a cursor for loop?
Hi Friends,
I have executed the code below and clocked the times for every line of the code using DBMS_PROFILER.
CREATE OR REPLACE PROCEDURE TEST
AS
p_file_id NUMBER := 151;
v_shipper_ind ah_item.shipper_ind%TYPE;
v_sales_reserve_ind ah_item.special_sales_reserve_ind%TYPE;
v_location_indicator ah_item.exe_location_ind%TYPE;
CURSOR activity_c
IS
SELECT *
FROM ah_activity_internal
WHERE status_id = 30
AND file_id = p_file_id;
BEGIN
DBMS_PROFILER.start_profiler ('TEST');
FOR rec IN activity_c
LOOP
SELECT DISTINCT shipper_ind, special_sales_reserve_ind, exe_location_ind
INTO v_shipper_ind, v_sales_reserve_ind, v_location_indicator
FROM ah_item --464000 rows in this table
WHERE item_id_edw IN (
SELECT item_id_edw
FROM ah_item_xref --700000 rows in this table
WHERE item_code_cust = rec.item_code_cust
AND facility_num IN (
SELECT facility_code
FROM ah_chain_div_facility --17 rows in this table
WHERE chain_id = ah_internal_data_pkg.get_chain_id (p_file_id)
AND div_id = (SELECT div_id
FROM ah_div --8 rows in this table
WHERE division = rec.division)));
END LOOP;
DBMS_PROFILER.stop_profiler;
EXCEPTION
WHEN NO_DATA_FOUND
THEN
NULL;
WHEN TOO_MANY_ROWS
THEN
NULL;
END TEST;The SELECT query inside the cursor FOR LOOP took 773 seconds.
I have tried using BULK COLLECT instead of cursor for loop but it did not help.
When I took out the select query separately and executed with a sample value then it gave the results in a flash of second.
All the tables have primary key indexes.
Any ideas what can be done to make this code perform better?
Thanks,
Raj.As suggested I'd try merging the queries into a single SQL. You could also rewrite your IN clauses as JOINs and see if that helps, e.g.
SELECT DISTINCT ai.shipper_ind, ai.special_sales_reserve_ind, ai.exe_location_ind
INTO v_shipper_ind, v_sales_reserve_ind, v_location_indicator
FROM ah_item ai, ah_item_xref aix, ah_chain_div_facility acdf, ah_div ad
WHERE ai.item_id_edw = aix.item_id_edw
AND aix.item_code_cust = rec.item_code_cust
AND aix.facility_num = acdf.facility_code
AND acdf.chain_id = ah_internal_data_pkg.get_chain_id (p_file_id)
AND acdf.div_id = ad.div_id
AND ad.division = rec.division;ALSO: You are calling ah_internal_data_pkg.get_chain_id (p_file_id) every time. Why not do it outside the loop and just use a variable in the inner query? That will prevent context switching and improve speed.
Edited by: Dave Hemming on Dec 3, 2008 9:34 AM -
How can i use index in select query.. facing problem with the select query.
Hi Friends,
I am facing a serious problem in one of the select query. It is taking a lot of time to fetch data in Production Scenario.
Here is the query:
SELECT * APPENDING CORRESPONDING FIELDS OF TABLE tbl_summary
FROM ztftelat LEFT JOIN ztfzberep
ON ztfzberep~gjahr = st_input-gjahr
AND ztfzberep~poper = st_input-poper
AND ztfzberepcntr = ztftelatrprctr
WHERE rldnr = c_telstra_accounting
AND rrcty = c_actual
AND rvers = c_ver_001
AND rbukrs = st_input-bukrs
AND racct = st_input-saknr
AND ryear = st_input-gjahr
And rzzlstar in r_lstar
AND rpmax = c_max_period.
There are 5 indices present for Table ZTFTELAT.
Indices of ZTFTELAT:
Name Description
0 Primary key( RCLNT,RLDNR,RRCTY,RVERS,RYEAR,ROBJNR,SOBJNR,RTCUR,RUNIT,DRCRK,RPMAX)
005 Profit (RCLNT,RPRCTR)
1 Ledger, company code, account (RLDNR,RBUKRS, RACCT)
2 Ledger, company code, cost center (RLDNR, RBUKRS,RCNTR)
3 Account, cost center (RACCT,RCNTR)
4 RCLNT/RLDNR/RRCTY/RVERS/RYEAR/RZZAUFNR
Z01 Activity Type, Account (RZZLSTAR,RACCT)
Z02 RYEAR-RBUKRS- RZZZBER-RLDNR
Can anyone help me out why it is taking so much time and how we can reduce it ? and also tell me if I want to use index number 1 then how can I use?
Thanks in advance.Hi Shiva,
I am using two more select queries with the same manner ....
here are the other two select query :
***************1************************
SELECT * APPENDING CORRESPONDING FIELDS OF TABLE tbl_summary
FROM ztftelpt LEFT JOIN ztfzberep
ON ztfzberep~gjahr = st_input-gjahr
AND ztfzberep~poper = st_input-poper
AND ztfzberepcntr = ztftelptrprctr
WHERE rldnr = c_telstra_projects
AND rrcty = c_actual
AND rvers = c_ver_001
AND rbukrs = st_input-bukrs
AND racct = st_input-saknr
AND ryear = st_input-gjahr
and rzzlstar in r_lstar
AND rpmax = c_max_period.
and the second one is
*************************2************************
SELECT * APPENDING CORRESPONDING FIELDS OF TABLE tbl_summary
FROM ztftelnt LEFT JOIN ztfzberep
ON ztfzberep~gjahr = st_input-gjahr
AND ztfzberep~poper = st_input-poper
AND ztfzberepcntr = ztftelntrprctr
WHERE rldnr = c_telstra_networks
AND rrcty = c_actual
AND rvers = c_ver_001
AND rbukrs = st_input-bukrs
AND racct = st_input-saknr
AND ryear = st_input-gjahr
and rzzlstar in r_lstar
AND rpmax = c_max_period.
for both the above table program is taking very less time .... although both the table used in above queries have similar amount of data. And i can not remove the APPENDING CORRESPONDING. because i have to append the data after fetching from the tables. if i will not use it will delete all the data fetched earlier.
Thanks on advanced......
Sourabh -
Problem in the "select" query....Just check out and help me
Hi there !!
I am getting the following errors while trying to execute the "select" query from JSP page:
java.sql.SQLException: [Microsoft][ODBC Driver Manager] Invalid cursor state
My code is :
String reg_no=(String)session.getAttribute("reg_no");
ResultSet rs=stmt.executeQuery("SELECT * FROM reg WHERE regno = "+reg_no);
(Note: the "regno" in the database is of numeric datatype)
Try to help me...
Thanx,
Ram.Just so everyone knows:
Ram Mohan joined the forum on 7-Sep-2003 and received
his 25 Dukes for joining.
Since then, he's posted three questions, each one
promising 5-10 Dukes, and never once awarded them.
His strategy seems to be to wave Dukes, ask his
hit-and-run question, and then bolt.
Answer his questions if you wish, but don't do it
expecting Dukes. - MODDuffy, you're getting just as cranky as I have. See what hanging around these forums does to oneself? :) -
Need some help with the Select query.
Need some help with the Select query.
I had created a Z table with the following fields :
ZADS :
MANDT
VKORG
ABGRU.
I had written a select query as below :
select single vkorg abgru from ZADS into it_rej.
IT_REJ is a Work area:
DATA : BEGIN OF IT_REJ,
VKORG TYPE VBAK-VKORG,
ABGRU TYPE VBAP-ABGRU,
END OF IT_REJ.
This is causing performance issue. They are asking me to include the where condition for this select query.
What should be my select query here?
Please suggest....
Any suggestion will be apprecaiated!
Regards,
DeveloperHello Everybody!
Thank you for all your response!
I had changes this work area into Internal table and changed the select query. PLease let me know if this causes any performance issues?
I had created a Z table with the following fields :
ZADS :
MANDT
VKORG
ABGRU.
I had written a select query as below :
I had removed the select single and insted of using the Structure it_rej, I had changed it into Internal table
select vkorg abgru from ZADS into it_rej.
Earlier :
IT_REJ is a Work area:
DATA : BEGIN OF IT_REJ,
VKORG TYPE VBAK-VKORG,
ABGRU TYPE VBAP-ABGRU,
END OF IT_REJ.
Now :
DATA : BEGIN OF IT_REJ occurs 0,
VKORG TYPE VBAK-VKORG,
ABGRU TYPE VBAP-ABGRU,
END OF IT_REJ.
I guess this will fix the issue correct?
PLease suggest!
Regards,
Developer. -
Restricting record in the select query
Is there any way to restrict the number of search (e.g 10) records in the SELECT query ?
Hi,
You can use as below:
select <fieldname> up to 10 rows from <tablename> into itab.
Thanks,
Sriram Ponna. -
Performance of the select query
i want to know wheter the conversion of the select query
SELECT * FROM mchb
WHERE matnr EQ mch1-matnr
AND charg EQ mch1-charg
AND werks EQ plant-werks.
itab-lgort = mchb-lgort.
itab-clabs = mchb-clabs.
APPEND itab.
ENDSELECT.
into following pattern
select lgort clabs
from mchb
into corresponding fields of itab
WHERE matnr EQ mch1-matnr
AND charg EQ mch1-charg
AND werks EQ plant-werks.
ENDSELECT.
will give the same results ?Hi,
It will results in performance issue because of end select statement.
select lgort clabs
from mchb
into table itab
WHERE matnr EQ mch1-matnr
AND charg EQ mch1-charg
AND werks EQ plant-werks.
Use like this you will get the values of lgort and clabs in the internal table itab.
Then use loop statement to use that values further.
Dont use end select it will be a performance issue.
In this code there is no need to use of ENDSELECT and use corresponding fields of table itab.
select lgort clabs
from mchb
into corresponding fields of itab
WHERE matnr EQ mch1-matnr
AND charg EQ mch1-charg
AND werks EQ plant-werks.
ENDSELECT.
Thanks,
Sakthi -
How to insert the select query result into table?
How to insert the select query result into table?
SELECT top 20 creation_time
,last_execution_time
,total_physical_reads
,total_logical_reads
,total_logical_writes
, execution_count
, total_worker_time
, total_elapsed_time
, total_elapsed_time / execution_count avg_elapsed_time
,SUBSTRING(st.text, (qs.statement_start_offset/2) + 1,
((CASE statement_end_offset
WHEN -1 THEN DATALENGTH(st.text)
ELSE qs.statement_end_offset END
- qs.statement_start_offset)/2) + 1) AS statement_text
FROM sys.dm_exec_query_stats AS qs
CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) st
ORDER BY total_elapsed_time / execution_count DESC;
Thanks,
Tirumala1. SELECT INTO
Below method will create table when data is inserted from one table to another table. Its useful when you need exactly same datatype as source table.
Use AdventureWorks2008R2;
Go
---Insert data using SELECT INTO
SELECT AddressLine1, City
INTO BothellAddresses
FROM Person.Address
where City = 'Bothell';
GO
---VERIFY DATA
Select AddressLine1, City
FROM BothellAddresses
---DROP TABLE
DROP TABLE BothellAddresses
GO
2. INSERT INTO SELECT
Below method will need table to be created prior to inserting data. Its really useful when table is already created and you want insert data from
another table.
Use AdventureWorks2008R2;
Go
---Create Table
CREATE TABLE BothellAddresses (AddressLine1 NVARCHAR(60), City NVARCHAR(30))
---Insert into above table using SELECT
INSERT INTO BothellAddresses(AddressLine1, City)
SELECT AddressLine1, City
FROM Person.Address
where City = 'Bothell';
---VERIFY DATA
Select AddressLine1, City
FROM BothellAddresses
---DROP TABLE
DROP TABLE BothellAddresses
GO
Regards,
Vishal Patel
Blog: http://vspatel.co.uk
Site: http://lehrity.com -
Using order by in the select query
Hi All,
I have query in which I need the latest record. To get the latest record, I can
1) Select all entries into an internal table and sort them descending and get the first one
2) Select all entries into an internal table and order by in the select statement itself.
Then read the first one.
Which approach should be better as for as the performance is concern.
The query is on MKPF table and I have the material document number. I need the latest entry.
Regards,
ShahuHi,
The first one would be better.At the most we should not use order by in select statement bcoz it directly sorts the data according to that in database itself reverting performance issues.
Otherwise you can use select max* if records are less.
Reward points if helpful.
Thanks,
Ponraj.s. -
Hi Experts,
I have a select query as follows:
select field1 field2 from db1 into table it1.
Now suppose the it1-field1 contains values a,b and c.
Now I want to select fields a,b and c from another db table db2.
Instead of hard coding like this:
select a b c from db2 into table it2.....
How can I do this?
Points will be awarded.
Regards,
Soumya.Hi Soumya ,
Pleas use FOR ALL ENTRIES syntax , in this way you can select the records from db2 which are available or selected in db1.
Like Header And Item table.
You select records frm header table . then for corres ponding header records you select from detail table db2 .....
Condition is the field containg a,b,c is key field in both tables ...
Reward points if it helps you .
Regards
Amber S -
Hi ,
Can we write a select query where we can pass varray in the where condition.
I have this query in my stored procedure.
eg :
SELECT * from emp
WHERE deptno in l_data1;
where l_data is varray of type varchar2(40) where I have all the deptno in l_data.
Thanks.This is business requirement.
Here is my code.
create or replace PROCEDURE getAttribute (p_product_model varchar2)
AS
l_data pattern_str_type := pattern_str_type();
l_count NUMBER;
l_child_name VARCHAR2(200);
l_product_model VARCHAR2(40);
l_data1 getAttrforModel := getAttrforModel();
i NUMBER;
BEGIN
i := 1;
l_data :=getattr();
for i in 1 .. l_data.count loop
l_data.extend;
l_data1.extend;
if(l_data(i).child_type = 'Attribute' and l_data(i).product_model_name <> p_product_model)THEN
l_data1(i):=l_data(i).child_name;
dbms_output.put_line(l_data1(i));
END If;
end loop;
SELECT count(*) into l_count from
WOC_ATTRIBUTE_CATEGORY WHERE category_name = 'Software' and Attribute_Name in l_data1;
EXCEPTION
WHEN OTHERS THEN
RAISE;
END ;
CREATE OR REPLACE FUNCTION getAttr RETURN pattern_str_type
IS
l_data pattern_str_type := pattern_str_type();
l_count NUMBER;
l_child_name VARCHAR2(200);
l_product_model VARCHAR2(40);
i NUMBER;
cursor getattr IS select child_name,child_type, product_model_name from woc_pattern_Structure;
BEGIN
i := 1;
for rec in getattr loop
l_data.extend;
l_data(i) := temp_table(rec.child_name,rec.child_type,rec.product_model_name);
--dbms_output.put_line(l_data(i).child_name||' '||l_data(i).child_type||' '||l_data(i).product_model_name);
i := i + 1;
end loop;
RETURN l_data;
EXCEPTION
WHEN OTHERS THEN
RAISE;
END ; -
How to write the select query with complex where condition
Hi all,
Can u help me in writing following select query.
select * from zu1cd_corr where time_stamp between firstday and lastday .
In the above query time_stamp contains the date and time.
where as firstday and lastday contains the dates.
I need to compare the date in the time_stamp with the firstday and lastday.
But time_stamp contains the time also and it is char of 14 characters length.Hi,
If that is the case u can do as advait specified....
if the firstday and secondday are select-options then declare two more variables having 14 character length and then concatenate '000000' to firstday variable and '240000' to last day variable and then write ur query.
CLEAR : lv_firstday,
lv_lastday.
concatenate firstday '000000' to lv_firstday.
concatenate lastday '240000' to lv_lastday.
ranges : r_Date for zu1cd_corr-time_stamp.
r_date-sign = 'I'.
r_date-option = 'BT'.
r_Date-low = lv_firstday.
r_Date-high = lv_lastday.
append r_date.
select * from zu1cd_corr into table it_zu1cd_corr where time_stamp in r_Date.
I hope it helps.
Regards,
Nagaraj -
Help reagrding the selection query
Hi All...
I have VBELN
LFART
VKORG
KUNNR
ERDAT
LFDAT
VSTEL
ROUTE in my selection-screen. All the fields are from LIKP.
Now I need to add one more field LAND1 from the table VBPA in the selection screen.
This is my logic to filter the previous requirement. Now how to validate the LAND1?
-> LIKP
select vbeln
into table i_likp
from likp
where vbeln in s_vbeln
and lfart in s_lfart
and vkorg in s_vkorg
and kunnr in s_kunnr
and erdat in s_erdat
and lfdat in s_lfdat
and vstel = p_vstel
and route in s_route.
if sy-subrc = 0.
-> LIPS
select vbeln posnr bwtar volum
into table i_lips
from lips
for all entries in i_likp
where vbeln = i_likp-vbeln.
Please help me in that. I will reward u with good points.
Reagrds
Arjun JainFor LAND1 validation do as below.
AT SELECTION-SCREEN ON s_land1.
SELECT land1 FROM t005
into t005-land1
where land1 IN s_land1.
if sy-subrc NE 0.
message e000 WITH 'Enter valid Land value'.
endif.
For the query u can use join
select a~vbeln b~posnr b~bwtar b~volum
from likp as a inner join lips as b
on a~vbeln =b~vbeln
into table i_tab
where a~vbeln in s_vbeln
and a~lfart in s_lfart
and a~vkorg in s_vkorg
and a~kunnr in s_kunnr
and a~erdat in s_erdat
and a~lfdat in s_lfdat
and a~vstel = p_vstel
and a~route in s_route.
This will solve ur problem.
U can reward for the helpful answers. -
Hi All
I want to know how exactly the internal flow of select query works. Can someone please explain
ThanksHi,
Thanks for the link.Is there any example for back end process of Select queryBingo.. ;)
See, a process (or) set of programs which are working internally. You can get some of the results about the statistics of query how much cpu time it consuming, fetches etc., check v$sql_monitor. Apart from that if you are looking for some thing else then kindly elaborate your requirement.
- Pavan Kumar N
Maybe you are looking for
-
Duplicate Payment Medium Creation
Hi All, We are using Payment Medium workbench format ACH forone of the payment methods. When we do payment run F110 with this payment method, we are seeing a check box "Create Payment Medium" even during payment proposal run. If a user is checking th
-
Ap authentication/join issue
i am having issues joining new 1242LAP's to my controller. i am receiving the follwing error on my controller: AAA Authentication Failure for UserName:5475d01144f0 User Type: WLAN USER username is the MAC of my new 1242LAP. older 1242LAP's have no
-
Search For business transactions
Hi All Please help on this when we do crmd_order and go to the search for business transaction screen there is a find tab in which we get all transaction types- I want to hide some of the transaction types from there Gor eg- the transaction types ar
-
Updating an EAR in WebLogic 10
Hi - I found a bug in my application (EAR file) after I deployed it using the WebLogic console. However, I noticed that there were multiple locations within the WebLogic directory where files resembling this application existed (e.g., tmp directories
-
My iCloud is not backing up. Help!
I loaded icloud and selected manage but, it doe not back up. Any suggestions?