Join 3 tables, return records which no match found in table 1 as null
Hi Gurus,
I really need your expert advice. I am stuck in below query.
I have joined 3 tables,
table 2 is an information reference (to get the process code and process name)
table 1 have and table 3 have daily data (which will have the process code and its values/qty)
all values return from table 1 and 3(rtrwip00 and prdyld01) should refer to code in table 1.
There can be a scenario where on a day, a process was stop, so it dont have values/qty in table 1/3,
The problem is my query will return only rows which have records in table 1/3.
While I want all the records in table 2 to be returned no matter have data or not in table 1/3, in case no data in table 1/3, return qty as '0'.
Please advice me..thanks in advance :)
Select lot.rw_oper,
lot.rw_area_grp,
lot.rw_oper_name,
proc_out.proc_out,
lot.rw_qty
from
( SELECT UNIQUE a.rw_oper,a.rw_area_grp,a.rw_oper_name,SUM(CASE WHEN a.rw_qty_unit = 'CM' THEN a.rw_qty*10 ELSE rw_qty END )as rw_qty
From rtrwip00 a, oprinf99 b
WHERE a.rw_facility='MIPOPRD'
AND b.oi_facility = a.rw_facility AND b.oi_oper = a.rw_oper
GROUP BY a.rw_area_grp,a.rw_oper,a.rw_oper_name
ORDER BY a.rw_oper,a.rw_area_grp) lot,
( SELECT py_oper, SUM(CASE WHEN py_out_unit1 = 'CM' THEN py_proc_out1*10 ELSE py_proc_out1 END) proc_out
FROM prdyld01
WHERE py_facility = 'MIPOPRD'
AND py_trs_time between TO_CHAR(SYSDATE-1,'YYYYMMDD')||'070000' AND TO_CHAR(SYSDATE,'YYYYMMDD')||'070000'
group by py_oper
)proc_out
where lot.rw_oper NOT IN ('2900','6700' ,'9100' ,'9920' ,'9921','9940','9941','9300','9500','6700','8910', 'REJT')
and lot.rw_oper = proc_out.py_oper
order by lot.rw_oper
CREATE TABLE PRDYLD01
PY_FACILITY VARCHAR2(12 BYTE) NOT NULL,
PY_TRS_TIME VARCHAR2(14 BYTE) NOT NULL,
PY_LOT VARCHAR2(11 BYTE) NOT NULL,
PY_OPER VARCHAR2(4 BYTE) NOT NULL,
PY_PO VARCHAR2(12 BYTE) NOT NULL,
PY_PROC_IN1 NUMBER(10,2) DEFAULT 0 NOT NULL,
PY_PROC_IN2 NUMBER(10,2) DEFAULT 0 NOT NULL,
PY_PROC_OUT1 NUMBER(10,2) DEFAULT 0 NOT NULL,
PY_PROC_OUT2 NUMBER(10,2) DEFAULT 0 NOT NULL,
sample data 1
PY_OPER PROC_OUT
3000 481
3100 214
3200 642
3250 291
5250 519
5300 439
5350 476
CREATE TABLE RTRWIP00
RW_FACILITY VARCHAR2(12 BYTE) NOT NULL,
RW_LOT VARCHAR2(11 BYTE) NOT NULL,
RW_OPER VARCHAR2(4 BYTE) NOT NULL,
RW_OPER_NAME VARCHAR2(35 BYTE) NOT NULL,
RW_PO VARCHAR2(12 BYTE) NOT NULL,
RW_AREA_GRP VARCHAR2(12 BYTE) NOT NULL,
RW_QTY NUMBER(10,2) DEFAULT 0 NOT NULL,
RW_QTY_UNIT VARCHAR2(6 BYTE) NOT NULL,
sample data 2
RW_OPER RW_QTY
3000 1248
3100 324
3200 133
3250 357
4400 424
4500 821
4600 909
5050 689
5250 2472
5300 53
5350 166
CREATE TABLE OPRINF99
OI_FACILITY VARCHAR2(12 BYTE) NOT NULL,
OI_OPER VARCHAR2(4 BYTE) NOT NULL,
OI_OPER_SEQ NUMBER(5) NOT NULL,
OI_AREA_GRP VARCHAR2(12 BYTE),
OI_SHORT_DESC VARCHAR2(10 BYTE) NOT NULL,
OI_LONG_DESC VARCHAR2(35 BYTE),
sample data 3
RW_OPER RW_AREA_GRP RW_OPER_NAME
3000 MOD ROD MOUNT
3100 MOD WIRE SAW
3200 MOD AS-CUT CLEANER
3250 MOD BEAM REMOVAL
4400 POL ESE-II
4500 POL EDGE OXIDE STRIP
4600 POL LTO INSPECTION
5050 POL DOUBLE SIDE BUFFER
5250 POL ACCUMULATION
5300 POL FINAL POLISHER
5350 CLN POST POLISH CLEANER
ABove is the DLL for the table and its mock data.
Sample output should be like below; where when no data found in sample data 1, it will return '0'
RW_OPER RW_AREA_GRP RW_OPER_NAME PROC_OUT RW_QTY
3000 MOD ROD MOUNT 481 1248
3100 MOD WIRE SAW 214 324
3200 MOD AS-CUT CLEANER 642 133
3250 MOD BEAM REMOVAL 291 357
4400 POL ESE-II 0 424
4500 POL EDGE OXIDE STRIP 0 821
4600 POL LTO INSPECTION 0 909
5050 POL DOUBLE SIDE BUFFER 0 689
5250 POL ACCUMULATION 519 2472
5300 POL FINAL POLISHER 439 53
5350 CLN POST POLISH CLEANER 476 166
mY current output is below; only return record that have match to all table.
RW_OPER RW_AREA_GRP RW_OPER_NAME PROC_OUT RW_QTY
3000 MOD ROD MOUNT 481 1248
3100 MOD WIRE SAW 214 324
3200 MOD AS-CUT CLEANER 642 133
3250 MOD BEAM REMOVAL 291 357
5250 POL ACCUMULATION 519 2472
5300 POL FINAL POLISHER 439 53
5350 CLN POST POLISH CLEANER 476 166
Really appreciate your response, sorry for taking time to reply.
Thanks in advance :)
Similar Messages
-
How to process all events with a status of "No Match Found"
Hello Everyone,
I have a requirement of processing records reconciled from the target on the basis of their status as 'No Match Found'. Now I am considering only OIM as a authentic source of Provisioning, so all these records with 'No Match Found' status should get disabled at the target as and when OIM gets an event associated for it.
Can anyone suggest me an idea.I don't believe an entity adapter will work because you cant do a pre-insert, and the event might not have completed yet on the post-insert.
There are no APIs so you will need to create an instance of the OIM database and run the SQL queries and process the information within them and then you can use APIs. If you create a scheduled task, you need to use the following code which should provide you enough information to perform the query:
tcDataSet dsList = new tcDataSet();
String query = "select USR_KEY from USR";
log.debug(query);
try {
dsList.setQuery(getDataBase(), query);
dsList.executeQuery();
if (!dsList.isEmpty()){
log.debug("Total Rows Found:" + dsList.getTotalRowCount());
result = true;
for (int i = 0; i < dsList.getTotalRowCount(); i++) {
dsList.goToRow(i);
System.out.println(dsList.getLong("USR_KEY"));
users.add(dsList.getLong("USR_KEY"));
log.debug("User Key: " + dsList.getLong("USR_KEY"));
}else{
log.debug("No Users Found");
result = false;
} catch (tcDataSetException e) {
e.printStackTrace();
result = false;
-Kevin -
Join two tables with no matching records
Hi All,
I have two tables which have got data as below. Now I need to join those two tables but there are no matching rows or columns in those tables. I have used OUTER JOIN but query was taking more than 5 mnts to run. Whats the best way to join two tables where
there are no matching records.
Table : Sections &Table : orders
I am expecting the final output like I have mentioned below.
How can i write a JOIN. Note there are no matching records from both the tables.
Pls.This is a very urgent requirement.Regards
-pepThanks Elrand. I have one question. What if I want to add 10 more parameters in @Section table then
the logic I have written below will not work. I will have to make it dynamic. Any suggestions.?
declare @test table (RowNo INT,CokeType NVARCHAR(MAX),BeginUsage dateTIME)
declare @Section table (SectionName NVARCHAR(100))
insert into @Section values ('CokeType')
insert into @Section values ('BeginUsage')
insert into @test values (1,'OMV - 02E (04/2012)','01-02-2014')
insert into @test values (2,'OMV - 02E (04/2012)','01-03-2014')
insert into @test values (3,'PCIC - 01 (01E/2013)','01-04-2014')
insert into @test values (4,'PCIC - 01 (01E/2013)','01-05-2014')
insert into @test values (5,'PCIC - 01 (E) - 07/2011 & Alba /2010 (C/F) 05/2011','01-06-2014')
select * from @Section
select * from @test
SELECT
RowNo,SectionName,
CASE
WHEN(SectionName = 'CokeType') THEN CokeType
ELSE
CAST (BeginUsage AS VARCHAR(MAX))
END
AS DATA
FROM @Section AS S
CROSS APPLY
(SELECT T.RowNo,T.CokeType,T.BeginUsage FROM @test T) P
ORDER BY SectionName DESC -
Join two tables, return max 1 record for each left
I want to join tables A and B, but I only want to return a maximum of one record for each record on the left. For example
Table A
Bob,1
John,2
Jill,3
Table B
1,MountainTime
1,MountainTime
2,CentralTime
3,PacificTime
Result
Bob,MountainTime
John,CentralTime
Jill,PacificTimeHi Andrew,
Create a report by joining two tables and insert a group on code(1) and place your fields on goup footer.
This will give only one record.
Thanks,
Sastry -
I am trying to develop a query in MS Access 2010 to join two tables using three joins, one of which is a (between) date range. The tables are contained in Access. The reason
the tables are contained in access because they are imported from different ODBC warehouses and the data is formatted for uniformity. I believe this cannot be developed using MS Visual Query Designer. I think writing a query in SQL would be suiting this project.
ABCPART links to XYZPART. ABCSERIAL links to XYZSERIAL. ABCDATE links to (between) XYZDATE1 and ZYZDATE2.
[ABCTABLE]
ABCORDER
ABCPART
ABCSERIAL
ABCDATE
[ZYXTABLE]
XYZORDER
XYZPART
XYZSERIAL
XYZDATE1
XYZDATE2Thank you for the looking at the post. The actual table names are rather ambiguous. I renamed them so it would make more sense. I will explain more and give the actual names. What I do not have is the actual data in the table. That is something I don't have
on this computer. There are no "Null" fields in either of the tables.
This table has many orders (MSORDER) that need to match one order (GLORDER) in GLORDR. This is based on MSPART joined to GLPART, MSSERIAL joined to GLSERIAL, and MSOPNDATE joined if it falls between GLSTARTDATE and GLENDDATE.
[MSORDR]
MSORDER
MSPART
MSSERIAL
MSOPNDATE
11111111
4444444
55555
2/4/2015
22222222
6666666
11111
1/6/2015
33333333
6666666
11111
3/5/2015
This table has one order for every part number and every serial number.
[GLORDR]
GLORDER
GLPART
GLSERIAL
GLSTARTDATE
GLENDDATE
ABC11111
444444
55555
1/2/2015
4/4/2015
ABC22222
666666
11111
1/5/2015
4/10/2015
AAA11111
555555
22222
3/2/2015
4/10/2015
Post Query table
GLORDER
MSORDER
GLSTARTDATE
GLENDDATE
MSOPNDATE
ABC11111
11111111
1/2/2015
4/4/2015
2/4/2015
ABC22222
22222222
1/5/2015
4/10/2015
1/6/2015
ABC22222
33333333
1/5/2015
4/10/2015
3/5/2015
This is the SQL minus the between date join.
SELECT GLORDR.GLORDER, MSORDR.MSORDER, GLORDR.GLSTARTDATE, GLORDR.GLENDDATE, MSORDR.MSOPNDATE
FROM GLORDR INNER JOIN MSORDR ON (GLORDR.GLSERIAL = MSORDR.MSSERIAL) AND (GLORDR.GLPART = MSORDR.MSPART); -
Creation of Data Control for custom java method which will return records
Hi Guys,
I have a requirement of creating a a custom java method in App module which will return a record set taking an id as input.In case of single return type it works fine but in case of returning record set it is not working.In my case i have to combine two tables and return it as a single entity as a view in Data Control.
Warm Regards,
Srinivas.Why don't you just create a custom view object? There's even an example or 2 in the docs:
http://docs.oracle.com/cd/E16162_01/web.1112/e16182/intro_tour.htm#CHDGDIEC (check out "View object on refcursor" example)
Edit: you are aware that you can create a View Object based on more than one table?
John -
Join 2 tables which are not related with any primary-foriegn key constraint
Hello,
How to join 2 tables which are not related with any primary key foreign key constraint.
Ex.Consider Table A has 5 columns->A_ID,A_Name,A_Address,A_City,A_Pin(Total 10 rows)
Table B has 5 columns->B_ID,B_Name,B_Adress,B_City,B_City(Total 30 rows)
From both the table i want the data,which i need to use in curosr to display finally as "Address Label".
Both the table are entirely different,but there can be some names of Table A which may come in Table B also,if the name,address and city is also same.That means same person in both the table.
So finally i want total number of distinct records(distinct data of Table A which is not in B+all the data of table B) to come under Address Label.
How can i write the select query for this condition?
Thanks
SwapnaHi, Swapna,
user11018268 wrote:
Hello,
How to join 2 tables which are not related with any primary key foreign key constraint.
Ex.Consider Table A has 5 columns->A_ID,A_Name,A_Address,A_City,A_Pin(Total 10 rows)
Table B has 5 columns->B_ID,B_Name,B_Adress,B_City,B_City(Total 30 rows)
Both the table are entirely different,but there can be some names of Table A which may come in Table B also,if the name,address and city is also same.That means same person in both the table.I think you want a UNION, not a join.
If you have 10 rows in table_a, and 30 rows in table_b, then
SELECT A_ID, A_Name, A_Address, A_City, A_Pin
FROM table_a
UNION
SELECT B_ID, B_Name, B_Adress, B_City, B_City
FROM table_b
ORDER BY 2, 4
;will produce up to 40 rows; 40 if there are no duplicates. (UNION implies DISTINCT.)
The corresponding column types should be similar, if not exactly the same.
There's no problem if A_Name is VARCHAR2 (50) and B_Name is CHAR (30).
However, there will be a problem if A_ID is a NUMBER and B_ID is a TIMESTAMP.
You must have the same number of columns in all branches of the UNION.
If you want an ORDER BY clause, put it at the very end, after the last branch of the UNION.
You can use positional notation (where 2 means the 2nd column) like I did, or you can use names from the first prong of the UNION (such as A_Name).
From both the table i want the data,which i need to use in curosr to display finally as "Address Label".
So finally i want total number of distinct records(distinct data of Table A which is not in B+all the data of table B) to come under Address Label.I'm not sure what your mean about "Address Label".
Whenever you have a question, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) from all tables, and the exact results (formatted) that you want from that data.
You can concatenate all 5 columns into one VARCHAR2 column, if you want to.
You'll probably want to use RPAD (or simillar functions) to make the output line up nicely.
If any of the columns are not strings, use TO_CHAR to make string versions of them.
For example:
SELECT TO_CHAR (A_ID, '9999999')
|| ' ' -- leave a little space between the left-justified a_id and the right-justified a_name
|| RPAD (A_Name, 25)
|| RPAD (A_Address, 50)
...Edited by: Frank Kulash on Nov 14, 2009 10:11 AM -
How to fetch the records which matches a partial value?
Hi,
I have a table called Employee which contains the following value in Category column
ID Value
1 Student,Staff
2 Staff
3 Parent,Student
4 Member,Staff,Student
I wanted to fetch only 1 and 3rd and 4th record since it matches the value Student.
basically looking for the exact pattern matched with student separated by comma.
how to perform this query. it would be great, if i get the query in MS Sql also.
Edited by: Pannar on Nov 1, 2009 7:08 PMHi,
I'm sure the regexps will pass by, but here's a way (no idea if this will run in MS SQL, I'm not familiar with that):
HO%xe> with employee as ( --generating your sample data:
2 select 1 id, 'Student,Staff' category from dual union all
3 select 2, 'Staff' from dual union all
4 select 3, 'Parent,Student' from dual union all
5 select 4 ,'Member,Staff,Student' from dual
6 )
7 --
8 -- Actual query:
9 --
10 select id
11 , category
12 from employee
13 where category like '%Student%'
14 and instr(category, ',')>0;
ID CATEGORY
1 Student,Staff
3 Parent,Student
4 Member,Staff,StudentYou can use UPPER or LOWER as well, if you want to be case-insensitive.
edit
Something like:
MHO%xe> with employee as ( --generating your sample data:
2 select 1 id, 'Student,Staff' category from dual union all
3 select 2, 'Staff' from dual union all
4 select 3, 'Parent,Student' from dual union all
5 select 4 ,'Member,Staff,Student' from dual union all
6 select 5, 'Student' from dual union all
7 select 6, 'Student,' from dual
8 )
9 --
10 -- Actual query:
11 --
12 select id
13 , category
14 from employee
15 where regexp_like(category,',Student|Student,');
ID CATEGORY
1 Student,Staff
3 Parent,Student
4 Member,Staff,Student
6 Student,Might work as well.
Edited by: hoek on Nov 1, 2009 2:50 PM -
Does a table/view still return records if the underlying VPD policy fails ?
Hi All,
I have one following question -
Does a table or view still return records if the underlying VPD policy fails?
I am adding one more thing here. Let us say security is being implemented using the application context and VPD.
What would happen when application context is not set ? Would the application apply any default privilege as VPD?
Thanks,
AshiAshiKD wrote:
Hi All,
I have one following question -
Does a table or view still return records if the underlying VPD policy fails?No, you get a standard error message if the VPD function is wrong and if you won't pass the conditions of the VPD, you get nothing. That's what in general happens and should happen as well.
>
I am adding one more thing here. Let us say security is being implemented using the application context and VPD.
What would happen when application context is not set ? Would the application apply any default privilege as VPD?This would be a wrong approach then. If you are truly setting a VPD, you have to make sure that the context is set and the condition variable are passed through it. This is normally done through a logon trigger so again, generically , its not possible that it would not be set. I didn't get at all what ever you said in the last line? What is "default priv for VPD" ?
HTH
Aman.... -
Join two tables which stored in difference database
Hi, i have a problem how to join two tables which are stored in difference databases. I'm using MS access and need to use Jsp to solve it. is anyone have idea to solve it. thxx
Hi,
I think you cannot join the tables which are in two diff databases. You have to connect to each database ,get the result and manipulate in your program. Use a bean.
cheers,
Narayana -
Table Join brings in multiple records
Hi guys,
I have created a table join for 2 HR master tables to get a view.
When i output the view however it brings in multiple records for each employee.
I have set PERNR as the Key field to join the tables but instead of giving me one instance of employee it picks it up 2 records....and in some cases many more, is their a fix for this?
ThanksHi,
Try using a combined primary key;
PERNR, BEGDA and ENDDA
There are more primary keys to be combined but try with this 3.
~BiSu -
How to avoid Duplicate Records while joining two tables
Hi,
I am trying to join three tables, basically two tables are same one is like history table, so I wrote a query like
select
e.id,
e.seqNo,
e.name,
d.resDate,
d.details
from employees e,
((select * from dept)union(select * from dept_hist)) d
join on d.id=e.id and e.seqno=d.seqno
but this returing duplicate records.
Could anyone please tell me how to avoid duplicate records of this query.Actually it is like if the record is processed it will be moved to hist table, so both table will not have same records and I need the record from both the tables so i have done the union of both the tables, so d will have the union of both records.
But I am getting duplicate records if even I am distinct. -
Create a library which joins 2 tables that report painter can use.
Hi,
Please help in resolving following issue:
Create a new library that joins these tables (in client system) GLPCT and ZFKHALIX that report painter could use. If we had this then we can create a P&L report that properly breaks out COGS and SGA in the same way as ou khalix feed .It would also save a lot of time in month end reconciliation between two tables.
ZFKHALIX: SAP->Khalix Interface --- Custom Mapping Table <b><u><i>(Developned)</i></u></b>
GLPCT : EC-PCA: Totals Table <b><u><i>(Standard Table)</i></u></b>
RegardsLibrary can be created only with reference to a single table as in GR21. If you are looking for joins of tables, ABAP Query may be the answer. There, you have the infoset where you can join two or more tables. And based on the infoset you create queries.
-
How to join multiple tables !
Give me the Example to join multiple tables 1
Inner join
IF p_bsart IS INITIAL.
SELECT ekko~bukrs
ekko~lifnr
ekko~ebeln
ekko~waers
ekko~bsart
ekko~ekorg
ekko~ekgrp
ekpo~ebelp
ekpo~txz01
ekpo~matnr
ekpo~werks
ekpo~menge
ekpo~meins
ekpo~netpr
ekpo~netwr
INTO TABLE t_itab1 FROM
ekko INNER JOIN ekpo ON ekkoebeln = ekpoebeln
WHERE ekko~ebeln IN s_ebeln AND
ekko~bukrs IN s_bukrs AND
ekko~lifnr IN s_lifnr AND
ekko~ekorg IN s_ekorg AND
ekko~ekgrp IN s_ekgrp AND
ekpo~matnr IN s_matnr.
The difference between an INNER JOIN and an OUTER JOIN is the following. If a query on an INNER JOIN of VBAK (outer table) and VBAP (inner table) finds a record in VBAK but no matching records in VBAP, then no data is retrieved from the database because the inner table is empty. If you still want to keep VBAK rows for which there are no matching VBAP rows, you need to use the OUTER JOIN construct available in ABAP/4 Open SQL in 4.x..
Hi
Syntax
... [(] {dbtab_left [AS tabalias_left]} | join
{[INNER] JOIN}|{LEFT [OUTER] JOIN}
{dbtab_right [AS tabalias_right] ON join_cond} [)] ... .
Effect
The join syntax represents a recursively nestable join expression. A join expression consists of a left-hand and a right- hand side, which are joined either by means of [INNER] JOIN or LEFT [OUTER] JOIN . Depending on the type of join, a join expression can be either an inner ( INNER) or an outer (LEFT OUTER) join. Every join expression can be enclosed in round brackets. If a join expression is used, the SELECT command circumvents SAP buffering.
On the left-hand side, either a single database table, a view dbtab_left, or a join expression join can be specified. On the right-hand side, a single database table or a view dbtab_right as well as join conditions join_cond can be specified after ON. In this way, a maximum of 24 join expressions that join 25 database tables or views with each other can be specified after FROM.
AS can be used to specify an alternative table name tabalias for each of the specified database table names or for every view. A database table or a view can occur multiple times within a join expression and, in this case, have various alternative names.
The syntax of the join conditions join_cond is the same as that of the sql_cond conditions after the addition WHERE, with the following differences:
At least one comparison must be specified after ON.
Individual comparisons may be joined using AND only.
All comparisons must contain a column in the database table or the view dbtab_right on the right-hand side as an operand.
The following language elements may not be used: BETWEEN, LIKE, IN.
No sub-queries may be used.
For outer joins, only equality comparisons (=, EQ) are possible.
If an outer join occurs after FROM, the join condition of every join expression must contain at least one comparison between columns on the left-hand and the right-hand side.
In outer joins, all comparisons that contain columns as operands in the database table or the view dbtab_right on the right-hand side must be specified in the corresponding join condition. In the WHERE condition of the same SELECT command, these columns are not allowed as operands.
Resulting set for inner join
The inner join joins the columns of every selected line on the left- hand side with the columns of all lines on the right-hand side that jointly fulfil the join_cond condition. A line in the resulting set is created for every such line on the right-hand side. The content of the column on the left-hand side may be duplicated in this case. If none of the lines on the right-hand side fulfils the join_cond condition, no line is created in the resulting set.
Resulting set for outer join
The outer join basically creates the same resulting set as the inner join, with the difference that at least one line is created in the resulting set for every selected line on the left-hand side, even if no line on the right-hand side fulfils the join_cond condition. The columns on the right-hand side that do not fulfil the join_cond condition are filled with null values.
Example
Join the columns carrname, connid, fldate of the database tables scarr, spfli and sflight by means of two inner joins. A list is created of the flights from p_cityfr to p_cityto. Alternative names are used for every table.
PARAMETERS: p_cityfr TYPE spfli-cityfrom,
p_cityto TYPE spfli-cityto.
DATA: BEGIN OF wa,
fldate TYPE sflight-fldate,
carrname TYPE scarr-carrname,
connid TYPE spfli-connid,
END OF wa.
DATA itab LIKE SORTED TABLE OF wa
WITH UNIQUE KEY fldate carrname connid.
SELECT ccarrname pconnid f~fldate
INTO CORRESPONDING FIELDS OF TABLE itab
FROM ( ( scarr AS c
INNER JOIN spfli AS p ON pcarrid = ccarrid
AND p~cityfrom = p_cityfr
AND p~cityto = p_cityto )
INNER JOIN sflight AS f ON fcarrid = pcarrid
AND fconnid = pconnid ).
LOOP AT itab INTO wa.
WRITE: / wa-fldate, wa-carrname, wa-connid.
ENDLOOP.
Example
Join the columns carrid, carrname and connid of the database tables scarr and spfli using an outer join. The column connid is set to the null value for all flights that do not fly from p_cityfr. This null value is then converted to the appropriate initial value when it is transferred to the assigned data object. The LOOP returns all airlines that do not fly from p_cityfr.
PARAMETERS p_cityfr TYPE spfli-cityfrom.
DATA: BEGIN OF wa,
carrid TYPE scarr-carrid,
carrname TYPE scarr-carrname,
connid TYPE spfli-connid,
END OF wa,
itab LIKE SORTED TABLE OF wa
WITH NON-UNIQUE KEY carrid.
SELECT scarrid scarrname p~connid
INTO CORRESPONDING FIELDS OF TABLE itab
FROM scarr AS s
LEFT OUTER JOIN spfli AS p ON scarrid = pcarrid
AND p~cityfrom = p_cityfr.
LOOP AT itab INTO wa.
IF wa-connid = '0000'.
WRITE: / wa-carrid, wa-carrname.
ENDIF.
ENDLOOP. -
Using functions in select statement(joining 5 tables) taking long time in Oracle
Hi,
I have created a query in oracle which joins 5 tables and uses two functions(function names are 'ca_concat' and 'ca_concat_noseq').
Query takes approximately 40 secs to execute around 12000 records. If I remove the functions from query it excutes within a second..
Note : I have used the oracle SQL Developer for testing the query.
It would be appriciated if anybody helps me to improve the perfomance of the query.
Below are the querie with and without functions:
1. Query with functions:
select
imsAuditEvent12.id as ID,
imsAuditEvent12.audit_time as AUDIT_TIME,
imsAuditEvent12.admin_dn as ADMIN_DN,
imsAuditEvent12.admin_name as ADMIN_NAME,
imsAuditEvent12.event_name as EVENT_NAME,
imsAuditEvent12.event_description as EVENT_DESCRIPTION,
imsAuditEvent12.event_state as EVENT_STATE,
imsAuditEvent12.envname as ENVNAME,
imsAuditTaskSession12.task_name as TASK_NAME,
imsAuditTaskSession12.id as TASK_ID,
imsAuditTaskSession12.task_description as TASK_DESCRIPTION,
imsAuditTaskSession12.task_priority as TASK_PRIORITY,
S1.OBJECT_ID,
S1.OBJECT_NAME as OBJECT_NAME,
S1.OBJECT_TYPE as OBJECT_TYPE,
S2.ATTRIBUTE_NAME as ATTRIBUTE_NAME,
S2.ATTRIBUTE_OLDVALUES as ATTRIBUTE_OLDVALUES,
S2.ATTRIBUTE_NEWVALUES as ATTRIBUTE_NEWVALUES,
S3.OBJECT_DN as OBJECT_DN,
S3.OBJECT_TYPE as IMSOBJECT_TYPE,
S3.CONTAINER_NAME as CONTAINER_NAME,
S3.CONTAINER_DN as CONTAINER_DN,
S3.CONTAINER_TYPE as CONTAINER_TYPE
from
imsAuditEvent12 LEFT JOIN imsAuditTaskSession12 ON imsAuditTaskSession12.id=imsAuditEvent12.tasksession_id LEFT JOIN
(select parent_event_id,
ca_concat('imsAuditEventObject12.parent_event_id',parent_event_id,'imsAuditEventObject12.object_name','imsAuditEventObject12') as OBJECT_NAME,
ca_concat('imsAuditEventObject12.parent_event_id',parent_event_id,'imsAuditEventObject12.object_type','imsAuditEventObject12') as OBJECT_TYPE,
ca_concat_noseq('imsAuditEventObject12.parent_event_id',parent_event_id,'imsAuditEventObject12.ID','imsAuditEventObject12') as OBJECT_ID
from
imsAuditEventObject12 group by parent_event_id) S1
ON imsAuditEvent12.id = S1.parent_event_id LEFT JOIN
(select
parent_object_id,
ca_concat('parent_object_id',parent_object_id,'attribute_name','imsauditobjectattributes12') as ATTRIBUTE_NAME,
ca_concat('parent_object_id',parent_object_id,'attribute_oldvalue','imsauditobjectattributes12') as ATTRIBUTE_OLDVALUES ,
ca_concat('parent_object_id',parent_object_id,'attribute_newvalue','imsauditobjectattributes12') as ATTRIBUTE_NEWVALUES
from
imsauditobjectattributes12 group by parent_object_id) S2
ON S1.OBJECT_ID = S2.parent_object_id LEFT JOIN
(select
parent_event_id,
ca_concat('parent_event_id',parent_event_id,'OBJECT_DN','imsauditobjectrelationship12') as OBJECT_DN,
ca_concat('parent_event_id',parent_event_id,'OBJECT_TYPE','imsauditobjectrelationship12') as OBJECT_TYPE ,
ca_concat('parent_event_id',parent_event_id,'CONTAINER_NAME','imsauditobjectrelationship12') as CONTAINER_NAME,
ca_concat('parent_event_id',parent_event_id,'CONTAINER_DN','imsauditobjectrelationship12') as CONTAINER_DN,
ca_concat('parent_event_id',parent_event_id,'CONTAINER_TYPE','imsauditobjectrelationship12') as CONTAINER_TYPE
from
imsauditobjectrelationship12 group by parent_event_id) S3
ON imsAuditEvent12.id =S3.parent_event_id where imsauditevent12.id > 0 and imsauditevent12.id <12000 order by imsauditevent12.id ASC;
2. Query without using functions:
select * from imsauditeventobject12 left join imsauditevent12 on imsauditeventobject12.id=imsauditevent12.id left join imsauditobjectattributes12 on imsauditeventobject12.id=imsauditobjectattributes12.parent_object_id left join imsaudittasksession12 on imsauditevent12.tasksession_id=imsaudittasksession12.id left join imsAuditObjectRelationship12 on imsAuditEvent12.id =imsAuditObjectRelationship12.parent_event_id where imsauditevent12.id >0 and imsauditevent12.id < 12000 order by imsauditevent12.id asc;
Thanks,
BadriHi,
Please find the below more information about the query.
DB version: Oracle 11g Enterprise Edition Release 11.2.0.1.0
Below are source of the functions:
create or replace function ca_concat( ca_key_name in varchar2,
ca_key_val in varchar2,
ca_other_col_name in varchar2,
ca_tname in varchar2 )
return varchar2
as
type rc is ref cursor;
l_str varchar2(32000);
l_sep varchar2(1);
l_val varchar2(32000);
l_count number(6);
l_cur rc;
begin
l_count :=1;
l_str := '';
open l_cur for 'select '|| ca_other_col_name ||'
from '|| ca_tname || '
where ' || ca_key_name || ' = '
using ca_key_val;
loop
fetch l_cur into l_val;
l_val := SUBSTR(l_val,0,102);
exit when (l_cur%notfound or l_count > 38);
l_str := l_str || l_sep || l_count || '.' || l_val;
l_sep := ',';
l_count := l_count + 1;
end loop;
close l_cur;
return l_str;
end;
create or replace function ca_concat_noseq( ca_key_name in varchar2,
ca_key_val in varchar2,
ca_other_col_name in varchar2,
ca_tname in varchar2 )
return varchar2
as
type rc is ref cursor;
l_str nvarchar2(32000);
l_sep varchar2(1);
l_val varchar2(32000);
l_count number(6);
l_cur rc;
begin
l_count :=1;
open l_cur for 'select '||ca_other_col_name||'
from '|| ca_tname || '
where ' || ca_key_name || ' = '
using ca_key_val;
loop
fetch l_cur into l_val;
exit when (l_cur%notfound or length(l_val)>3000 or l_count>1);
l_str := l_str || l_sep || l_val ;
l_sep := ',';
l_count := l_count + 1;
end loop;
close l_cur;
return l_str;
end;
Below are the tables structures:
DESC imsauditevent12;
Name Null Type
ID NOT NULL NUMBER
TASKSESSION_ID NOT NULL NUMBER
TASKSESSION_OID VARCHAR2(100)
PARENT_EVENT_OID VARCHAR2(100)
AUDIT_TIME NOT NULL TIMESTAMP(6)
EVENT_OID NOT NULL VARCHAR2(100)
ADMIN_DN NOT NULL VARCHAR2(512)
ADMIN_NAME VARCHAR2(255)
EVENT_NAME NOT NULL VARCHAR2(255)
EVENT_DESCRIPTION VARCHAR2(4000)
EVENT_STATE VARCHAR2(100)
ENVNAME NOT NULL VARCHAR2(100)
ENV_OID NOT NULL VARCHAR2(100)
DESC imsauditeventobject12;
Name Null Type
ID NOT NULL NUMBER
PARENT_EVENT_ID NOT NULL NUMBER
AUDIT_TIME NOT NULL TIMESTAMP(6)
OBJECT_TYPE NOT NULL VARCHAR2(100)
OBJECT_NAME VARCHAR2(255)
DESC imsauditobjectattributes12;
Name Null Type
ID NOT NULL NUMBER
PARENT_OBJECT_ID NOT NULL NUMBER
AUDIT_TIME NOT NULL TIMESTAMP(6)
DISPLAY_NAME VARCHAR2(255)
ATTRIBUTE_NAME NOT NULL VARCHAR2(255)
ATTRIBUTE_OLDVALUE VARCHAR2(4000)
ATTRIBUTE_NEWVALUE VARCHAR2(4000)
DESC imsaudittasksession12;
Name Null Type
ID NOT NULL NUMBER
PARENT_TS_OID VARCHAR2(100)
PARENT_EVENT_OID VARCHAR2(100)
AUDIT_TIME NOT NULL TIMESTAMP(6)
TASKSESSION_OID NOT NULL VARCHAR2(100)
ADMIN_DN NOT NULL VARCHAR2(512)
ADMIN_NAME VARCHAR2(255)
TASK_NAME VARCHAR2(255)
TASK_TAG NOT NULL VARCHAR2(255)
TASK_DESCRIPTION VARCHAR2(4000)
TASK_PRIORITY NUMBER
STATE NOT NULL VARCHAR2(100)
ENVNAME NOT NULL VARCHAR2(100)
ENV_OID NOT NULL VARCHAR2(100)
DESC imsAuditObjectRelationship12;
Name Null Type
ID NOT NULL NUMBER
PARENT_EVENT_ID NOT NULL NUMBER
AUDIT_TIME NOT NULL TIMESTAMP(6)
OBJECT_TYPE NOT NULL VARCHAR2(100)
OBJECT_DN NOT NULL VARCHAR2(512)
CONTAINER_TYPE NOT NULL VARCHAR2(100)
OBJECT_NAME NOT NULL VARCHAR2(255)
CONTAINER_NAME NOT NULL VARCHAR2(255)
CONTAINER_DN NOT NULL VARCHAR2(512)
OPERATION NOT NULL VARCHAR2(50)
Thanks,
Badri
Maybe you are looking for
-
What does AverageOfChildren aggregate function in SSAS 2005 actually do?
Folks, Have any of you been playing around with SSAS 2005 to have worked out what the AverageOfChildren aggregate function actually does? I was expecting it to do the equivalent of a simple AVG() with a GROUP BY in SQL, but it seems to be doing somet
-
I created one procedure in database objects how can i link this procedure from my home page.Is it possible? null
-
Hi experts I use resource-related billing for a cs order. and also the cs order reference a service contract(contract type wv).I want to know if i can set the billing price for the spare part which will be used in the cs order? because Inow i use dp9
-
Show mail attachments as icons
How to permanently show mail attachments as icons?
-
Ip inspect problem, dropping important connections on 887VAMG router
Hi All. I've got a problem on 887VAMG router. It drops important connections. As customer wants to have a firewall I created ACL and ip inspect rules ,but the router drops their connections to cloud and some websites are not opening. So I removed ACL