FULLT OUTER JOIN on two different selects
HelloI am facing a problem where I don't just get the handle to solve.
I have two pieces of music with different return values. This value should be compared and joined. Simply put, I do an example where the return of the selection is represented as the table1 and table2
Here is the example:
table1
abc_comp_nr, abc_cnt_emp
12000, 15
12500, 10
13000, 30
13500, 10
table2
xyz_comp_nr, xyz_cnt_emp
12000, 15
13000, 30
14000, 20
Output should be this:
comp_nr, abc_cnt_emp, xyz_cnt_emp
12000, 15, 15
12500, 10, 0
13000, 30, 30
13500, 10, 0
14000, 0, 20
comp_nr is the combination of abc_comp_nr or xyz_comp_nrI tried this but did not work as expected:
Select?, abc_cnt_emp, xyz_cnt_emp
Of
Table1 FULL OUTER JOIN table2
ON abc_comp_nr = xyz_comp_nr
Hope that someone was in trouble similar and found a good way to work it.
Thanks in advance
Tobias
Hi, Tobias,.
Tobias Arnhold wrote:
HelloI am facing a problem where I don't just get the handle to solve.
I have two pieces of music with different return values. This value should be compared and joined. Simply put, I do an example where the return of the selection is represented as the table1 and table2
Here is the example:table1 abc_comp_nr, abc_cnt_emp 12000, 15 12500, 10 13000, 30 13500, 10 table2 xyz_comp_nr, xyz_cnt_emp 12000, 15 13000, 30 14000, 20
Whenever you have a problem, post CREATE TABLE and INSERT statements for the sample data.
See the FAQ forum {message identifier: = 9360002}
Output should be this: comp_nr, abc_cnt_emp, xyz_cnt_emp 12000, 15, 15 12500, 10, 0 13000, 30, 30 13500, 10, 0 14000, 0, 20
comp_nr is the combination of abc_comp_nr or xyz_comp_nr
I tried this but did not work as expected:
Select?, abc_cnt_emp, xyz_cnt_emp
Of
Table1 FULL OUTER JOIN table2
ON abc_comp_nr = xyz_comp_nr
Instead of??? you want to
NVL (abc_comp_nr, xyz_comp_nr)
The first column is supposed to be the common identifier, abc_comp_nr, or xyz_comp_nr. It doesn't matter which, since the join condition
ON abc_comp_nr = xyz_comp_nr
said they will be the same, except that it's a FULL OUTER JOIN, if one of them might be null. If you want to display the non NULL when one of them is NULL and or the other (any) when they are both present. It's just that NVL.
You can use NVL on other columns, too, to display 0 instead of NULL when there is no match:
SELECT NVL ( abc_comp_nr, xyz_comp_nr) AS comp_nr
, NVL ( abc_cnt_emp, 0) AS abc_cnt_emp
, NVL ( xyz_cnt_emp, 0) AS xyz_cnt_emp
FROM table1
FULL OUTER JOIN table2 ON abc_comp_nr = xyz_comp_nr
;
However, this will display 0 instead of NULL for abc_cnt_emp and xyz_cnt_emp when these columns are NULL for a reason, not just at the time when they do not reach the join condition. If the original paintings contain NULL values and you should keep NULL values in the result set, but you want NULLs which were created by the outer join to show as 0, then you may want something like this:
SELECT NVL ( abc_comp_nr, xyz_comp_nr) AS comp_nr
, NVL2 ( abc_comp_nr, abc_cnt_emp, 0) AS abc_cnt_emp
, NVL2 ( xyz_comp_nr, xyz_cnt_emp, 0) AS xyz_cnt_emp
FROM table1
FULL OUTER JOIN table2 ON abc_comp_nr = xyz_comp_nr
;
Whenever you use FULL OUTER JOIN, there is an excellent chance that you will want to use NVL (or his brother younger, stronger, COALESCE, or their cousin NVL2) as well.
Published by: Frank Kulash on 3 March 2013 14:49
Tags: Database
Similar Questions
-
Diff "full outer join" is going. "(+)" SELECT syntax?
As far as I know there are two ways to set an outer join:
Select... from tab1 external t1 full join tab2 t2 on t1.id = t2.id;
or:
SELECT... from tab1 tab2 t2, t1 where t1.id = t2.id (+)
They are absolutely equivialent?
Who are the most common one?
Who serve as well on other databases (DB2, MySQL)?
Can I put an additional WHERE clause at the end of the first statement:
SELECT... from tab1 t1 outer join t2 on t1.id = t2.id tab2 complete WHOSE...;Select... from tab1 external t1 full join tab2 t2 on t1.id = t2.id;
It's FULL OUTER JOIN
SELECT... from tab1 tab2 t2, t1 where t1.id = t2.id (+)
It's LEFT OUTER JOIN.
They are quite different.
FULL OUTER JOIN - retrieves all the rows from tab1 and tab2
LEFT OUTER JOIN - gets all the tab1 and lines than the line that matches the join condition of tab2.
Here is a simple test.
create table tab1(id integer) / create table tab2(id integer) / begin insert into tab1 values(1); insert into tab1 values(2); insert into tab1 values(3); end; / begin insert into tab2 values(3); insert into tab2 values(4); insert into tab2 values(5); end; / Select t1.id, t2.id from tab1 t1 full outer join tab2 t2 on t1.id =t2.id / Select t1.id, t2.id from tab1 t1, tab2 t2 where t1.id = t2.id(+) /
Run it and see the result.
-
He had to know the right outer join using...
He had to know the right outer join using...
For example: first query left outer join for the emp table: SELECT EMPNO, ENAME, D.DEPTNO FROM EMP E, Department D WHERE the E.DEPTNO = D.DEPTNO)
Second query left outer join for the Dept table: SELECT EMPNO, ENAME, D.DEPTNO FROM EMP E, Department D WHERE the D.DEPTNO = E.DEPTNO)
In the example above I just Exchange where condition condition to get an outer join of two table with a left outer join itself. Wat is use right outer join, instead, I can swap the status of table name for the result. Please suggest...
Hello
chan001 wrote:
He had to know the right outer join using...
For example: first query left outer join for the emp table: SELECT EMPNO, ENAME, D.DEPTNO FROM EMP E, Department D WHERE the E.DEPTNO = D.DEPTNO)
Second query left outer join for the Dept table: SELECT EMPNO, ENAME, D.DEPTNO FROM EMP E, Department D WHERE the D.DEPTNO = E.DEPTNO)
In the example above I just Exchange where condition condition to get an outer join of two table with a left outer join itself. Wat is use right outer join, instead, I can swap the status of table name for the result. Please suggest...
The two examples above use the old syntax outer join of Oracle. (I guess there should be a sign inside the parentheses, e.g.. +)
... WHERE E.DEPTNO = D.DEPTNO (+)
)
The LEFT OUTER JOIN and RIGHT OUTER JOIN terms apply only to the ANSI join syntax, e. g.
.
.. FROM EMP E
DEPT LEFT OUTER JOIN D ON E.DEPTNO = D.DEPTNO
As Blushadow said above, there's no real point in having LEFT OUTER JOIN and RIGHT OUTER JOIN; What you can do with one (or a combination of both) can be done with the other. Most people use LEFT OUTER JOIN systematically and never use RIGHT OUTER JOIN.
There are situations where using a combination of the two would mean a little less striking, but only a little less and this kind of situation is very common, and one may wonder if the somewhat shorter code is more specific. I suggest that forget you RIGHT OUTER JOIN.
-
Query of queries: error when you try to fake Left Outer Join
Hello
I'm trying to reproduce a left outer join, combining two query of queries using a method I find here
However, I get an error message...
Here is the code I use...
< cfquery dbtype = "query" name = "qry" >
SELECT *.
OF returnQry, returnQry2
WHERE returnQry.mediumImage = returnQry2.mediumImage
ORDER BY returnQry.name
< / cfquery >
< name cfquery = "returnQry3", dbtype = "#application.mx #" >
SELECT *.
OF trackmeanings t
< / cfquery >< cfquery dbtype = "query" name = "endQry" >
SELECT name, nameRcd, mediumImage, comments COUNT (sMessage)
OF qry, returnQry3
WHERE qry.name = returnQry3.sNameTrack
UNION
SELECT name, nameRcd, mediumImage, comments COUNT (sMessage)
OF qry, returnQry3
WHERE #qry.name # NOT IN (#QuotedValueList (returnQry3.sNameTrack) #)
GROUP BY name, nameRcd, mediumImage
< / cfquery >When I try to use the query output in a page, I get the error message "incorrect conditional expression, waiting for an a [as |]» null | between | in | "comparison condition.
Anyone have any ideas?
Thank you very much
The items in the list should be cited. Since you are using quotedvaluelist, it's weird that they aren't. However, if you use cfqueryparam, list = Yes, you should have better luck. Among other things, it will protect you against the apostrophe in names.
-
[8i] need help with full outer join combined with a cross join...
I can't understand how to combine a full outer join with a different type of join... is it possible?
Here are some create table and insert for examples of database:
And, the results that I want to get:CREATE TABLE my_tab1 ( record_id NUMBER NOT NULL , workstation VARCHAR2(4) , my_value NUMBER CONSTRAINT my_tab1_pk PRIMARY KEY (record_id) ); INSERT INTO my_tab1 VALUES(1,'ABCD',10); INSERT INTO my_tab1 VALUES(2,'ABCD',15); INSERT INTO my_tab1 VALUES(3,'ABCD',5); INSERT INTO my_tab1 VALUES(4,'A123',5); INSERT INTO my_tab1 VALUES(5,'A123',10); INSERT INTO my_tab1 VALUES(6,'A123',20); INSERT INTO my_tab1 VALUES(7,'????',5); CREATE TABLE my_tab2 ( workstation VARCHAR2(4) , wkstn_name VARCHAR2(20) CONSTRAINT my_tab2_pk PRIMARY KEY (workstation) ); INSERT INTO my_tab2 VALUES('ABCD','WKSTN 1'); INSERT INTO my_tab2 VALUES('A123','WKSTN 2'); INSERT INTO my_tab2 VALUES('B456','WKSTN 3'); CREATE TABLE my_tab3 ( my_nbr1 NUMBER , my_nbr2 NUMBER ); INSERT INTO my_tab3 VALUES(1,2); INSERT INTO my_tab3 VALUES(2,3); INSERT INTO my_tab3 VALUES(3,4);
I tried a number of different things, google my problem and no luck yet...workstation sum(my_value) wkstn_name my_nbr1 my_nbr2 --------------------------------------------------------------- ABCD 30 WKSTN 1 1 2 ABCD 30 WKSTN 1 2 3 ABCD 30 WKSTN 1 3 4 A123 35 WKSTN 2 1 2 A123 35 WKSTN 2 2 3 A123 35 WKSTN 2 3 4 B456 0 WKSTN 3 1 2 B456 0 WKSTN 3 2 3 B456 0 WKSTN 3 3 4 ???? 5 NULL 1 2 ???? 5 NULL 2 3 ???? 5 NULL 3 4
So, what I want, it's a full outer join of t1 and t2 on workstation and a cross join of one with the t3. I wonder if I can't find examples of it online because it is not possible...SELECT t1.workstation , SUM(t1.my_value) , t2.wkstn_name , t3.my_nbr1 , t3.my_nbr2 FROM my_tab1 t1 , my_tab2 t2 , my_tab3 t3 ...
Note: I'm stuck dealing with Oracle 8i
Thank you!!Hello
The query I posted yesterday is a little more complex that it should be.
My_tab2.workstation is unique, there is no reason to make a separate subquery as mt1. We can join my_tab1 to my_tab2 and get the SUM in a subquery.SELECT foj.workstation , foj.sum_my_value , foj.wkstn_name , mt3.my_nbr1 , mt3.my_nbr2 FROM ( -- Begin in-line view foj for full outer join SELECT mt1.workstation , SUM (mt1.my_value) AS sum_my_value , mt2.wkstn_name FROM my_tab1 mt1 , my_tab2 mt2 WHERE mt1.workstation = mt2.workstation (+) GROUP BY mt1.workstation , mt2.wkstn_name -- UNION ALL -- SELECT workstation , 0 AS sum_my_value , wkstn_name FROM my_tab2 WHERE workstation NOT IN ( -- Begin NOT IN sub-query SELECT workstation FROM my_tab1 WHERE workstation IS NOT NULL ) -- End NOT IN sub-query ) foj -- End in-line view foj for full outer join , my_tab3 mt3 ORDER BY foj.wkstn_name , foj.workstation , mt3.my_nbr1 , mt3.my_nbr2 ;
Thanks for posting the CREATE TABLE and INSERT statements, and very clear expected results!
user11033437 wrote:
... So, what I want, it's a full outer join of t1 and t2 on workstation and a cross join of one with the t3.She, exactly!
The trickiest part is when and how get SUM (my_value). You could address the question of exactly what my_tab3 must be attached to a cross that's exactly what should look like the result set of the full outer join between my_tab1 and my_tab2 to. To do this, take your desired results, remove columns that do not come from the outer join complete and delete duplicate rows. You will get:workstation sum(my_value) wkstn_name ----------- ------------- ---------- ABCD 30 WKSTN 1 A123 35 WKSTN 2 B456 0 WKSTN 3 ???? 5 NULL
So the heart of the problem is how to get these results of my_tab1 and my_tab2, which is done in the subquery FOJ above.
I tried to use auto-documenté in my code names. I hope you can understand.
I could spend hours explaining the different parts of this query more in detail, but I don't know that I would lose some of that time, explain things that you already understand. If you want an explanation of the specific element (s), let me know. -
Bug in outer join syntax?
I use Oracle 10.2, and I think I found a bug in what is allowed for the outer join syntax. Specifically, I am allowed to specify "outer join" without specifying if it's left, right, or full outer join. It behaves as an inner join.
The documents show that the type_de_jointure is optional, but does not allow the 'outside' keyword be used alone: http://download.oracle.com/docs/cd/A97630_01/server.920/a96540/statements_103a.htm#2126207
Small example:
The release of the last three selects shows that the OUTER JOIN behaves as a simple JOIN, rather than return a syntax error or at least behave like a FULL OUTER JOIN (this is where the absence of a syntax error is misleading):create table TABLE_A (ID number(10) primary key, VALUE_A varchar2(50)); create table TABLE_B (ID number(10) primary key, VALUE_B varchar2(50)); insert into TABLE_A (ID, VALUE_A) values (1, 'abc'); insert into TABLE_A (ID, VALUE_A) values (2, 'def'); insert into TABLE_A (ID, VALUE_A) values (3, 'ghi'); insert into TABLE_B (ID, VALUE_B) values (2, 'jkl'); insert into TABLE_B (ID, VALUE_B) values (3, 'mno'); insert into TABLE_B (ID, VALUE_B) values (4, 'pqr'); commit; select ID, VALUE_A from TABLE_A; select ID, VALUE_B from TABLE_B; select ID, VALUE_A, VALUE_B from TABLE_A join TABLE_B using (ID); select ID, VALUE_A, VALUE_B from TABLE_A full outer join TABLE_B using (ID); select ID, VALUE_A, VALUE_B from TABLE_A outer join TABLE_B using (ID);
Y at - there somewhere that I can tell you that?SQL> select ID, VALUE_A, VALUE_B from TABLE_A join TABLE_B using (ID); ID VALUE_A VALUE_B ---------- -------------------------------------------------- -------------------------------------------------- 2 def jkl 3 ghi mno SQL> select ID, VALUE_A, VALUE_B from TABLE_A full outer join TABLE_B using (ID); ID VALUE_A VALUE_B ---------- -------------------------------------------------- -------------------------------------------------- 1 abc 2 def jkl 3 ghi mno 4 pqr SQL> select ID, VALUE_A, VALUE_B from TABLE_A outer join TABLE_B using (ID); ID VALUE_A VALUE_B ---------- -------------------------------------------------- -------------------------------------------------- 2 def jkl 3 ghi mno SQL>
If you have a supported Oracle agreement you can save a Service request with Oracle, but they can answer that this is not a bug. The problem is that the 'outside' keyword in your 3rd example is treated as an alias for TABLE_A because it is not considered as a reserved keyword.
with table_a as ( select 1 as id, 'abc' as value_a from dual union all select 2 as id, 'def' as value_a from dual union all select 3 as id, 'ghi' as value_a from dual ) , table_b as ( select 2 as id, 'jkl' as value_b from dual union all select 3 as id, 'mno' as value_b from dual union all select 4 as id, 'pqr' as value_b from dual ) select ID, outer.VALUE_A, VALUE_B from TABLE_A outer join TABLE_B using (ID); ID VALUE_A VALUE_B ---------------------- ------- ------- 2 def jkl 3 ghi mno
If you query the view RESERVED_WORDS of V$ it will tell you what keywords are reserved.
select * from V$RESERVED_WORDS where keyword in ('OUTER', 'SELECT','USING'); KEYWORD LENGTH RESERVED RES_TYPE RES_ATTR RES_SEMI DUPLICATE ------------------------------ ---------------------- -------- -------- -------- -------- --------- USING 5 N N N N N OUTER 5 N N N N N SELECT 6 Y N N N N
You'll get a similar result, if you tried
select ID, VALUE_A, VALUE_B from TABLE_A using join TABLE_B using (ID);
Kind regards
Bob -
Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - Production 64-bit
With partitioning, OLAP, Data Mining and Real Application Testing options
When I run:
SELECT ACCF. SPECIMEN_ID as ACC_ID,
ACCF. PREFIX,
ACCF. SPECIMEN_NBR as ACC_NBR,
RP. PHYSICIAN_ID as REFPHY_BUS_KEY,
ACCF. SIGNOUTLOC,
THE. Location_id as LAB_BUS_KEY,
ACCF. COLLDATE as ACC_SPCMN_COLL_DT_ID,
ACCF. ACDATE as ACC_CREATED_DT_ID,
ACCF. SODATEORIG as ACC_ORIG_SIGNOUT_DT_ID,
ACCF. SODATE as ACC_SIGNOUT_DT_ID
OF ACC_FACT_WS ACCF.
REFPHY_WS RP,
THE LAB_WS
WHERE ACCF. BLINK = RP. PHYSICIAN_ID
AND ACCF. ACDATE > to_date('2010-06-17','YYYY-MM-DD')
AND ACCF. PREFIX = A '
AND ACCF. SIGNOUTLOC = (LOUISIANA). LOCATION_ID
It works fine, but I really have an outer join on LAB_WS.
When I run with an outer join, I get:
SQL > SELECT ACCF. SPECIMEN_ID as ACC_ID,
2 ACCF. PREFIX,
3 ACCF. SPECIMEN_NBR as ACC_NBR,
4. PR PHYSICIAN_ID as REFPHY_BUS_KEY,
5 ACCF. SIGNOUTLOC,
6. THE. Location_id as LAB_BUS_KEY,
ACCF 7. COLLDATE as ACC_SPCMN_COLL_DT_ID,
ACCF 8. ACDATE as ACC_CREATED_DT_ID,
ACCF 9. SODATEORIG as ACC_ORIG_SIGNOUT_DT_ID,
ACCF 10. SODATE as ACC_SIGNOUT_DT_ID
11 ACC_FACT_WS ACCF,
12 REFPHY_WS RP
13 LEFT OUTER JOIN LAB_WS ON ACCF. SIGNOUTLOC = (LOUISIANA). LOCATION_ID
14. WHERE ACCF. BLINK = RP. PHYSICIAN_ID
15 AND ACCF. ACDATE > to_date('2010-06-17','YYYY-MM-DD')
16 AND ACCF. PREFIX = A ';
LEFT OUTER JOIN LAB_WS ON ACCF. SIGNOUTLOC = (LOUISIANA). LOCATION_ID
*
ERROR on line 13:
ORA-00904: "ACCF. "" SIGNOUTLOC ": invalid identifier
The previous query shows ACCF. SIGNOUTLOC is not the problem.
What is the problem and how to fix it?
Note: the syntax of the old outer join is not an option. The query will be finally 9 outer joins.
Thank you
Jon JacobsHello
You are mixing syntax to join Oracle with ANSI, which is sometimes delicate.
Best is to use a unique syntax, for example ANSI:... FROM ACC_FACT_WS ACCF JOIN REFPHY_WS RP ON ACCF.CLIN = RP.PHYSICIAN_ID LEFT OUTER JOIN LAB_WS LA on ACCF.SIGNOUTLOC = LA.LOCATION_ID WHERE ACCF.ACDATE > to_date('2010-06-17','YYYY-MM-DD') AND ACCF.PREFIX = 'D'
-
Left join between two tables using two different conditions
I have following three tables with their data, as shown below.
CREATE TABLE TIREMASTERPROCESS_TEMP
(
PRODUCTIONCODE TANK (12 BYTES),
FIELDNAME CHAR (12 BYTES),
DATACHR VARCHAR2 (60 BYTE),
REVISIONNO TANK (3 BYTES),
DATANUM NUMBER (9.4)
)
Whether the table c. and its sample data are like
FIELDNAME DATACHR REVISIONNO DATANUM PRODUCTIONCODE
AB No. Nch 1 100 0
AB No. Nch 1 108 0
AB No.-of-Nch 1 1 0
Ass42 teased dishes 1 0
Ass42 BTDrumNo1 BTD-051 1 0
AB53 BTDrumNo1 BTD-051 104 0
CREATE TABLE materialcode
(
UPPERMATERIALCODE CHAR (20),
PROCESSNO TANK (3),
PROCESSADOPTDATE TANK (9)
)
Whether the table b. and its sample data are like
UPPERMATERIALCODE PROCESSNO PROCESSADOPTDATE
Ass42 1 20120717
AB53 108 20121121
111 20111104 AS05
104 20120928 AS30
CREATE TABLE BUILDINGSCHEDULEMASTER
(
BUILDINGSIZE TANK (5 BYTES),
GTCODE FLOAT (4 BYTES),
TIREPERCART NUMBER (3.0).
BUILDINGLOT NUMBER (3.0)
)
Whether table and its sample data are like
BUILDINGSIZE GTCODE TIREPERCART BUILDINGLOT
AB42A 4 12 ass42
AB53A 4 88 AB53
AS30A AS30 4 80
BF03A 1 90 BF03
Now, I need to get some data in these fields by using the suite of mappings.
« PR_ » || Trim (a.BUILDINGSIZE) | ' / MPL2' Processid,--PRIMARY KEY
TO_DATE (substr (NVL(b.PROCESSADOPTDATE,'19800101'), 7, 2) |) » /'|| SUBSTR (NVL(b.PROCESSADOPTDATE,'19800101'), 5, 2) | » /'|| SUBSTR (NVL(b.PROCESSADOPTDATE,'19800101'), 1, 4). (' 01:00:00 ',' dd-mm-yyyy hh24:mi:ss') starteff, to_date (SUBSTR (NVL(b.PROCESSADOPTDATE,'19800101'), 7, 2) |) » /'|| SUBSTR (NVL(b.PROCESSADOPTDATE,'19800101'), 5, 2) | » /'|| SUBSTR (NVL(b.PROCESSADOPTDATE,'19800101'), 1, 4). (' 01:00:00 ',' dd-mm-yyyy hh24:mi:ss') startDate.
b.PROCESSNO revisioncodeid,
a.BUILDINGLOT tirepercart ,
CASE WHEN c.FIELDNAME = 'BTDrumNo1' then c.DATACHR end drumtype1.
BOX WHEN (trim (C.DATACHR) = '1' and trim (C.FIELDNAME) = "No. Nch") and THEN click 'YES '.
ANOTHER 'NO '.
END may-BUG,
case
When c.FIELDNAME = 'Wavy'
AND c.DATACHR = 'Use' THEN on 'YES '.
When c.FIELDNAME = 'Wavy'
AND c.DATACHR <>'Use' THEN 'NO '.
fine wavy
Now for the tables A and B I have the simple join as condition
a.GTCODE = b.UPPERMATERIALCODE
But I have to use two different left join conditions to join the table B and C to calculate the RESP fields according to the condition.
Now, I have to use
(trim (b.UPPERMATERIALCODE)). » -'|| Trim (b.PROCESSNO) = Trim (c.PRODUCTIONCODE) | » -'|| Trim (c.REVISIONNO)
condition to get the data for the WAVY and DRUMTYPE1 fields.
But at the same time, I have to use
substr (Trim (b.UPPERMATERIALCODE), 1, 2). » -'|| Trim (b.PROCESSNO) = Trim (c.PRODUCTIONCODE) | » -'|| Trim (c.REVISIONNO)
conition to derive the CHAFER field.
And for this reason I am come so many duplicates and do not correct results for JUNEBUG, WAVY, DRUMTYPE1, and STARTDATE field.
I need some advice as how can I do so that I get the correct results without duplicates for the PRIMARY KEY.
Thank you
MaheshIt would have been easier to answer if you gave us your example query that gives incorrect results - which both show us what you are doing and give us a starting point.
Insert instructions for data, rather than only the data would have helped too, making it more quick to test this point.
You do not show what you have done with your two outer join criteria, but I think you need to list TIREMASTERPROCESS_TEMP in your query twice, with alias names. Here's what I did:select a.BUILDINGSIZE, a.GTCODE, b.PROCESSNO , c1.FIELDNAME, c1.DATACHR, C2.FIELDNAME, C2.DATACHR , CASE WHEN c1.FIELDNAME='BTDrumNo1' then c1.DATACHR end drumtype1 , CASE WHEN (trim(c2.DATACHR) = '1' and trim(c2.FIELDNAME) = 'No-of-Nch') THEN 'YES' ELSE 'NO' END CHAFER , case when c1.FIELDNAME='Wavy' AND c1.DATACHR='Use' THEN 'YES' when c1.FIELDNAME='Wavy' then 'NO' else null end wavy from BUILDINGSCHEDULEMASTER A join materialcode b on a.GTCODE=b.UPPERMATERIALCODE left outer join TIREMASTERPROCESS_TEMP c1 ON trim(b.UPPERMATERIALCODE)||'-'||trim(b.PROCESSNO) = trim(c1.PRODUCTIONCODE)||'-'||trim(c1.REVISIONNO) left outer join TIREMASTERPROCESS_TEMP c2 ON substr(trim(b.UPPERMATERIALCODE),1,2)||'-'||trim(b.PROCESSNO) = trim(c2.PRODUCTIONCODE)||'-'||trim(c2.REVISIONNO)
C1 is an alias for the TIREMASTERPROCESS_TEMP line which is necessary for drumtype1 and wavy, c2 is an alias for the TIREMASTERPROCESS_TEMP line required for JuneBug. There was some minor errors in your calculations of field I tried to correct.
The results are:
BUILD GTCO PRO FIELDNAME DATACHR FIELDNAME DATACHR DRUMTYPE1 CHA WAV ----- ---- --- ------------ ---------- ------------ ---------- ---------- --- --- AB53A AB53 108 No-of-Nch 1 YES AB42A AB42 1 BTDrumNo1 BTD-051 No-of-Nch 1 BTD-051 YES AB42A AB42 1 Wavy NotUse No-of-Nch 1 YES NO AS30A AS30 104 NO
Hope this helps,
David -
using outer joins if the two column is null? Use only (+)
Hi all
create the table xxc_tr_num (tl_number number, tr_no number tl_no_id);
insert into xxc_tr_num values (123,100,222);
insert into xxc_tr_num values (124,100,333);
create the table xxc_od_tab (tl_number number, tl_id number);
insert into xxc_od_tab values (123,001);
insert into xxc_od_tab values (null, null);
create table xxc_oth_tab (name varchar2 (10), number of tl_id);
insert into xxc_oth_tab values('abc',,001);
insert into xxc_oth_tab values (null, null);
Wait it out put
tr_no tl_no_id name
100 222 abc
100 333
using outer joins if the two column is null? use only please of outer joins
And I tried to use outer joins on both tl_id column but not get values and I use have County (tr_no ) > 1
Rajesh123 wrote:
Thank you Kiss it is not possible to use having clause?
You need to understand the functioning of the group. If you will not be asked this question.
Check this box
SQL> select tr_no, 2 tl_no_id, 3 count(*) 4 from xxc_tr_num a, 5 xxc_od_tab b, 6 xxc_oth_tab c 7 where a.tl_number = b.tl_number(+) 8 and b.tl_id = c.tl_id(+) 9 group 10 by tr_no 11 , tl_no_id; TR_NO TL_NO_ID COUNT(*) ---------- ---------- ---------- 100 333 1 100 222 1
See what returns the count? You have grouped according to TR_NO and TL_NO_ID. You must take into consideration the TL_NO_ID just put COUNT (TR_NO) does not increase the NUMBER of the whole group. To get the NUMBER on the whole group, I used the analytical function and did. Like this, see the number of the analytical function here
SQL> select tr_no, 2 tl_no_id, 3 count(*), 4 count(*) over(partition by tr_no) 5 from xxc_tr_num a, 6 xxc_od_tab b, 7 xxc_oth_tab c 8 where a.tl_number = b.tl_number(+) 9 and b.tl_id = c.tl_id(+) 10 group 11 by tr_no 12 , tl_no_id; TR_NO TL_NO_ID COUNT(*) COUNT(*)OVER(PARTITIONBYTR_NO) ---------- ---------- ---------- ------------------------------ 100 222 1 2 100 333 1 2
So to answer your question, yes you can't do in the HAVING clause...
-
I have 2 tables, INVOICE and INVOICE_REFNUM similar to below. I'm writing a query that withdrawing the first date (less) for a single record remaining the two tables. Each table records the receipt of invoices, but according to the source of the received invoice some may reside in INVOICE_REFNUM and others the INVOICE table. I also use DECODE on the topic of INVOICE_SOURCE, when the date less is taken from the INVOICE table that contains that field, so if the date is in the field INVOICE_REFNUM DECODE is not necessary. Example:
INVOICE (table)
INVOICE_GID INVOICE_SOURCE INSERT_DATE
ABC. AVRT123 I HAVE 11/03/2012-15:49:32
ABC. CNWY234 G 12/03/2012 14:07:30
ABC. UPGF678 M 15/03/2012 20:21:54
INVOICE_REFNUM (table)
INVOICE_GID INVOICE_REFNUM_QUAL INVOICE_REFNUM_VALUE INSERT_DATE
ABC. AVRT123 SOURCE ACS 11/03/2012 14:49:32
ABC. CNWY234 SOURCE CSA 12/03/2012 13:07:30
ABC. UPGF678 SOURCE ACS 15/03/2012 21:21:54
My decode statement is: DECODE ("INVOICE_SOURCE, 'I', 'IDE', 'G', ' Auto-Pay", ', 'Manual')
If I were to write a query on these two tables, the output will resemble the following:
INVOICE_GID INVOICE_SOURCE INSERT_DATE
ABC. AVRT123 ACS 11/03/2012 14:49:32
ABC. CNWY234 ACS 12/03/2012 13:07:30
ABC. UPGF678 Manual 15/03/2012 20:21:54
I worked on it for 2 days, and I was able to pull a date by using the following SQL code, but cannot determine how to extract the corresponding source:
SELECT THE LEAST (GLOGOWNER. INVOICE_REFNUM. INSERT_DATE, GLOGOWNER. THE INVOICE. INSERT_DATE) AS 'DATE OF THE INVOICE.
GLOGOWNER. THE INVOICE. INVOICE_GID,
DECODE (GLOGOWNER. THE INVOICE. INVOICE_SOURCE, 'I', 'IDE', 'G', 'Auto-Pay', ', 'Manual').
GLOGOWNER. INVOICE_REFNUM. INVOICE_REFNUM_VALUE
OF GLOGOWNER. INVOICE
LEFT JOIN GLOGOWNER. INVOICE_REFNUM
ON GLOGOWNER. THE INVOICE. INVOICE_GID = GLOGOWNER. INVOICE_REFNUM. INVOICE_GID
WHERE GLOGOWNER. INVOICE_REFNUM. INVOICE_REFNUM_VALUE = "ACS."
AND GLOGOWNER. INVOICE_REFNUM. INVOICE_REFNUM_QUAL_GID = 'SOURCE '.
AND GLOGOWNER. THE INVOICE. INVOICE_TYPE = 'I '.
ORDER OF GLOGOWNER. THE INVOICE. INVOICE_GID
Any help is greatly appreciated!
Best regards
-Adam
Hello
Is that what you want?
SELECT i.invoice_gid
CASE
WHEN ir.insert_date<= i.insert_date ="" then ="">=>
WHEN i.invoice_souce = 'I' and THEN 'EDI '.
WHEN i.invoice_souce = 'G' THEN 'debit '.
WHEN i.invoice_souce = am' THEN 'manual '.
END AS invoice_source
, The LEAST (i.insert_date, ir.insert_date) AS insert_date
I have the Bill OF
JOIN invoice_refnum ON i.invoice_gid = ir.invoice_gid ir
WHERE ir.invoice_refnum_value = 'ACS '.
AND ir.invoice_refnum_qual_gid = 'SOURCE '.
AND i.invoice_type = 'I '.
ORDER BY i.invoice_gid
;
There is no reason to do an outer join if you only want the lines with specific values anyway.
What happens if the 2 tables have exactly the same invoice_date? The query above takes the invoice_source of invoice_refnum in this case, but that can easily be changed.
Or the other of the insert_dates can be NULL?
I hope that answers your question.
Otherwise, thank you post a small example data (CREATE TABLE and only relevant columns, INSERT statements) for all of the tables involved and also after the results you want from these data (otherwise what you have already posted).
Explain, using specific examples, how you get these results from these data.
Always say what version of Oracle you are using (for example, 11.2.0.2.0).
See the FAQ forum: https://forums.oracle.com/message/9362002#9362002
-
How do the query select outer join to a report of the APEX
Hi all
I'm Ann.
I have a select statement that is used to calculate statistics for a month (October 2012 in this example)
Select ph.phase_number
sum ((case
WHEN ph.date_finished IS NULL or ph.date_finished > last_day (TO_DATE (' ' Oct 2012 ', ' MY YYYY' "))
THEN last_day (TO_DATE (' ' Oct 2012 ', ' MY YYYY' "))
Of OTHER ph.date_finished
END)
(-ph.date_started + 1) / count (def.def_id) as avg_days
Ph phase_membership
inner join court_engagement this on ph.mpm_eng_id = ce.engagement_id
join in-house defendant def on ce.defendant_id = def.def_id
where def.active = 1
and ph.date_started < = last_day (TO_DATE (' ' Oct 2012 ', ' MY YYYY' "))
and ph.active = 1
and UPPER (ce.court_name) LIKE '% '.
Rollup Group (phase_number)
;
The result is as below
Phase_Number AVG_DAYS
Phase One 8.6666666666666667
Phase two 14.6
Phase three 12
11.4615365
I have another list of selection mainly the list of months between two date value.
Select to_char (which_month, 'LUN YYYY') as display_month
de)
Select add_months (to_date (' ' August 2012 ', ' MY YYYY' "), rownum-1) which_month
of object
where
rownum < = months_between (to_date (' ' Oct 2012 ', ' MY YYYY' "), add_months (to_date (' ' August 2012", "MY YYYY"), - 1))
order of which_month)
The query result is as below
DISPLAY_MONTH
AUGUST 2012
SEP 2012
OCT 2012
Is it possible I can join these two select statement above to generate a comparable result:
Days of month Phase number Avg
August 2012 Phase One 8.666
Sep 2012 Phase One 7.66
Oct 2012 Phase One 5,66
August 2012 Phase two 8.666
Sep 2012 Phase two 7.66
Oct 2012 Phase two 5,66
August 2012 Phase three 8.666
Sep 2012 Phase three 7.66
Oct 2012 Phase three 5,66
Or
Days of month Phase number Avg
August 2012 Phase One 8.666
August 2012 Phase two 7.66
August 2012 Phase three 5,66
Sep 2012 Phase One 8.666
Sep 2012 Phase two 7.66
Sep 2012 Phase three 5,66
Oct 2012 Phase One 8.666
Oct 2012 Phase two 7.66
Oct 2012 Phase three 5,66
And it can be controlled by Phase number or month.
My other colleague suggested I should use a left outer join, but after having tried many ways, I'm still stuck.
I tried select is
Select a.display_month, b.* in)
Select to_char (which_month, 'LUN YYYY') as display_month
de)
Select add_months (to_date (' ' August 2012 ', ' MY YYYY' "), rownum-1) which_month
of object
where
rownum < = months_between (to_date (' ' Oct 2012 ', ' MY YYYY' "), add_months (to_date (' ' August 2012", "MY YYYY"), - 1))
order which_month)) a left outer join
(Select to_char (ph.date_finished, 'MY YYYY') as join_month, ph.phase_number)
sum ((case
WHEN ph.date_finished IS NULL or ph.date_finished > last_day (TO_DATE (a.display_month, 'MY YYYY'))
THEN last_day (TO_DATE (a.display_month, 'MY YYYY'))
Of OTHER ph.date_finished
END)
(-ph.date_started + 1) / count (def.def_id) as avg_days
Ph phase_membership
inner join court_engagement this on ph.mpm_eng_id = ce.engagement_id
join in-house defendant def on ce.defendant_id = def.def_id
where def.active = 1
and ph.date_started < = last_day (TO_DATE (a.display_month, 'MY YYYY'))
and ph.active = 1
and UPPER (ce.court_name) LIKE '% '.
To_char (ph.date_finished, 'MY YYYY'), group (phase_number) rollup) b
On a.display_month = b.join_month
but then I get an error
SQL error: ORA-00904: "A." "" DISPLAY_MONTH ": invalid identifier
I need to view a report on the APEX with option for people to download at least format CSV.
Already 1 inteactive report in the page, so I don't think adds another interactive report without using the iframe trick.
If any of you have any ideas, please help.
Thank you very much.
AnnHello Ann,.
Frank has done a very good job. I am also impressed.
Is in regard to your correction to his question, the problem is on this replacement you did
last_day(TO_DATE(am.which_month,'MON YYYY'))
AM.which_month is already a date type, and you don't need to convert it to this day.
Here is the correct way:last_day(am.which_month)
There are also sometimes with the data you've posted have no line for this month. So I also added a function NVL to display 0 under avg_days for these cases.
Here is my corrected query:
DEFINE startmonth = "Aug 2012"; DEFINE endmonth = "Oct 2012"; WITH all_months AS ( SELECT ADD_MONTHS(to_date('&startmonth','MON YYYY'), ROWNUM-1) AS which_month , ADD_MONTHS(to_date('&startmonth','MON YYYY'), ROWNUM ) AS next_month from all_objects where rownum <= months_between(to_date('&endmonth','MON YYYY'), add_months(to_date('&startmonth','MON YYYY'), -1)) ) SELECT TO_CHAR (am.which_month, 'Mon YYYY') AS month , ph.phase_number , NVL(sum ( (CASE WHEN ph.date_finished IS NULL OR ph.date_finished > last_day(am.which_month) THEN last_day(am.which_month) ELSE ph.date_finished END ) - ph.date_started + 1 ) / count(def.def_id), 0) as avg_days FROM all_months am LEFT OUTER JOIN a_phase_membership ph PARTITION BY (ph.phase_number) ON am.which_month <= ph.date_started AND am.next_month > ph.date_started AND ph.date_started <= last_day(am.which_month) -- May not be needed AND ph.active = 1 LEFT OUTER JOIN a_engagement ce ON ph.mpm_eng_id = ce.engagement_id AND ce.court_name IS NOT NULL -- or something involving LIKE LEFT OUTER join a_defendant def ON ce.defendant_id = def.def_id AND def.active = 1 GROUP BY ROLLUP(phase_number, am.which_month) ORDER BY am.which_month , ph.phase_number ; The output is: MONTH PHASE_NUMBER AVG_DAYS -------- -------------------- ---------- Aug 2012 PHASE ONE 0 Aug 2012 PHASE THREE 0 Aug 2012 PHASE TWO 0 Sep 2012 PHASE ONE 12 Sep 2012 PHASE THREE 1 Sep 2012 PHASE TWO 9 Oct 2012 PHASE ONE 8 Oct 2012 PHASE THREE 0 Oct 2012 PHASE TWO 14 PHASE ONE 11 PHASE THREE 1 PHASE TWO 11.5 9.71428571
I don't know if that's really what you want. In the case check it and let me know.
Kind regards.
Al -
Join of two tables in two different schemas
Hi all
I have an obligation to join two tables on two different schemas. How to join these two tables in the object view.
Thanks in advance.
Concerning
Kaushik GuillauminYou can do just that using schema name in the +' view object.table name ' + according to the query object and also grant select the another schema to this schema user
ex
you need schem is test and another Act you need to a view object based on the test to join a table on shcema Actyou write semply object sql view code:
Act.table name
and you can also give him select statement on Bill schem table to test
concerning
-
Different Outer Join query result
Hi all
Under query gives the expected result
Select DECODE (FPOVR. PROFILE_OPTION_VALUE,
NULL, FPOVS. PROFILE_OPTION_VALUE, FPOVR. ZZ PROFILE_OPTION_VALUE)
OPS FND_PROFILE_OPTIONS, FND_PROFILE_OPTION_VALUES FPOVR,.
FND_PROFILE_OPTION_VALUES FPOVS where 1 = 1 and FPOVS. LEVEL_ID (+) = 10001
and FPOVS. LEVEL_VALUE (+) = 0
and FPOVS. PROFILE_OPTION_ID (+) IS FRENCH POLYNESIA. PROFILE_OPTION_ID
and FPOVR. (+) LEVEL_ID = 10003 and FPOVR. LEVEL_VALUE (+) = 21533
and FPOVR. PROFILE_OPTION_ID (+) IS FRENCH POLYNESIA. PROFILE_OPTION_ID
and fpo.profile_option_name = 'GL_SET_OF_BKS_ID. '
But the following query does not give the expected result.
Select DECODE (FPOVR. PROFILE_OPTION_VALUE,
NULL, FPOVS. PROFILE_OPTION_VALUE, FPOVR. ZZ PROFILE_OPTION_VALUE)
OPS FND_PROFILE_OPTIONS, FND_PROFILE_OPTION_VALUES FPOVR,.
FND_PROFILE_OPTION_VALUES FPOVS where 1 = 1 and FPOVS. LEVEL_ID (+) = 10001
and FPOVS. LEVEL_VALUE (+) = 0
and FPOVS. PROFILE_OPTION_ID (+) IS FRENCH POLYNESIA. PROFILE_OPTION_ID
and FPOVR. (+) LEVEL_ID = 10003 and FPOVR. LEVEL_VALUE = 21533
and FPOVR. PROFILE_OPTION_ID (+) IS FRENCH POLYNESIA. PROFILE_OPTION_ID
and fpo.profile_option_name = 'GL_SET_OF_BKS_ID. '
Difference between them is a join with external FPOVR. LEVEL_VALUE and hard coded value. Why these query above return different results even if the outer join is made with hard coded in value?
Thank you
SKSK says:
Hi allUnder query gives the expected result
Select DECODE (FPOVR. PROFILE_OPTION_VALUE,
NULL, FPOVS. PROFILE_OPTION_VALUE, FPOVR. ZZ PROFILE_OPTION_VALUE)
OPS FND_PROFILE_OPTIONS, FND_PROFILE_OPTION_VALUES FPOVR,.
FND_PROFILE_OPTION_VALUES FPOVS where 1 = 1 and FPOVS. LEVEL_ID (+) = 10001
and FPOVS. LEVEL_VALUE (+) = 0
and FPOVS. PROFILE_OPTION_ID (+) IS FRENCH POLYNESIA. PROFILE_OPTION_ID
and FPOVR. (+) LEVEL_ID = 10003 and FPOVR. LEVEL_VALUE (+) = 21533
and FPOVR. PROFILE_OPTION_ID (+) IS FRENCH POLYNESIA. PROFILE_OPTION_ID
and fpo.profile_option_name = 'GL_SET_OF_BKS_ID. 'But the following query does not give the expected result.
Select DECODE (FPOVR. PROFILE_OPTION_VALUE,
NULL, FPOVS. PROFILE_OPTION_VALUE, FPOVR. ZZ PROFILE_OPTION_VALUE)
OPS FND_PROFILE_OPTIONS, FND_PROFILE_OPTION_VALUES FPOVR,.
FND_PROFILE_OPTION_VALUES FPOVS where 1 = 1 and FPOVS. LEVEL_ID (+) = 10001
and FPOVS. LEVEL_VALUE (+) = 0
and FPOVS. PROFILE_OPTION_ID (+) IS FRENCH POLYNESIA. PROFILE_OPTION_ID
and FPOVR. (+) LEVEL_ID = 10003 and FPOVR. LEVEL_VALUE = 21533
and FPOVR. PROFILE_OPTION_ID (+) IS FRENCH POLYNESIA. PROFILE_OPTION_ID
and fpo.profile_option_name = 'GL_SET_OF_BKS_ID. 'Difference between them is a join with external FPOVR. LEVEL_VALUE and hard coded value.
In fact, the difference is that the condition relating to FPOVR. LEVEL_VALUE to 21533 is part of the outer join condition in the first query, but it is not in the second query.
Why these query above return different results even if the outer join is made with hard coded in value?
When you say
FPOVR.LEVEL_VALUE(+) = 21533
(with a sign +) only the lines of fpovr with level_value = 21533 will join the other table. If there is no such line in fpovr, then a row of output will be generatedd for each row in the other table anyway.
When you sayFPOVR.LEVEL_VALUE = 21533
(without a sign +) then the outer join is made using the other conditions, which may result in some lines of the other table who appear in the result set with NULL in places where the values of fpovr would normally appear. Then when the join is complete, the condition is applied, and all these lines with the NULL value instead of fpovr.level_value are deleted because they do not meet the condition "FPOVR. LEVEL_VALUE = 21533 ". The effect is identical to an inner join.
When you use the old rating of outer join (i.e. with signs +), if any condition involving the fpovr table has a + sign, and then all of the conditions involving table fpovr must have a + sign, otherwise the result will be the same if none of the conditions were a sign +.
-
outer join: difference between two queries
Below two queries which should give the same results in my opinion. I want that all records of u_protocol and the pval.u_protocol_variable_value value if necessary.
Why the outer join in query2 does not work as in query1?
Query1:
Query2:select p.u_protocol_id, i.u_protocol_variable_value from lims_sys.u_protocol p, ( select pval.u_protocol_id, pval.u_protocol_variable_value from lims_sys.u_protocol_variable pvar, lims_sys.u_protocol_value_user pval where pvar.u_protocol_variable_id = pval.u_protocol_variable_id and pvar.name = 'VALUE_Protocol_Group' ) i where p.u_protocol_id = i.u_protocol_id (+)
select prt.u_protocol_id, pval.u_protocol_variable_value from lims_sys.u_protocol prt, lims_sys.u_protocol_variable pvar, lims_sys.u_protocol_value_user pval where pvar.u_protocol_variable_id = pval.u_protocol_variable_id and prt.u_protocol_id = pval.u_protocol_id (+) and pvar.name = 'VALUE_Protocol_Group'
Hello
When you perform an outer join, all conditions on the tables in option must be outer join conditions. Otherwise, the effect is an inner join.
There is one exception: cascade of outer joins, external-junction b of a and c is outside attached to b. In this case, in the b - c join only columns of c are marked with the sign +.Re - write to obtain the same results as query1, query2 like this:
select prt.u_protocol_id , pval.u_protocol_variable_value from lims_sys.u_protocol prt , lims_sys.u_protocol_variable pvar , lims_sys.u_protocol_value_user pval where pvar.u_protocol_variable_id (+) = pval.u_protocol_variable_id -- cascading outer join and prt.u_protocol_id = pval.u_protocol_id (+) and pvar.name (+) = 'VALUE_Protocol_Group' -- (+) added ;
I find the syntax ANSI easier to use for all the joins and much easier for outer joins.
-
Problem using Extract IMAQ. Don't not out two different images
Hi, I am using the multiple functions of extract IMAQ for some parts of the image of the webcam, then using color extracted from function to get values of RGB. The problem is that two functions extract IMAQ give the same image on its out port. The extract of diagram of block and VI are attached below. Also, please let me know if there is a better way to do this. I need to expand this later to extract parts of the image instead of two different about 40 - 50 RGB values below.
It's happened because IMAQ Images transferred by references. You must provide images of different destination for each extraction:
Andrey.
Maybe you are looking for
-
As above
-
iPad 4 with retina display screen becomes completely white
I have had my ipad for a couple of years and just recently my screen randomly goes white. I tried holding the home and power button several times, I have also restored and upward he dated at ios 9.2.1 with no solution to the problem. A temporary solu
-
Ports USB Hi-Speed on Satellite P200-140
I use a P200-140 and it was announced as having USB 2.0 ports, but whenever I have something to connect to USB ports, the message appears: this device can perform faster if plug you it into a USB Port of speed Hi. This laptop is supposed to have 4 hi
-
How can I print with terminal server with a printer that uses a user code or identification?
Hello Anyone know who to print with the Terminal Server services with a printer that uses an authentication code? Some printers have the ability to Setup user accounts. When printing with the Terminal services Server the printer displays "authentica
-
I have an intermittent (about 19 times out of 20) on a Pavilion DV7-3060us BIOS failure. After that you press power, the fan and the light turns on, the screen remains black and lock/num lock flashing twice, pause, flash twice, etc.. Sometimes, the